2011-09-01 94 views
1

我需要確定某些LatLngs是否位於Google地圖圈內(其中一個爲http://code.google.com/apis/maps/documentation/javascript/overlays.html#Circles)。我該如何解決這個問題?我製作圈子的標記是:計算LatLng與LatLng之間的距離(或圓圈中的點數) - Google Maps v3

geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     circlemarker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
     THEradius = parseFloat(THEradius); 
     var populationOptions = { 
      strokeColor: "#BDAEBB", 
      strokeOpacity: 0.8, 
      strokeWeight: 2, 
      fillColor: "#BDAEBB", 
      fillOpacity: 0.5, 
      map: map, 
      center: results[0].geometry.location, 
      radius: THEradius 
     }; 
     cityCircle = new google.maps.Circle(populationOptions); 
     map.fitBounds(cityCircle.getBounds()); 
    } 
}); 

我可以使用半徑嗎?

回答

2
var distance = google.maps.geometry.spherical.computeDistanceBetween(
     results[0].geometry.location, otherLatLng); 

if (distance <= THEradius) {...} else {...} 

我希望你的作品。見http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical

+0

順便說一句,你**不能**使用畢達哥拉斯定理,因爲地球是不平坦的! – kargeor

+0

在網站上的第一個很好的答案,非常感謝,絕對是最好的解決方案和我正在尋找的。你知道這個選項是否有任何請求限制? – rickyduck

+1

該文檔沒有提到任何限制。我不確定該函數是在本地還是在服務器上進行評估。 – kargeor

1

您需要做的是將經緯度列表轉換爲谷歌座標空間或將圓轉換爲緯度座標空間。

轉換的方式取決於您使用的語言,但有些網站會爲您轉換,如果它是一次性的。

一旦你獲得了與你的圓相同的座標空間中的緯度位置,你可以使用簡單的畢達哥拉斯數學來計算出位置是否小於圓的半徑(如你所建議的那樣) 。

HYP = (OPP^2 * ADJ^2)^0.5 

其中:

OPP is the difference in x direction from the centre of the circle 
ADJ is the difference in y direction from the centre of the circle. 
HYP is the distance in a straight line from the centre of the circle 
1

在數學方面,找到從一個點到另一個2D中的距離,使用Pythagoras

X = X1 - X2 
Y = Y1 - Y2 

(以上有效地從一個點到另一個計算的向量)

從1至2 =開方距離(X^2 + Y^2)

然後你就可以比較你的半徑。如果距離小於半徑,則該點位於圓內。

您需要首先獲取圓的中心點和試圖比較的點。這些必須在相同的座標空間中。

相關問題