2
votes

I am working on a application and I want to get directions for more than 2 points

Which I assume other than origin and destination other locations are waypoints.

So I am trying to use directions.setWaypoint() to add waypoints to the map but it doesn't work

please help for me to understand what I do wrong.

I have created a JsFiddle https://jsfiddle.net/3uzm1nh0/1/

and this is the documentation I am referring to https://github.com/mapbox/mapbox-directions.js/blob/mb-pages/API.md

Thanks in advance.

1
If you want to get directions for more than 2 points, wouldn't it be better to just call the API more than once? I'm not sure if the waypoints will help you if you need to get direction from A to B and from C to D? Maybe you can explain better what you're trying to achieve?kmandov
Hi @kmandov, I actually want to do is make a map so that users can type in names of locations by order of they want to visit and get the directions for those locations in a single map. its kind of a trip planer. for example in google we can get directions in between 2 locations and we can add more destinations to the map and it will show how to get to paint A,B,C... same thing I want to do with mapbox maps directions.js. please let me know if anything unclear. appreciate your helpTharindu

1 Answers

1
votes

You need someting like this

 // = L.mapbox.directions({profile: 'mapbox.driving'})
var directions = L.mapbox.directions();
var directionsLayer = L.mapbox.directions.layer(directions).addTo(map);
var directionsRoutesControl = L.mapbox.directions.routesControl('routes', directions).addTo(map);
directions.setOrigin(L.latLng(14.6059596413528, -90.49169592683657));

directions.addWaypoint(0,L.latLng(14.60026436463006, -90.49669902226937));
directions.addWaypoint(1,L.latLng(14.59689160135752, -90.49520561914318));
directions.addWaypoint(2,L.latLng(14.60036292858185, -90.49586222238077));

directions.setDestination(L.latLng(14.6059596413528, -90.49169592683657));
directions.query();