perliedman / leaflet-routing-machine

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

Looking for a way to dynamically add route #584

Closed vespino closed 4 years ago

vespino commented 4 years ago

I have been able to add the plugin to my project, but I was hoping to make dynamic routes instead of fixed ones. I have a number of markers which of course have lat/lng and when clicked on a link, I had hoped to fire up the plugin and add the route. Is this possible?

ElHassaniWalid commented 4 years ago

hi, can you share your code ? to how did you try to do it.

vespino commented 4 years ago

I'm using this code to insert a fixed route:

L.Routing.control({
    router: L.routing.mapbox('api-key'),
    waypoints: [
        L.latLng('some lat', 'some lng'),
        L.latLng('some lat', 'some lng')
    ],
    routeWhileDragging: true
}).addTo(map);

I was hoping to add this dynamically with variable waypoint.

perliedman commented 4 years ago

Yes, it is possible. Just just initialize the waypoints array with your coordinates, or use the setWaypoints method.

vespino commented 4 years ago

@perliedman would you mind sharing an example?