Last active
May 20, 2022 09:23
-
-
Save otakupahp/d740d6a873cd570294fb68c4eaef3dcd to your computer and use it in GitHub Desktop.
Calculate distance between 2 locations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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