leafletleaflet-draw

Leaflet Draw how to trigger button events from outside the map


I've got a leaflet draw instance where I need buttons outside the map to start / cancel draw actions. I have successfully triggered draw start with

map.Draw.Polyline(map, myDrawControlInstance.options.polyline).enable();

but the only action I could find anywhere in the API that is mentioned to cancel a draw action: map.Draw.Polyline(map, myDrawControlInstance.options.polyline).disable(); does not trigger the cancel action.

How can I mimic all buttons that appear in the Leaflet Draw interface when drawing, editing, deleting, etc.

Any help is greatly appreciated


Solution

  • There is an error in the answer. You need to store the draw handler before doing enable.

    new L.Draw.Polyline(map, drawControl.options.polyline).enable();
    

    returns undefined

    The correct implementation would be:

    const drawHandler = new L.Draw.Polyline(map, drawControl.options.polyline);
    drawHandler.enable();
    //After drawing
    drawHandler.disable();
    

    Here is a github issue referencing a similar issue: https://github.com/Leaflet/Leaflet.draw/issues/370