0

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?

Savandy
  • 363
  • 1
  • 3
  • 14
  • Firstly, have you tried - `map._deletedLayers` ? Secondly, this answer isn't an exact duplicate but might help - https://stackoverflow.com/questions/21125543/leaflet-draw-trash-button-delete-all-polygons-and-save/25506262 – peeebeee Nov 21 '18 at 17:57
  • I think I've already tried that one with no success. I have posted my solution below. Thanks for your comment. – Savandy Nov 26 '18 at 09:19

1 Answers1

1

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');
    }
}   
Savandy
  • 363
  • 1
  • 3
  • 14