计算两地距离

/*
 * 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

 

转载于:https://my.oschina.net/sloan521/blog/2991539

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值