问题
I have created a rectangle on the map along with a listener for bounds_changed. It calls a method to determine if a point is within the adjusted rectangle. However I'm getting the error Cannot read property 'getLength' of undefined when I change the size of the rectangle. This error is coming from the call to containsLocation.
In my init:
rectangle = new google.maps.Rectangle({
bounds: bounds,
editable: true,
draggable: true,
geodesic: true,
map: map
});
google.maps.event.addListener(rectangle , 'bounds_changed', isWithinPoly);
function to determine if point falls in adjusted rectangle:
function isWithinPoly(){
console.log(rectangle);
var point = new google.maps.LatLng(51.331, 3.538);
var isWithinPolygon = google.maps.geometry.poly.containsLocation(point, rectangle);
console.log(isWithinPolygon);
}
回答1:
containsLocation expects a polygon as argument, not a rectangle.
For a rectangle use the method contains of the bounds:
var isWithinRectangle = rectangle.getBounds().contains(point);
来源:https://stackoverflow.com/questions/26413963/google-maps-api-determine-if-point-is-inside-shape