vla*_*aru 1 javascript google-maps
如何删除加载了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);
});
Run Code Online (Sandbox Code Playgroud)
参考:从数据层中删除所有功能
编辑:
如果你只想删除点击的顶点,它有点棘手,但我可以通过以下点击处理程序完成:
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);
});
Run Code Online (Sandbox Code Playgroud)
您可能需要根据您的需要/数据结构进行调整.它适用于提供的结构.
希望这次有所帮助;)