How to indicate that a point is inside the circle or rectangle by geometry library in Google maps v3?

怎甘沉沦 提交于 2019-12-31 07:40:33

问题


Look at this:

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

This is the condition to indicates that a point is inside the polygon or no. I'm looking for something similar but for circle or rectangle.

something like this:

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

Or for rectangle:

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

回答1:


Both Circle and Rectangle have method

getBounds() LatLngBounds Returns the bounds of this rectangle.

Try call method getBounds() witch return google.maps.LatLngBounds item. It has method

contains(latLng:LatLng) boolean Returns true if the given lat/lng is in this bounds.

something like

(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))

Hope it is what you need




回答2:


Important notice: If you don't set getSouthWest and getNorth... it always return 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");
}


来源:https://stackoverflow.com/questions/22343475/how-to-indicate-that-a-point-is-inside-the-circle-or-rectangle-by-geometry-libra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!