There are two points with coordinates (longitude; latitude). How to find out the distance between them in kilometers? Can it be done mathematically? Or maybe through the Google API?
2 answers
Found a function on the Internet, thank you!
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; $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $km = $r * $c; return ($miles ? ($km * 0.621371192) : $km); }
|
And I use a slightly different function, can someone come in handy:
function distance($lat1,$lon1,$lat2,$lon2) { // Convert degrees to radians. $lat1=deg2rad($lat1); $lon1=deg2rad($lon1); $lat2=deg2rad($lat2); $lon2=deg2rad($lon2); // Calculate delta longitude and latitude. $delta_lat=($lat2 - $lat1); $delta_lng=($lon2 - $lon1); return round( 6378137/1000 * acos( cos( $lat1 ) * cos( $lat2 ) * cos( $lon1 - $lon2 ) + sin( $lat1 ) * sin( $lat2 ) ),1 ); }
- Thanks, you will need to compare - iproger
|