1

我目前正在使用 Google Maps API v3.13。不过,我的编码已经停了下来,文档并没有真正帮助我。

我所做的是我已经实现了 DrawingLibrary,我可以在地图上绘制形状。当我完成绘制某些东西时,我想做的是获取所绘制形状的边界框/角(我只激活了折线和矩形)。

然后我想使用这个区域来查看其中是否有任何标记,然后让它们“有弹性”或类似的东西。所以我的问题是,我如何获得用户绘制的区域?这些数据是什么格式的?每个角的坐标?我是否必须将 DrawingLibrary 的功能与 GeometryLibrary 结合起来才能做到这一点?

我已经检查了这些文档,但仍然无法找到解决方案。 https://developers.google.com/maps/documentation/javascript/geometry https://developers.google.com/maps/documentation/javascript/drawing

这是我到目前为止所拥有的:

function bindOverlayFinishedEvents() {
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
    if (event.type == google.maps.drawing.OverlayType.POLYGON) {
        //bounds = event.overlay.getBounds();
    }
    else if (event.type == google.maps.drawing.OverlayType.RECTANGLE) {
        //bounds = event.overlay.getBounds();
    }
});

}

任何帮助将不胜感激!

4

1 回答 1

3

由于您的目标是确定标记是否位于某个区域内,因此以下示例演示了如何根据形状类型来完成它:

  • 对于圈子:circle.getBounds().contains(latLng) && google.maps.geometry.spherical.computeDistanceBetween(circle.getCenter(), latLng) <= circle.getRadius()
  • 对于矩形: rectangle.getBounds().contains(latLng)
  • 对于多边形:google.maps.geometry.poly.containsLocation(latLng,polygon)

判断点是否位于形状内部的包装函数:

//wrapper for contains function 
var shapeContains = function (shape, latLng) {
    if (shape instanceof google.maps.Circle)
        return shape.getBounds().contains(latLng) && google.maps.geometry.spherical.computeDistanceBetween(shape.getCenter(), latLng) <= shape.getRadius();
    else if (shape instanceof google.maps.Rectangle)
        return shape.getBounds().contains(latLng);
    else if(shape instanceof google.maps.Polygon)
        return google.maps.geometry.poly.containsLocation(latLng, shape);
    else 
        throw new Error("contains is not supported for this type of shape");
}

Codepen(演示)

于 2016-01-25T13:06:00.230 回答