0

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

bfeist
  • 78
  • 1
  • 8

2 Answers2

1

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

StoneFish
  • 26
  • 1
0

Found a post somewhere from almost 10 years ago with the answer.

You have to keep the drawHandler that's returned by the new L.Draw. method and call the cancel() on it.

start a draw from outside:

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

cancel the draw from outside the map:

drawHandler.disable();
bfeist
  • 78
  • 1
  • 8