我想要得到兩個位置的距離,我真的需要用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
我越來越28米的距離。
無論如何我可以解決這個問題。我需要一個解決方案來在MySQL結束工作。 Appriciate你所有的支持。
編輯:
我試過用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);
?>
這可能是因爲你正在使用不同的(準確度較低)公式 - Vincenty和Haversine是兩個主要的大圓公式(Vincenty更準確,但需要更多處理能力來計算),我想Google會使用其中的一個......你使用一個簡單得多的公式(餘弦的球面法則),這將比前面提到的大圓距離公式更精確 –