Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stop edit and remove marker/line/polygon? #29

Open
robertd opened this issue Aug 19, 2015 · 11 comments
Open

How to stop edit and remove marker/line/polygon? #29

robertd opened this issue Aug 19, 2015 · 11 comments

Comments

@robertd
Copy link

robertd commented Aug 19, 2015

What's the best way to cancel out of current edit and remove any marker/line/polygon created? I've tried map.editTools.stopDrawing(); but that doesn't remove markers it just stops editing.

edit:

I guess my question is how to stop the workflow and clear uncommitted edits...

@yohanboniface
Copy link
Member

Humm, you have more than one option. It depends how you use the API.
Basically, I'd say calling cancelDrawing sounds good, then listening for editable:drawing:cancel, and doing someting like e.layer.remove() in the callback.

Does that help?

@robertd
Copy link
Author

robertd commented Aug 24, 2015

I can't seem to find map.editTools.cancelDrawing()... would you mind providing a github gist?

Thanks

@yohanboniface
Copy link
Member

Sorry, I meant stopDrawing just like you've done, and then listening for editable:drawing:cancel event.

@robertd
Copy link
Author

robertd commented Aug 24, 2015

Hmm... I've tried that too, but e.layer.remove() doesn't exist.

@yohanboniface
Copy link
Member

humm, yeah, sorry, it depends which version of Leaflet your are using, e.layer.remove is only on latest beta.
On 0.7.3, you need to do something like e.layer.removeFrom(e.editTools.editLayer). Note that it's just pure Leaflet here, no Editable specifics.

@etrochim
Copy link

etrochim commented Dec 7, 2015

I'm having a similar issue. Your suggestion didn't work for me because e.layer doesn't have a removeFrom method. However, I changed it so that my editable:drawing:cancel handler calls map.removeLayer(e.layer). If the user is in the middle of drawing a new polygon this will allow the user to cancel the operation and remove their current edit.

However, this doesn't work if the user is drawing a new hole in a polygon. This method will remove the polygon that the new hole is a part of. Is there a way to cancel the drawing of a new polygon?

I am using Leaflet.Editable 0.6.2 and Leaflet 0.7.7.

@adamtomat
Copy link

I was having a similar issue, and none of the suggestions worked for me. The solution from @etrochim got me close, but I was getting the editable:drawing:cancel event fired twice.

The solution for me was to listen for the editable:drawing:end event instead, as this is fired after editable:drawing:cancel.

This is because map.removeLayer(e.layer) in the callback for editable:drawing:cancel was firing off a remove event which in turn causes L.Editable.BaseEditor to call its disable function. This disable call will cancel the drawing and fire off editable:drawing:cancel for the same layer & will try and delete a layer that doesn't exist anymore.

Why does this fix work? At the point editable:drawing:cancel is called, Editable thinks it is drawing (this.drawing = true). It will wait for all event listeners to complete before stopping drawing. The problem is that calling map.removeLayer(e.layer) triggers another editable:drawing:cancel event (and therefore calls our listener again). During both calls, Editable still thinks it is drawing (this.drawing = true) and will try and clear layers that don't exist.

By listening to editable:drawing:end instead, after the first execution Editable knows it is not drawing anymore (this.drawing = false), so when it gets called a 2nd time it doesn't try and remove any layers as it knows they don't exits.

Note: I'm also using Leaflet.Editable 0.6.2 and Leaflet 0.7.7.

@chocho11
Copy link

chocho11 commented Oct 18, 2017

from click event, for markers , "e.target.remove()" works

@srinathS-tvsnext
Copy link

hai i am currently remove the polygons in the map but i cant able to remove the markers in the polygons

@Nightwelf
Copy link

hai i am currently remove the polygons in the map but i cant able to remove the markers in the polygons

how you do it? please help!

@ardicoetzee
Copy link

ardicoetzee commented Apr 26, 2023

Prob not the best way to do it, but using a global variable worked for me:

var ACTIVE_LAYER = null; LEAFLET_MAP.on('editable:drawing:commit', function (event) { ACTIVE_LAYER = event.layer; });

And then in a button:

function cancelDrawing() { ACTIVE_LAYER.remove(); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants