Skip to content

Instantly share code, notes, and snippets.

@kaspergrubbe
Created September 20, 2013 08:35
Show Gist options
  • Save kaspergrubbe/6634792 to your computer and use it in GitHub Desktop.
Save kaspergrubbe/6634792 to your computer and use it in GitHub Desktop.
<?php
// Haversine
function distancekm($lat1,$lon1,$lat2,$lon2)
{
$R = 6371; // km
$dLat = deg2rad($lat2-$lat1); // to radians
$dLon = deg2rad($lon2-$lon1);
$a = sin($dLat/2) * sin($dLat/2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($dLon/2) * sin($dLon/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $R * $c;
return $d;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment