2013-03-01 15 views
1

我在我的應用程序中使用Google Maps API(帶有MVC的ASP.NET)。我們稱之爲「原點」(可以是多邊形,折線或標記)和另一個座標數組,我們稱之爲「目的地」 (可以是多邊形,折線或標記)。如何使用JavaScript在Google地圖上獲得兩個形狀之間的最短距離?

我想計算「起源」和「目的地」之間的最短距離。我怎樣才能做到這一點?

+1

http://stackoverflow.com/questions/3838493/d​​istance-between-two-geo-locations – user15 2013-03-01 08:17:42

+0

@ user15您的鏈接僅關於單一座標(原點和目的地都是標記),而不是形狀。 – Matmarbon 2014-08-04 13:49:31

回答

1

一種解決方案是採用here找到的選項之一,並計算從原點中的每個點到目的地中的每個點的距離。最小的距離是兩個形狀之間的距離。

代碼可能是這樣的(未經測試):

var minDistance = Number.POSITIVE_INFINITY; 
for (var i=0; i<origin.length; i++){ 
    for (var j=0; j<destination.length; j++){ 
     var dist = google.maps.geometry.spherical.computeDistanceBetween(origin[i], destination[j]); 
     if (dist < minDistance) 
     minDistance = dist; 
    } 
} 

這如果性能是一個問題,可能會進行優化。欲瞭解更多信息,我會看看這個question及其解決同一問題的答案,儘管從純粹的數學角度來看。

+0

與aecend的解決方案相同。看看[這張圖](http://i.stack.imgur.com/hzJdY.png)。 – Matmarbon 2014-08-05 21:42:01

1

我建議您使用餘弦球形定律來計算點之間的距離。如果你有緯度和經度起源的數組,和緯度的數組和目的地經度座標,那麼你可以做這樣的事情:

var origins = [{lat: "35.5", lon: "-80.0"}, ...]; // Array of origin coordinates 
var destinations = [{lat: "34.5", lon: "-80.0"}, ...]; // Array of destination coordinates 
var shortestDistance = null; 
var shortestPair = []; 

for (i = 0; i < origins.length; i++) { 
    for (j = 0; j < destinations.length; j++) { 

     var lat1 = origins[i].lat.toRadians(); 
     var lat2 = destinations[j].lat.toRadians(); 
     var lon = (destinations[j].lon - origins[i].lon).toRadians(); 
     var R = 6371; // gives distance in kilometers 
     var calcDistance = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon)) * R; 

     if (shortestDistance === null || calcDistance < shortestDistance) { 
      shortestPair[0] = origins[i]; // Store the origin coordinates 
      shortestPair[1] = destinations[j]; // Store the destination coordinates 
      shortestDistance = calcDistance; // Update the shortest distance 
     } 
    } 
} 

/* After this runs, you'll have the array indexes for the origin and 
destination with the shortest distance as well as the actual distance (kilometers) 
in the array shortestPair and the variable shortestDistance respectively. 

For miles, divide shortestDistance by 1.609344 
For nautical miles, divide shortestDistance by 1.852 

*/ 

這似乎是一個簡單的方法不是試圖用用於距離計算的Maps API。上述配方來源於http://www.movable-type.co.uk/scripts/latlong.html。如果你需要更精確的計算,你也可以使用半正式公式;它在我鏈接的頁面上詳細說明。

+0

您的解決方案存在邏輯錯誤。看看[這個問題的圖形演示](http://i.stack.imgur.com/hzJdY.png) – Matmarbon 2014-08-05 21:38:57

+0

@Matmarbon:是的,我的解決方案按原樣實施將不會解決這種情況,但通過比較多邊形的每個頂點,我的解決方案仍然有效。要進一步研究這個問題,需要計算每個多邊形中每個段沿點的距離,這是一個計算量很大的NP完全問題。 – aecend 2014-08-05 22:34:18

+0

因此,長話短說:「忘了它,它太複雜了」?看,確切地說,這個問題沒有提到任何有關性能的問題,但需要解決這個問題。無論如何,我感謝你的幫助到目前爲止:) – Matmarbon 2014-08-07 19:09:29

0
function moveAlongPath(points, distance, index) { 
    index = index || 0; 

    if (index < points.length && typeof points[index +1] !="undefined") { 
    var polyline = new google.maps.Polyline({ 
     path: [points[index], points[index + 1]], 
     geodesic: true, 
     strokeColor: '#FF0000', 
     strokeOpacity: 1.0, 
     strokeWeight: 2 
    }); 

    var distanceToNextPoint = polyline.Distance(); 
    if (distance <= distanceToNextPoint) { 
     return polyline_des(points[index],points[index + 1], distance); 
    } 
    else { 
     return moveAlongPath(points, 
      distance - distanceToNextPoint, 
      index + 1); 
    } 
    } 
    else { 
    return null; 
    } 
} 
+0

什麼是'polyline_des'? – Matmarbon 2014-08-08 07:38:37

+0

它的谷歌地圖路線模式其繪製路徑之間latlng之間直接忽略它,如果你不使用它使用你的潰敗點 – 2014-08-11 04:17:40

1

好,從中尋找數學的角度:

你的問題是要找到在空間中的點和VEKTOR線或平面之間的最短距離。

所以,如果你有你的COORDS像[a1,a2,a3][b1,b2,b3]陣列在三維空間中這2點之間的距離就像是有三個元素勾股定理: sqrt[(a1-b1)²+(a2-b2)²+(a3-b3)²]=d(a,b)

我知道這並不考慮地球曲率,但對於「短」距離來說這並不重要。

如果你瞭解一些數學的wikipedia文章也可以幫助你。 http://en.wikipedia.org/wiki/Euclidean_distance#Three_dimensions

編輯14年8月12日:
要充分地考慮的弧度,你可以做一個簡單的計算:
(1)你已經知道地球
(2)你知道的距離約。地球的半徑

以你的出發點(A)和你的目的地(B)已知,你現在用地心(C)建立一個三角形。您現在可以計算(C)(sin/cos/tan)處的角度。現在,您可以獲得地球的長度(包括曲率)。

([地球邊界]/360°)* [在(C)處的角度] =從地球曲率上的(A)到(B)的實例。