Home >Backend Development >PHP Tutorial >How to calculate the distance between two latitude and longitude using php
Calculate the distance between two latitude and longitude by using php.
/*
* Calculate the distance between two longitudes and latitudes (unit: meters)
* */
<strong>public function </strong>getdistanceAction() { $lng1=117.27; //经度1 $lat1=31.86; //纬度1 $lng2=120.19; //经度2 $lat2=30.26; //纬度2 $EARTH_RADIUS = 6378137; //地球半径 $RAD = pi() / 180.0; $radLat1 = $lat1 * $RAD; $radLat2 = $lat2 * $RAD; $a = $radLat1 - $radLat2; // 两点纬度差 $b = ($lng1 - $lng2) * $RAD; // 两点经度差 $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; print_r($s); //正确答案:330518.674 }
This article introduces the use PHP to calculate the distance between two longitudes and latitudes. For more related content, please pay attention to the PHP Chinese website.
Related recommendations:
Explain relevant examples of PHP array traversal
Explain the methods of PHP array classification and array creation
Explain the predefined super global array variables of PHP arrays
The above is the detailed content of How to calculate the distance between two latitude and longitude using php. For more information, please follow other related articles on the PHP Chinese website!