perliedman / leaflet-routing-machine

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

Is it possible to edit routes once they have been found? #693

Open benzene29 opened 9 months ago

benzene29 commented 9 months ago

Hi, I am currently working on a university project where we are creating an application that includes routing as one of its features. I was wondering if it is possible to access the array of coordinates inside the selected route and then selectively change them?

Any help would be much appreciated, thank you.

peter1599 commented 9 months ago

Also curios if its possible in some way. Tried with _selectedRoute but sadly not working.

I really need to change the route manually.

curtisy1 commented 9 months ago

Apologies for the late reply @benzene29 and @peter1599

That's an excellent question and I don't think it's currently possible with the built-in functionality of LRM.

However, as @peter1599 said, _selectedRoute should work, in theory, as long as you set it again after (since Leaflet doesn't support on the fly updates, so we need to trigger an update internally).

This is done by the _updateLine function, which in turn gets called when either a route is selected, or alternatives are set. As such, you can update the route by retriggering the _routeSelected or setAlternative functions.

For a working example, see this fiddle (note, you need to press the button in the bottom left corner twice for it to work)