删除谷歌地图的DataLayer顶点

Delete google maps DataLayer vertex

本文关键字:顶点 DataLayer 谷歌地图 删除      更新时间:2023-09-26

我如何删除一个顶点(多边形点)在谷歌地图加载loadGeoJson的数据层?

http://output.jsbin.com/tuyived

我想通过右键单击白色圆圈来删除单个顶点(例如,来自googLe的L的左上角)

你可以用下面的代码片段删除所有的

map.data.forEach(function(feature) {
    //If you want, check here for some constraints.
    map.data.remove(feature);
});

参考:删除数据层的所有特征

编辑:

如果你想只删除点击的顶点,这有点棘手,但我可以通过以下点击处理程序来完成:

map.data.addListener('click', function(ev) {
    //array to hold all LatLng in the new polygon, except the clicked one
    var newPolyPoints = [];
    //iterating over LatLng in features geometry
    ev.feature.getGeometry().forEachLatLng(function(latlng) {
        //excluding the clicked one
        if (latlng.lat() == ev.latLng.lat() && latlng.lng() == ev.latLng.lng()) {
            console.log('This one will be removed: lat: ' + latlng.lat() + ', lng: ' + latlng.lng());
        } else {
            //keeping not matching LatLng
            newPolyPoints.push(latlng);
        }
    });
    //creating new linear ring
    var newLinearRing = new google.maps.Data.LinearRing(newPolyPoints);
    //creating a new polygon out of the new linear ring
    var newPoly = new google.maps.Data.Polygon([newLinearRing]);
    //apply the new polygon to the clicked feature
    ev.feature.setGeometry(newPoly);
});

你可能需要根据你的需要/你的数据结构来调整它。它适用于所提供的结构。

希望这次能有所帮助;)