leaflet.draw trash button delete all polygons and save
javascript, leaflet, leaflet.draw
Solution
Solved my own problem with a custom control (thanks to stackexchange - https://gis.stackexchange.com/questions/60576/custom-leaflet-controls):
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:
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);
Problem
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(); } }); ```