Skip to content

Instantly share code, notes, and snippets.

@otakupahp
Last active May 20, 2022 09:23
Show Gist options
  • Save otakupahp/d740d6a873cd570294fb68c4eaef3dcd to your computer and use it in GitHub Desktop.
Save otakupahp/d740d6a873cd570294fb68c4eaef3dcd to your computer and use it in GitHub Desktop.
Calculate distance between 2 locations
<?php
/**
* Calculate the distance between the 2 locations
*
* @param array $point_a
* @param array $point_b
*
* @return float
*/
function calculate_distance( array $point_a, array $point_b ) : float {
$theta = $point_a['longitude'] - $point_b['longitude'];
$point_a_rad_lat = deg2rad( $point_a['latitude'] );
$point_b_rad_lat = deg2rad( $point_b['latitude'] );
$dist =
( sin( $point_a_rad_lat ) * sin( $point_b_rad_lat ) ) +
( cos( $point_a_rad_lat ) * cos( $point_b_rad_lat ) * cos( deg2rad( $theta ) ) );
$dist = rad2deg ( acos( $dist ) );
return $dist * 60 * 1.1515;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment