正文
思路:
公式(W为纬度对应的弧度,J为经度对应的弧度):
源码:
/**
* $lat 纬度
* $lng 经度
* return int 单位:米
*/
function getDistance($lat1, $lng1, $lat2, $lng2)
{
$earthRadius = 6367000; // 地球的近似半径(米)
$lat1 = ($lat1 * pi()) / 180;
$lng1 = ($lng1 * pi()) / 180;
$lat2 = ($lat2 * pi()) / 180;
$lng2 = ($lng2 * pi()) / 180;
$calcLongitude = $lng2 - $lng1;
$calcLatitude = $lat2 - $lat1;
$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
$calculatedDistance = $earthRadius * $stepTwo;
return round($calculatedDistance);
}
参考资料
PHP根据经纬度计算距离 https://www.cnblogs.com/lyc94620/p/9459592.html
PHP根据经纬度计算两地之间的距离 https://blog.csdn.net/sinat_35861727/article/details/64444107