2013-08-27 60 views
4

我想要得到兩個位置的距離,我真的需要用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結束工作。 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); 

?> 
+2

這可能是因爲你正在使用不同的(準確度較低)公式 - Vincenty和Haversine是兩個主要的大圓公式(Vincenty更準確,但需要更多處理能力來計算),我想Google會使用其中的一個......你使用一個簡單得多的公式(餘弦的球面法則),這將比前面提到的大圓距離公式更精確 –

回答

2

您提供的地圖鏈接有不同的經度和緯度。

 "northeast" : { 
      "lat" : 6.913845999999999, 
      "lng" : 79.9733002 
     }, 
     "southwest" : { 
      "lat" : 6.913794800000001, 
      "lng" : 79.9730488 
     } 

通過使用在查詢中,它提供了正確的距離。

4

我試過大圓公式,結果是85.5m(你的公式不太正確,但是很接近)。與谷歌不同的是,谷歌試圖計算一條路上的兩個點的距離,不知何故它被投射到路上。看下面的圖片。 (紅線是座標圖,距離爲85.5米,藍線由谷歌渲染,不知何故'搶購') The actual line is red has a distance of 85.5m, the blue line is render by Google, is 'snapped' to the road

+0

僅僅是因爲它更直觀,更容易理解?你爲什麼那麼卑鄙? – haibuihoang

相關問題