gpt4 book ai didi

php - 用经度和纬度计算距离有不同的结果

转载 作者:可可西里 更新时间:2023-11-01 07:27:49 24 4
gpt4 key购买 nike

我正在尝试获取两个位置的距离,我真的需要使用 mySQL 完成此操作,因为我必须过滤大量记录。不幸的是,mysql 语句的结果与 google maps 的结果不同。可能是什么原因。

我的 Mysql 语句是 (Values hardcoded)

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 

但是如果我使用 google 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.5m(你的公式不太正确,但很接近)。与谷歌的不同之处在于,谷歌试图计算道路上两点的距离,以某种方式将一个点投影到道路上。见下图。 (红线是根据坐标绘制的,距离为 85.5m,蓝线是谷歌渲染的,不知何故被“捕捉”到道路上) The actual line is red has a distance of 85.5m, the blue line is render by Google, is 'snapped' to the road

关于php - 用经度和纬度计算距离有不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18459179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com