Mapkit js routes more then 2 locations

In this sample code I can get the routes to 2 different locations.

var myDirections = new mapkit.Directions();
myDirections.route({
  origin: "San Francisco, CA",
  destination: "Los angeles, CA",
  transportType: mapkit.Directions.Transport.Automobile },
  function(error, data) {
  // Results are returned asynchronously via this callback function, in `data`
  }
);


How can I get the same results for say 5 or 10 locations?? I have the lat and long coordinates if that is better.

var myDirections = new mapkit.Directions();
myDirections.route({
  origin: "San Francisco, CA",
  otherLocation: "other location",
  otherLocation: "other location etc",
  destination: "Los angeles, CA",
  transportType: mapkit.Directions.Transport.Automobile },
  function(error, data) {
  // Results are returned asynchronously via this callback function, in `data`

  }
);

Accepted Reply

You can add multiple destinations as shown


var myDirections = new mapkit.Directions();
myDirections.route({
  origin: "San Francisco, CA",
  destination: "Los angeles, CA",
  destination: "Riverside, CA",
  destination: "Las Vegas, NV",
  transportType: mapkit.Directions.Transport.Automobile },
  function(error, data) {
  // Results are returned asynchronously via this callback function, in `data`
  console.table(data.routes);
  console.log(getMiles(data.routes[0].distance)+' miles');
  }
);

Replies

Your first example only has an origin and a destination. That is 1 route. There are no "otherLocations".

My question was how can I get the same results for say 5 or 10 locations?? The first code set is just a regular working example.

Can't you loop throughj destinations ?


let allDestinations: [String] = ["Los angeles, CA", Palo Alto CA"]
var myDirections = new mapkit.Directions();
for destination in allDestinations {
     myDirections.route({
       origin: "San Francisco, CA",
       destination: destination,
       transportType: mapkit.Directions.Transport.Automobile },
       function(error, data) {
       // Results are returned asynchronously via this callback function, in `data`
       }
     )
}


Note: maybe you have to make myDirections an Array…

No this would only give directions from the starting point to each individual point along the route.

You can add multiple destinations as shown


var myDirections = new mapkit.Directions();
myDirections.route({
  origin: "San Francisco, CA",
  destination: "Los angeles, CA",
  destination: "Riverside, CA",
  destination: "Las Vegas, NV",
  transportType: mapkit.Directions.Transport.Automobile },
  function(error, data) {
  // Results are returned asynchronously via this callback function, in `data`
  console.table(data.routes);
  console.log(getMiles(data.routes[0].distance)+' miles');
  }
);

I thought that was your spec.

o you wanted a sequence of consecutive destination ?


Seems you found the solution. Great.