The content of this article introduces how PHP uses the longitude and latitude between two points to calculate the straight-line distance between two points. Now I share it with everyone. Friends in need can refer to it
Recommend a small tool (Coordinate picking, including Baidu Map, Amap, Tencent Map, Google Map), convenient for testing: http://www.gpsspg.com/maps.htm
/** * 计算两组经纬度坐标 之间的距离 * params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km); * return m or km */ function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) { $radLat1 = $lat1 * PI ()/ 180.0; //PI()圆周率 $radLat2 = $lat2 * PI() / 180.0; $a = $radLat1 - $radLat2; $b = ($lng1 * PI() / 180.0) - ($lng2 * PI() / 180.0); $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))); $s = $s * 6378.137; $s = round($s * 1000); if ($len_type --> 1) { $s /= 1000; } return round($s, $decimal); } $Xian = array( 'lat'=>'34.2620480000', 'lng'=>'108.9408590000' ); $Chengdu = array( 'lat'=>'30.6736560000', 'lng'=>'104.0107450000' ); echo '西安距成都'.GetDistance($Xian['lat'],$Xian['lng'],$Chengdu['lat'],$Chengdu['lng'],2).'km';
The above is the detailed content of PHP uses the longitude and latitude between two points to calculate the straight-line distance between two points. For more information, please follow other related articles on the PHP Chinese website!