/* 计算两组经纬度坐标之间的距离
* @param $lat1 纬度1
* @param $lng1 经度1
* @param $lat2 纬度2
* @param $lng2 经度2
* @param int $len_type 返回值类型(1-m 2-km)
* @param int $decimal 保留小数位数
* @return float
*/
function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
{
$radLat1 = $lat1 * 3.1415926 / 180.0;
$radLat2 = $lat2 * 3.1415926 / 180.0;
$a = $radLat1 - $radLat2;
$b = ($lng1 * 3.1415926 / 180.0) - ($lng2 * 3.1415926 / 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);
}
$p1 = [121.554874, 29.813311];
$p2 = [121.554586, 29.813444];
echo getDistance($p1[0], $p1[1], $p2[0], $p2[1], 1, 2) . "米";
Done!