计算两个经纬度之间的距离,可以用于查看用户与商家之间的距离

来源:互联网 发布:sql日月年转年月日 编辑:程序博客网 时间:2024/06/05 18:21

查到的一个方法,还不错,所以记录下来,

数据库中存储了商家的经纬度

前端用接口获取到用户的经纬度

然后调用如下方法,就可以计算出两个之间的距离了,单位:m


/**
* 计算两个经纬度之间的距离
*/

public function getDistance($u_la, $u_lon, $s_la, $s_lon)
{
// 将角度转为狐度
$rad_u_la = deg2rad($u_la); //deg2rad()函数将角度转换为弧度
$rad_s_la = deg2rad($s_la);
$rad_u_lon = deg2rad($u_lon);
$rad_s_lon = deg2rad($s_lon);
$a = $rad_u_la - $rad_s_la;
$b = $rad_u_lon - $rad_s_lon;
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($rad_u_la) * cos($rad_s_la) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
return $s;

}


http://www.cnblogs.com/cqingt/p/5466648.html

可以参考:http://www.tcao.net/article/71.html  这位博主写的不错

原创粉丝点击