2

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?

1 Answer 1

2

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.