/ Published in: PHP
This PHP function calculates the distance between to pairs of latitude longitude coordinates. Returns the distance in miles or kilometers.
Expand |
Embed | Plain Text
function distance($lat1, $lng1, $lat2, $lng2, $miles = true) { $pi80 = M_PI / 180; $lat1 *= $pi80; $lng1 *= $pi80; $lat2 *= $pi80; $lng2 *= $pi80; $r = 6372.797; // mean radius of Earth in km $dlat = $lat2 - $lat1; $dlng = $lng2 - $lng1; $km = $r * $c; return ($miles ? ($km * 0.621371192) : $km); }
Comments
Subscribe to comments
You need to login to post a comment.

great work...tested it with my zipcode database and worked great
Thanks for the function. Was very helpful on a number of my geographic sites to show the distance of nearby points (see example)
Thanks! Great job!