const EARTH_RADIUS = 6378.137; //地球半径
const PI = 3.1415926; //圆周率
if (!function_exists('getDistance')) {
/**
* 求两个百度坐标之间的距离
* @param $lat1 坐标1经度
* @param $lng1 坐标1纬度
* @param $lat2 坐标2经度
* @param $lng2 坐标2纬度
* @param int $len_type 距离单位 (1 米 2:千米)
* @param int $decimal 精度
* @return float
*/
function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 0)
{
$radLat1 = $lat1 * PI / 180.0;
$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 * EARTH_RADIUS;
$s = round($s * 1000) / 1;
if ($len_type > 1) {
$s /= 1000;
}
return round($s, $decimal);
}
}
工作中遇到通过百度坐标求两点之间的距离,备忘一下