perliedman / leaflet-routing-machine

Control for routing in Leaflet
https://www.liedman.net/leaflet-routing-machine/
Other
1.06k stars 347 forks source link

Cancel marker dragging by pressing the ESCAPE key #687

Open SuperPat45 opened 1 year ago

SuperPat45 commented 1 year ago

While we drag an itinerary marker, it would be great to have ability to cancel de move by pressing the ESCAPE key. In this case, the marker return to its original position (or is deleted for new marker) and the original itinerary is redrawn.

curtisy1 commented 1 year ago

This is a tricky one and might not work with all Leaflet versions, since it's probably undefined behavior but it is possible. You will have to override the createMarker function to save markers in an intermediate array (this is so you know which marker you will be dragging).

Then you need to hook into the marker's dragstart event to save the original position (since it gets mutated on drag)

And finally, define a listener that stops dragging on keydown/keyup. I played around with it in this fiddle. As you can see, when pressing ESC while dragging, the dragend event does not get fired and so the route should not be affected.

Note that this does not incorporate LRM yet but I'm sure you can figure that part out! And if you need help, feel free to ask