leaflet.draw垃圾按钮删除所有多边形并保存 [英] leaflet.draw trash button delete all polygons and save
问题描述
使用javascript,如何更改leaflet.draw废纸篓按钮以删除已绘制并自动保存的所有多边形。下面是我实现的代码,但它是一个完整的黑客。它删除了活动多边形,但是当我在控制台中出现错误后,当我点击垃圾图标(例如 NotFoundError:未找到节点
)时删除对象后 TypeError:this._deletedLayers为null
Using javascript, how can I alter the leaflet.draw "Trash" button to delete all polygons that have been drawn and automatically save. Below is the code I've implemented but it is a complete hack. It removes the active polygon, but after I delete an object once I begin to get errors in the console when I click the "Trash" icon like NotFoundError: Node was not found
and TypeError: this._deletedLayers is null
map.on('draw:editstart', function (e) {
if(e.handler == 'remove' && typeof drawnItem != 'undefined' && drawnItem !== null){
if(window.console) window.console.log('Drawing deleted...');
if(typeof drawnItem != 'undefined' && drawnItem !== null){
drawnItems.removeLayer(drawnItem);
}
$('.leaflet-draw.leaflet-control .leaflet-draw-actions').hide();
$('.leaflet-popup-pane .leaflet-draw-tooltip').remove();
}
});
推荐答案
通过自定义控件解决了我自己的问题(感谢stackexchange - https://gis.stackexchange.com/questions/60576/custom-leaflet-controls):
Solved my own problem with a custom control (thanks to stackexchange - https://gis.stackexchange.com/questions/60576/custom-leaflet-controls):
更新!添加了@SpiderWan建议(谢谢!)所以不需要自定义CSS。此外,事件先前已附加到整个控制栏。而只是附加到controlUI按钮本身。
UPDATED! added @SpiderWan suggestions (thanks!) so no need for custom CSS. Also, the event was previously attached to the entire control bar. Instead attached to just the controlUI button itself.
Javascript:
Javascript:
L.Control.RemoveAll = L.Control.extend({
options: {
position: 'topleft',
},
onAdd: function (map) {
var controlDiv = L.DomUtil.create('div', 'leaflet-control leaflet-bar');
var controlUI = L.DomUtil.create('a', 'leaflet-draw-edit-remove', controlDiv);
controlUI.title = 'Remove all drawn items';
controlUI.setAttribute('href', '#');
L.DomEvent
.addListener(controlUI, 'click', L.DomEvent.stopPropagation)
.addListener(controlUI, 'click', L.DomEvent.preventDefault)
.addListener(controlUI, 'click', function () {
drawnItems.clearLayers();
if(window.console) window.console.log('Drawings deleted...');
});
return controlDiv;
}
});
removeAllControl = new L.Control.RemoveAll();
map.addControl(removeAllControl);
这篇关于leaflet.draw垃圾按钮删除所有多边形并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!