javascriptleafletleaflet-draw

Remove multiple layers on leaflet draw


I use leaflet draw for drawing polygons on a map. At the moment, I'm only able to delete the clicked polygons.

This is an example of what I have right now: http://leaflet.github.io/Leaflet.draw/docs/examples-0.7.x/full.html

Let's say that you draw 3 polygons. If you want to delete the first 2, you have to click on the trash icon, click on the first 2, and click save. What I want to achieve is to not have to click on the second one. I actually have the id of the second but I can't manage to add it to the removingLayers array that keeps the clicked layers in.

What I have atm:

function deleteSubPolygons(e) {
        var layersToRemove = [];
        if (e.layer && e.layer._originalPoints != null && e.layer._latlngs != null && e.layer.id != null && Number.isInteger(e.layer.id)) {         
            var polygonChildren = getPoligonChildren(e.layer.id);
            for (var l in map._layers) {
                if (polygonChildren.indexOf(map._layers[l].id) > -1) {
                    polygonsToDelete.push(map._layers[l].id);
                    layersToRemove.push(map._layers[l]);
                }
            }
            for (var i = 0; i < layersToRemove.length; i++) {
                map.removeLayer(layersToRemove[i]);
            }       
        }   
    }

I managed to remove them from the view manually, but the revert option doesn't work well anymore.I'm pretty sure I should just push the layer I want to remove to a list of leaflet-draw.js as they are doing:

this._deletableLayers.removeLayer(e),
this._deletedLayers.addLayer(e)

My question would be: How can I acces the _deletedLayers from outside?


Solution

  • I solved this by firing the click event on the polygons I want to delete when I'm in delete mode:

    for (var l in map._layers) {
        if (polygonChildren.indexOf(map._layers[l].id) > -1) {
            map._layers[l].fireEvent('click');
        }
    }