Home > Article > Backend Development > How to use longitude and latitude in php to calculate the distance between two points (pure code)
The content of this article is about how PHP uses longitude and latitude to calculate the distance between two points (pure code). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.
function rad($d) { return $d * 3.1415926535898 / 180.0; } function GetDistance($lat1, $lng1, $lat2, $lng2) { $EARTH_RADIUS = 6378.137; $radLat1 = rad($lat1); $radLat2 = rad($lat2); $a = $radLat1 - $radLat2; $b = rad($lng1) - rad($lng2); $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2))); $s = $s *$EARTH_RADIUS; $s = round($s * 10000) / 10000; return $s; }
Recommended related articles:
How does PHP obtain the code instance of the IP region (pure code)
How does PHP achieve return json data (code)
The above is the detailed content of How to use longitude and latitude in php to calculate the distance between two points (pure code). For more information, please follow other related articles on the PHP Chinese website!