/**
* 计算距离
* @param Decimal $lat1 起点纬度
* @param Decimal $lng1 起点经度
* @param Decimal $lat2 终点纬度
* @param Decimal $lng2 终点经度
*/
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)));
$Distance = $earthRadius * $stepTwo;
if ($Distance >= 1000) {
$Distance = round($Distance/1000,2);
return $Distance . 'km';
}
return round($Distance) . 'm';
}
网友评论