微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 经度和纬度的距​​离计算有不同的结果

我试图获得两个位置的距离,我真的需要用MysqL完成,因为我必须过滤大量的记录.不幸的是,mysql语句结果与谷歌地图结果不同.可能是什么原因.

我的MysqL语句是(值硬编码)

SELECT ((ACOS(SIN(6.914556 * PI() / 180) * SIN(6.913794 * PI() / 180) + COS(6.914556 * PI() / 180) * COS(6.913794 * PI() / 180) * COS((79.973194- 79.97330) * PI() / 180)) * 180 / PI()) * 60 * 1.609344 * 1000) AS `distance` 

我得到74.27米的距离.

然后我使用了我发现的另一个sql语句,它给出了85.53米.

SELECT (1.609344 * 1000 * 3959 * acos( cos( radians(6.914556) ) * cos( radians( 6.913794 ) ) * cos( radians( 79.97330 ) - radians(79.973194) ) + sin( radians(6.914556) ) * sin( radians( 6.913794) ) ) ) AS distance 

但是,如果我使用谷歌API

http://maps.googleapis.com/maps/api/directions/json?origin=6.914556,79.973194&destination=6.913794,79.97330&sensor=false

我距离是28米.

无论如何我可以解决这个问题.我需要一个MysqL端工作的解决方案.赞赏你所有的支持.

编辑:

我尝试使用PHP仍然有距离差异.

<?PHP
function distance($lat1, $lon1, $lat2, $lon2, $unit) {

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344);
  } 
  else if ($unit == "M") {
     return ($miles * 1.609344 * 1000);
  }else if ($unit == "N") {
    return ($miles * 0.8684);
  } else {
    return $miles;
  }
}

 function getDrivingdistance($inLatitude,$inLongitude,$outLatitude,$outLongitude)
    {
        if(empty($inLatitude) || empty($inLongitude) ||empty($outLatitude) ||empty($outLongitude))
            return 0;

        // Generate URL
        $url = "http://maps.googleapis.com/maps/api/directions/json?origin=$inLatitude,$inLongitude&destination=$outLatitude,$outLongitude&sensor=false";

        // Retrieve the URL contents
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $url);
        $jsonResponse = curl_exec($c);
        curl_close($c);

        $dataset = json_decode($jsonResponse);
        if(!$dataset)
            return 0;
        if(!isset($dataset->routes[0]->legs[0]->distance->value))
            return 0;
        $distance = $dataset->routes[0]->legs[0]->distance->value;

        return $distance;
    }
echo distance(6.914556,79.973194,6.913794,79.97330,'M') . "<br>";
echo getDrivingdistance(6.914556,79.973194,6.913794,79.97330);

?>

解决方法:

我尝试了大圈公式,结果是85.5米(你的公式不太正确,但很接近).与谷歌的不同之处在于,谷歌试图计算出道路上两点的距离,不知何故,它会向道路投射一点.见下图. (红线是坐标图,距离为85.5米,蓝线由谷歌渲染,不知何故被“抢购”到了道路上)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐