How to indicate that a point is inside the circle

2020-05-10 00:15发布

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)
{
}

2条回答
该账号已被封号
2楼-- · 2020-05-10 00:41

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

查看更多
来,给爷笑一个
3楼-- · 2020-05-10 00:51

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");
}
查看更多
登录 后发表回答