8

I'm using the Leaflet Draw control, and would like to enable/activate a particular tool (Draw Polygon) using JavaScript, so that this tool is active when the page loads. Is this possible?

enter image description here

https://stackoverflow.com/a/17144318/906814 shows how to bypass the control and start drawing directly, but I'd like instead to programatically enable the existing tool since it gives easy access to additional functionality.

9

Simply trigger a "click" event on the "Draw a polygon" toolbar button (it has a specific class "leaflet-draw-draw-polygon"):

document.querySelector(".leaflet-draw-draw-polygon").click();

Demo: http://playground-leaflet.rhcloud.com/zaze/1/edit?html,output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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