gmaps.js - 绘制多边形

gmaps.js - Drawing a Polygon

我正在使用 gmaps.js 并且我可以使用此代码绘制多边形

var paths = [];
function drawPoly(p1, p2) {
  paths.push([p1, p2]);
  console.log(paths);

  oldPolygon = null;

  map.drawPolygon({
      paths: paths,
      strokeColor: '#432070',
      strokeOpacity: 1,
      strokeWeight: 3,
      fillColor: '#432070',
      fillOpacity: 0.6
    });
}

但我的问题是它们相互重叠导致了这个,

我的问题是如何删除重叠的多边形(旧多边形),以便剩余的多边形成为最后生成的多边形。我希望我解释得很好。谢谢

如果您真的想使用多段线或多边形,您可以使用:

.setMap(Null)

您可以在这里找到更多相关信息:https://developers.google.com/maps/documentation/javascript/examples/polyline-remove

但我认为您可能想要使用可编辑的多边形:https://jsfiddle.net/3L140cg3/16/

我的问题的解决方法是使用 map.removePolygon 方法:

var paths = [];
var oldPolygon;
function drawPoly(p1, p2) {
    paths.push([p1, p2]);
    console.log(paths);

    polygons = map.drawPolygon({
       paths: paths,
       strokeColor: '#432070',
       strokeOpacity: 1,
       strokeWeight: 3,
       fillColor: '#432070',
       fillOpacity: 0.6
    });

    // remove old one if exists.
    if(oldPolygon != null){
        map.removePolygon(oldPolygon);
    }

    // ... and save a reference to the new polygon for next time around.
    oldPolygon = polygons;
}