使用
javascript,如何更改leaflet.draw“废纸篓”按钮以删除已绘制并自动保存的所有多边形.下面是我实现的代码,但它是一个完整的黑客.它删除了活动多边形,但是当我在控制台中出现错误时删除一个对象后,当我点击NotfoundError之类的“Trash”图标时:找不到Node并且TypeError:this._deletedLayers为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(); } });
解决方法
像jduhls的答案,但使用Leaflet.draw CSS类:
L.Control.RemoveAll = L.Control.extend( { options: { position: 'topleft',},onAdd: function (map) { var controlDiv = L.DomUtil.create('div','leaflet-draw-toolbar leaflet-bar'); L.DomEvent .addListener(controlDiv,'click',L.DomEvent.stopPropagation) .addListener(controlDiv,L.DomEvent.preventDefault) .addListener(controlDiv,function () { drawnItems.clearLayers(); }); var controlUI = L.DomUtil.create('a','leaflet-draw-edit-remove',controlDiv); controlUI.title = 'Remove All Polygons'; controlUI.href = '#'; return controlDiv; } }); var removeAllControl = new L.Control.RemoveAll(); map.addControl(removeAllControl);