Leaflet / Leaflet.draw

Vector drawing and editing plugin for Leaflet
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html
MIT License
1.96k stars 992 forks source link

Emit event to stop drawing #1000

Closed khalld closed 3 years ago

khalld commented 3 years ago

when I draw a line using the polyne drawing I would like the "drawing" mode to be deactivated immediately as soon as I select the second point of the line (as I do in line 101 of the code). How do I make a library event propagate? And above all on what element should I have it emitted?

https://codesandbox.io/s/romantic-jepsen-7esnz

How to reproduce

khalld commented 3 years ago

Solved, hope that can be useful to someone

Trigger the second click automaticcally to complete the shape

mymap.on(L.Draw.Event.DRAWVERTEX, function (e) {

    const layerIds = Object.keys(e.layers._layers);

    if (layerIds.length > 1) {

        const secondVertex = e.layers._layers[layerIds[1]]._icon;

        requestAnimationFrame(() => secondVertex.click());
    } 
});