我在 map 上创建了一个矩形以及 bounds_changed
的监听器。它调用一个方法来确定一个点是否在调整后的矩形内。但是,当我更改矩形的大小时,我收到错误无法读取未定义的属性“getLength”
。此错误来自对 containsLocation
的调用。
在我的初始化中:
rectangle = new google.maps.Rectangle({
bounds: bounds,
editable: true,
draggable: true,
geodesic: true,
map: map
});
google.maps.event.addListener(rectangle , 'bounds_changed', isWithinPoly);
确定点是否落在调整矩形内的函数:
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);
}
最佳答案
containsLocation
需要一个多边形作为参数,而不是一个矩形。
对于矩形,请使用边界的方法contains
:
var isWithinRectangle = rectangle.getBounds().contains(point);
关于javascript - 谷歌地图 API : determine if point is inside shape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26413963/