如何从具有多个路径的多边形中删除顶点?

时间:2019-05-30 18:18:11

标签: google-maps-api-3 polygon

在googlemaps api中,当编辑具有多个路径的多边形时,removeAT会消除错误的顶点

对于简单的多边形,removeAT会删除正确的顶点,但是对于多路径多边形,似乎只从第一条路径中删除了顶点编号。

给出用于多个路径的多边形的定义:

var blockpolygon = newgoogle.maps.Polygon({
paths: [blockcoords0,blockcoords1,blockcoords2,blockcoords3,blockcoords4,blockcoords5]
});

先前已定义路径的位置,例如:

var blockcoords4 = [{lat:51.799693211411,lng:-114.12380330669},{lat:51.799109509173,lng:-114.12273800578},{lat:51.799558197929,lng:-114.1223323167},{lat:51.799684004911,lng:-114.12232429316},{lat:51.799876802912,lng:-114.12248608283},{lat:51.800102904916,lng:-114.12290678386},{lat:51.800133809341,lng:-114.12306439938},{lat:51.800077007986,lng:-114.12331471639},{lat: 51.799693211411, lng: -114.12380330669}];

并且多边形被设置为可编辑

此事件触发时:

blockpolygon.addListener("rightclick", function(event)
{
this.getPath().removeAt(event.vertex);
}

列表中第一个路径的顶点被删除,而不是被“单击”的顶点。

有什么方法可以从正确的路径中移除顶点?

如果我可以确定哪条路径并让removeAT指向正确的路径,那将是我的工作。

1 个答案:

答案 0 :(得分:0)

来自geocodezip的提示就足够了。解决方法如下:

blockpolygon.addListener("rightclick", function(event)
    {
    for (i=0; i<this.getPaths().getLength(); i++)
        {
        for (j=0; j< this.getPaths().getAt(i).getLength(); j++)
            {
            var distance = google.maps.geometry.spherical.computeDistanceBetween(event.latLng, this.getPaths().getAt(i).getAt(j));
            if (distance==0) this.getPaths().getAt(i).removeAt(j);
            }
        }
}