Skip to content

Instantly share code, notes, and snippets.

@dansku
Last active December 17, 2015 22:29
Show Gist options
  • Save dansku/5682429 to your computer and use it in GitHub Desktop.
Save dansku/5682429 to your computer and use it in GitHub Desktop.
Measuring distance from two Geo-Points in PHP
function distance($lat1, $long1, $lat2, $long2) {
$R = 6371; // Earth Radius in Km
$dLat = deg2rad($lat2-$lat1);
$dLong = deg2rad($long2-$long1);
$lat1 = deg2rad($lat1);
$lat2 = deg2rad($lat2);
$a = sin($dLat/2)*sin($dLat/2) + sin($dLong/2)*sin($dLong/2)*cos($lat1)*cos($lat2);
$c = 2 * atan2(sqrt($a),sqrt(1-$a));
$d = $R * $c;
// Return with the distance
return $d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment