For anyone experienced with leaflet or leaflet.draw plugin:
I want to initiate drawing a polygon without using the toolbar from leaflet.draw
. I've managed to find the property that allows editing without using the toolbar (layer.editing.enable();
) by searching online (it's not in the main documentation). I can't seem to find how to begin drawing a polygon without the toolbar button. Any help would be much appreciated!
Thank you :)
This simple code works for me:
Just put it into the onclick handler of your custom button (or wherever you want).
The variables
map
anddrawControl
are references to your leaflet map and draw control.Diving into the source code (leaflet.draw-src.js) you can find the functions to draw the other elements and to edit or delete them.
I hope this will be useful for you too.
EDIT: The
L.EditToolbar.Edit
andL.EditToolbar.Delete
classes expose the following useful methods:So I've figured this out for circles, but it should be the same for polygons. It's actually really simple. Hopefully the following code answers your question, but if not let me know and I can post more to a gist or something.
Sorry a lot of that is just noise, but it should give you an idea of how to go about this. You can control editing like you said with editing.enable()/.disable().
Make sure to comment with any questions. Good luck man.
I think it's worth mentioning Jacob Toyes answer to this problem. You're always drawing with handlers in leaflet.draw - not directly with layers. If you want to edit a layer, you use the handler saved in a layers
editing
field like that:layer.editing.enable();
. And if you want to create a new layer, you first create a new handler:By now there actually is an example on the leaflet.draw github page: https://github.com/Leaflet/Leaflet.draw/blob/master/examples/edithandlers.html
Nevertheless I think handlers aren't well documented there yet.
Like stated above,
L.EditToolbar.Edit
andL.EditToolbar.Delete
expose interesting methods and events like editstart and editstop. What's not mentioned is that these two classes themselves are derived fromL.Handler
.