[GIS] Revert a Leaflet.draw edit event

leaflet-draw

I'm trying to prevent a user in edit mode from creating overlapping shapes. With a CREATED event, I can easily remove the layer. But I want to revert an edited shape back to its original position instead of removing the layer.

My code is as follows:

map.on(L.Draw.Event.EDITED, function (e) {
   var layers = e.layers;
   layers.eachLayer(function (layer) {

     var reversedCoordinates = [], latlngs = layer.getLatLngs();
     for (var i = 0; i < latlngs.length; i++)
         reversedCoordinates.push([latlngs[i].lng, latlngs[i].lat]);

     reversedCoordinates.push([reversedCoordinates[0][0], reversedCoordinates[0][1]]);
     if (checkRectangleOverlap(reversedCoordinates)) {

         //drawnItems.removeLayer(layer);                        
        //revert the layer to its original position here
      };
    });
 });

Could anybody help me in this regard?

Best Answer

One way to do it is to trigger the edit toolbar's "cancel" button functionality. However, this will also revert any other edits made since you opened the toolbar.

You should already have a reference to the draw control:

var drawControl = new L.Control.Draw()

The draw control contains toolbars keyed by name:

drawControl._toolbars['edit']

A toolbar's disable() method will cancel the current tool's action, revert any edits, and close the toolbar.

drawControl._toolbars['edit'].disable();
Related Question