I want to change the routing vehicle when clicked on a button. The default routing vehicle is "car" ("auto") when clicked a button routing vehicle should change to "foot" ("pedestrian").
I am using leaflet.easy.button and leaflet.routing.machine plugins, as routeservice I use mapzen.
Links:
- leaflet.routing.machine:
https://github.com/perliedman/leaflet-routing-machine - leaflet.easy.button:
https://github.com/CliffCloud/Leaflet.EasyButton - mapzen:
https://github.com/mapzen/lrm-mapzen
My code:
-
Routing.control :
var myRouter = L.Routing.control({ router: L.Routing.mapzen('valhalla-ABCDEFG', { costing:"auto"}), routeWhileDragging: true, geocoder: L.Control.Geocoder.nominatim(), formatter: new L.Routing.mapzenFormatter() }).addTo(map);
-
Easy.Button :
L.easyButton('<img src="pic.png">', function(btn,map){ myRouter.route({costing: "pedestrian"}); }).addTo(map);
Any ideas what could be wrong? Or maybe a different way to approach this problem.
Best Answer
The
route
method does not pass any options on to the router. You'll want to use thegetRouter
method to set the router options first. Then you can recalculate usingroute
: