/*
* 1.纬度1,经度1,纬度2,经度2
* 2.返回结果是单位是KM。
* 3.保留一位小数
*/
function getDistance($lat1=29.68328,$lng1=106.60583,$lat2=38.21229,$lng2=112.18140)
{
//将角度转为狐度
$radLat1 = deg2rad($lat1);//deg2rad()函数将角度转换为弧度
$radLat2 = deg2rad($lat2);
$radLng1 = deg2rad($lng1);
$radLng2 = deg2rad($lng2);
$lat = $radLat1 - $radLat2;
$lng = $radLng1 - $radLng2;
// dump( sin($lat/2) );
$s = 2*asin(
sqrt(
pow(sin($lat/2),2)
+cos($radLat1)
*cos($radLat2)
*pow(sin($lng/2),2)
)
)*6371;
return $s;
}
mysql
方案1
SELECT
*,
RADIANS(start_map_j) AS begin_lng,
RADIANS(start_map_w) AS begin_lat,
RADIANS(end_map_j) AS end_lng,
RADIANS(end_map_w) AS end_lat,
RADIANS(start_map_j) - RADIANS(end_map_j) AS lng,
RADIANS(start_map_w) - RADIANS(end_map_w) AS lat,
2 *
ASIN(
SQRT(
POW(SIN( (RADIANS(start_map_w) - RADIANS(end_map_w) )/2),2) +
+COS( RADIANS(start_map_w) )
*COS( RADIANS(end_map_w) )
*POW( (SIN(RADIANS(start_map_j) - RADIANS(end_map_j))/2),2)
)
) * 6371
AS km
FROM
ss_conta_goods
LIMIT 0,10
方案2
SELECT
*,
2 *
ASIN(
SQRT(
POW(SIN( lat/2),2) +
+COS( begin_lat )
*COS( end_lat )
*POW( SIN(lng/2),2)
)
) * 6371
AS km
FROM
(
SELECT
*, RADIANS(start_map_j) AS begin_lng,
RADIANS(start_map_w) AS begin_lat,
RADIANS(end_map_j) AS end_lng,
RADIANS(end_map_w) AS end_lat,
RADIANS(start_map_j) - RADIANS(end_map_j) AS lng,
RADIANS(start_map_w) - RADIANS(end_map_w) AS lat
FROM
ss_conta_goods AS a
) AS b