Skip to content

Instantly share code, notes, and snippets.

@oleg-andreyev
Created February 22, 2020 19:26
Show Gist options
  • Save oleg-andreyev/82ef8b710308c3b2b2fa56712a246493 to your computer and use it in GitHub Desktop.
Save oleg-andreyev/82ef8b710308c3b2b2fa56712a246493 to your computer and use it in GitHub Desktop.
Haversine formula
/**
* Calculates the geodesic distance between two points specified by radian latitude/longitude using the
* Haversine formula (hf)
*/
function hf($lat1, $lng1, $lat2, $lng2)
{
$R = 6371e3;
$lng1 = deg2rad($lng1);
$lat1 = deg2rad($lat1);
$lng2 = deg2rad($lng2);
$lat2 = deg2rad($lat2);
$deltaLat = $lng2 - $lng1;
$deltaLng = $lat2 - $lat1;
$a = (sin($deltaLat / 2) ** 2) + (cos($lng1) * cos($lng2) * (sin($deltaLng / 2) ** 2));
$c = 2 * asin(min(1, sqrt($a)));
$d = $R * $c;
return $d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment