2016-06-28 77 views
0

如何删除装载了loadGeoJson的谷歌地图的数据图层上的顶点(多边形点)?删除Google地图DataLayer顶点

http://output.jsbin.com/tuyived

我想删除右键单击白色圆圈

+0

其实我想要实现这个https://developers.google.com/maps/documentation/javascript/examples/delete-vertex-menu在数据层https://开头developers.google.com/maps/documentation/javascript/datalayer#overview –

回答

4

你可以用下面的代码片段删除所有(例如,将L从谷歌的左上角)一个顶点:

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

参考:Remove all features from data layer

编辑:

如果要删除刚刚点击的顶点是比较麻烦一些,但我可以通过以下点击处理程序来完成:

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

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

希望这一次帮助;)

+0

也许我的问题不完整。请检查更新后的问题。 –

+0

编辑答案匹配更新的问题 –

+0

很好的答案。谢谢! –