【问题标题】:How do you remove a vertex from a polygon with multiple paths?如何从具有多条路径的多边形中删除顶点?
【发布时间】:2019-05-30 18:18:11
【问题描述】:

在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 指向正确的路径,那将是我的一天。

【问题讨论】:

  • this.getPath() 返回第一个路径。使用this.getPaths()(带有's')获取所有路径。

标签: google-maps-api-3 polygon


【解决方案1】:

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);
            }
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多