javascript - 谷歌地图 API : determine if point is inside shape

标签 javascript google-maps google-maps-api-3

我在 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/

相关文章:

javascript - 在javascript中添加时间

javascript - 在 ng-repeat 中使用 ng-click 和 ng-hide/show,Angular

javascript - ruby on Rails 无法 JavaScript 未加载

android - LocationManager 与 Google Maps API 的准确性

javascript - Google map API 空白输出

javascript - Sencha Touch : Can't get map object from Ext. map 提供给谷歌方向服务

javascript - 使用 jQuery 迭代 JSON 对象

javascript - 当用户手动滚动时如何防止自动滚动?

android - 在谷歌地图中更改 autocomplete_fragment 的图标

javascript - 当水 visibility 关闭时,谷歌会在景观上绘制奇怪的多边形