我的目标是检测标记是否位于形状奇特的多边形对象内(想想 25 个不同的点)。我注意到 LatLngBounds 属性仅包含两组 LatLngs,即使在使用 .extend 函数之后也是如此,例如:
((34.347971491244955, -122.40966796875), (34.829586636768205, -120.9649658203125))
这是正常行为吗?
下面的代码是我正在构建的程序的一小段,但包含有关我如何使用扩展函数的相关信息。如果我不能结合使用 .extend() 和 .contains(),有没有人知道如何检测标记是否在形状奇特的多边形内?
var coordinates = (polygon.getPath().getArray()); //get
var bounds = new google.maps.LatLngBounds();
function extendBounds(bounds, coordinates) {
for (b in coordinates) {
bounds.extend(coordinates[b]);
};
console.log(bounds.toString());
};
最佳答案
多边形的边界是一个矩形。如果要检测标记是否在多边形中,请使用 google.maps.geometry.poly namespace 中的 containsLocation 函数,将多边形和标记的位置传递给它
containsLocation(point:LatLng, polygon:Polygon) | boolean | Computes whether the given point lies inside the specified polygon.
关于google-maps-api-3 - 扩展多边形的边界 -- Google Maps V3 Javascript API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16114387/