Created
March 6, 2021 09:08
-
-
Save nickopris/22a994585d8b2e663edd3efdabc734c4 to your computer and use it in GitHub Desktop.
Geolocation distance between two points with lat and lon values
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 | |
/** | |
* https://www.movable-type.co.uk/scripts/latlong.html | |
* @param $lat1 | |
* @param $lat2 | |
* @param $lon1 | |
* @param $lon2 | |
* | |
* @return float|int | |
*/ | |
function _haversine($lat1, $lat2, $lon1, $lon2) { | |
$R = 6371; // earth radius in km | |
$fione = $lat1 * pi() / 180; // φ, λ in radians | |
$fitwo = $lat2 * pi() / 180; | |
$deltafi = ($lat2 - $lat1) * pi() / 180; | |
$deltadelta = ($lon2 - $lon1) * pi() / 180; | |
$a = sin($deltafi / 2) * sin($deltafi / 2) + | |
cos($fione) * cos($fitwo) * | |
sin($deltadelta / 2) * sin($deltadelta / 2); | |
$c = 2 * atan2(sqrt($a), sqrt(1 - $a)); | |
$d = $R * $c; // in km | |
return $d; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment