【发布时间】:2016-06-28 14:47:55
【问题描述】:
如何删除加载了 loadGeoJson 的谷歌地图数据层上的顶点(多边形点)?
http://output.jsbin.com/tuyived
我想通过右键单击白色圆圈来删除单个顶点(例如,来自googLe的L的左上角)
【问题讨论】:
如何删除加载了 loadGeoJson 的谷歌地图数据层上的顶点(多边形点)?
http://output.jsbin.com/tuyived
我想通过右键单击白色圆圈来删除单个顶点(例如,来自googLe的L的左上角)
【问题讨论】:
您可以使用以下 sn-p 删除所有内容:
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);
});
您可能需要根据您的需要/您的数据结构对其进行调整。它适用于提供的结构。
希望这次能有所帮助;)
【讨论】: