复制自:https://www.geodatasource.com/developers/php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
function getDistanceBetweenPoints($lat1, $lon1, $lat2, $lon2, $unit = 'K') { if (($lat1 == $lat2) && ($lon1 == $lon2)) { return 0; }
$theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; $unit = strtoupper($unit);
if ($unit === 'K') { return $miles * 1.609344; } else if ($unit === 'N') { return $miles * 0.8684; } else { return $miles; } }
|
测试
这边通过高德地图拿到了广州昌岗、客村两个地铁站的经纬度。这边代码计算的是一个直线距离,如果需要一个精准的最好使用地图的接口,它们考虑了道路等方面。
1 2 3
| getDistanceBetweenPoints(113.276885, 23.090654, 113.320331, 23.096197);
|
参考