perliedman / leaflet-routing-machine

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

How to select route? #673

Closed 16bc closed 1 year ago

16bc commented 1 year ago

How to select route (to fire 'routeselected') by index? this way partly working:

control._routeSelected({route: routes[idx]});

but hides altroutes on map. routes variable got by 'routesfound' event. What is the right way?

curtisy1 commented 1 year ago

You're on the right track!

_routeSelected accepts a second event argument called alternatives that updates the alternative routes.

control._routeSelected({route: routes[idx], alternatives: routes.filter((r, i) => routes.indexOf(r) !== i)});

should work, hopefully

16bc commented 1 year ago

You're on the right track!

Thanks, i replace i to idx, and it works! control._routeSelected({route: routes[idx], alternatives: routes.filter((r, i) => routes.indexOf(r) !== idx)});