perliedman/leaflet-routing-machine

Cancel marker dragging by pressing the ESCAPE key

SuperPat45 opened this issue · 1 comments

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.

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