2014-03-12 17 views
-1

看看這個:如何通過Google地圖v3中的幾何庫指示某個點位於圓形或矩形內?

if(google.maps.geometry.poly.containsLocation(new google.maps.LatLng(arrayLatitude[counter1], arrayLongitude[counter1]), polygon)) 
{ 
} 

這是爲了表明一個點在多邊形或裏面沒有的條件。 我正在尋找類似的東西,但對於圓形或矩形。

是這樣的:

if(google.maps.geometry.circle.containsLocation(new google.maps.LatLng(arrayLatitude[counter1], arrayLongitude[counter1]), circle) 
{ 
} 

或爲矩形:

if(google.maps.geometry.rectangle.containsLocation(new google.maps.LatLng(arrayLatitude[counter1], arrayLongitude[counter1]), rectangle) 
{ 
} 

回答

4

兩個CircleRectangle有方法

的getBounds()的LatLngBounds返回此矩形的界限。

嘗試調用方法getBounds()witch return google.maps.LatLngBounds item。它有方法

contains(latLng:LatLng)boolean如果給定的lat/lng在這個範圍內,則返回true。

(new google.maps.Circle({center: new google.maps.LatLng(YOUR_LAT, YOUR_LNG),radius: YOUR_RADIUS})).getBounds().contains(google.maps.LatLng(POINT_LAT, POINT_LNG)) 

希望這是你需要

+0

謝謝。我需要一些東西來處理'幾何'庫。可以? –

+0

不是。它是非標準的GM功能。只需創建對象(圓或矩形與您需要的參數,並調用方法來檢查內部圖中的點)。可能我不明白問題,對不起?你能舉例說明你在jsbin或plunker上需要什麼嗎? –

+0

我想要一個像這樣的方法:var distanceInMetres = google.maps.geometry.spherical.computeDistanceBetween(latLngCircleCenter,latLngPoint);它工作嗎? –

0

重要注意事項是什麼:如果您沒有設置getSouthWest和getNorth ......它總是返回false: Google Maps v3 map.getBounds().containsLatLng is not a function

var temp_rectangle = new google.maps.Rectangle({ 
    bounds: new google.maps.LatLngBounds(
    new google.maps.LatLng(array_shapes_object[counter].getSouthWestLat, array_shapes_object[counter].getSouthWestLng), 
    new google.maps.LatLng(array_shapes_object[counter].getNorthEastLat, array_shapes_object[counter].getNorthEastLng)) 
}); 

if(temp_rectangle.getBounds().contains(new google.maps.LatLng(arrayLatitude[counter1], arrayLongitude[counter1]))) 
{ 
    alert("Inside"); 
} 
else 
{ 
    alert("Outside"); 
} 
相關問題