Javascript 删除谷歌地图数据层顶点
如何删除加载了loadGeoJson的google maps数据层上的顶点多边形点Javascript 删除谷歌地图数据层顶点,javascript,google-maps,Javascript,Google Maps,如何删除加载了loadGeoJson的google maps数据层上的顶点多边形点 我想通过右键单击白色圆圈来删除单个顶点,例如,googLe中L的左上角。您可以使用以下代码段删除所有顶点: map.data.forEach(function(feature) { //If you want, check here for some constraints. map.data.remove(feature); }); 参考: 编辑: 如果您只想删除单击的顶点,这有点棘手,但我可
我想通过右键单击白色圆圈来删除单个顶点,例如,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);
});
您可能需要根据您的需要/数据结构对其进行调整。它适用于提供的结构
希望这次能有所帮助 您可以使用以下代码段删除所有内容:
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);
});
您可能需要根据您的需要/数据结构对其进行调整。它适用于提供的结构
希望这次能有所帮助 也许我的问题不完整。请检查更新的问题。可能我的问题不完整。请检查更新的问题。实际上我想在数据层上实现这一点。实际上我想在数据层上实现这一点