Multiple Routes in RouteTask

967
5
Jump to solution
01-23-2014 02:56 PM
WesleyAskew
New Contributor III
I have a route task that is calculating multiple routes simultaneously.   When the user clicks on a map, a stop is added and draws different routes for up to seven different locations.  I have assigned every stop a route name and each route is drawing properly to the appropriate stops.  I need to return the total length of each individual route as a variable for input into a function and was wondering what would be the best way of going about this.  I can successfully alert box the lengths of each route by using: var routeInfo = solveResult.routeResults[0].directions.totalLength; alert(JSON.stringify(routeInfo)); However, the order that each alert message displays starts with the shortest distance routes first.  How do I need to go about identifying which distance is associated with each route?  Thanks in advance.
0 Kudos
1 Solution

Accepted Solutions
RobertoPepato
Occasional Contributor II
Thanks for your response.  Yes, I have tried to use this property, however, I cannot seem to get the properties associated with each individual route by name.  I only specify the routeName value for the stops, and am not really sure how this associates with each route segment.  Each route is solved using the following:

routeTask.solve(routeParams);
map.graphics.add(solveResult.routeResults[0].route.setSymbol(routeSymbol));

I can also display the route name and distance in the same alert box using:
var routeLength = solveResult.routeResults[0].directions.totalLength;
var routeName =  solveResult.routeResults[0].routeName;
var routeNameLength = routeName +" "+routeLength;

Do you have any suggestions on how to correspond the name of each route to the length as far as returning a variable is concerned?


You can use a Javascript object in a hastable-like manner. For exemple, inside a loop for your routeResults you could use:

routes[solveResult.routeResults[index].routeName] = solveResult.routeResults[0].directions.totalLength


In the previous sample, index is the for loop indexer and you need to previously declare de routes object (before the loop):

var routes = {};


After that, you can get the total length of a specific route by using its name as an indexer for the routes object:

var distance = routes["your_route_name"];

View solution in original post

0 Kudos
5 Replies
RobertoPepato
Occasional Contributor II
I have a route task that is calculating multiple routes simultaneously.   When the user clicks on a map, a stop is added and draws different routes for up to seven different locations.  I have assigned every stop a route name and each route is drawing properly to the appropriate stops.  I need to return the total length of each individual route as a variable for input into a function and was wondering what would be the best way of going about this.  I can successfully alert box the lengths of each route by using: var routeInfo = solveResult.routeResults[0].directions.totalLength; alert(JSON.stringify(routeInfo)); However, the order that each alert message displays starts with the shortest distance routes first.  How do I need to go about identifying which distance is associated with each route?  Thanks in advance.


Have you consider to use the routeName property of each RouteResult instance?
0 Kudos
WesleyAskew
New Contributor III
Have you consider to use the routeName property of each RouteResult instance?


Thanks for your response.  Yes, I have tried to use this property, however, I cannot seem to get the properties associated with each individual route by name.  I only specify the routeName value for the stops, and am not really sure how this associates with each route segment.  Each route is solved using the following:

routeTask.solve(routeParams);
map.graphics.add(solveResult.routeResults[0].route.setSymbol(routeSymbol));

I can also display the route name and distance in the same alert box using:
var routeLength = solveResult.routeResults[0].directions.totalLength;
var routeName =  solveResult.routeResults[0].routeName;
var routeNameLength = routeName +" "+routeLength;

Do you have any suggestions on how to correspond the name of each route to the length as far as returning a variable is concerned?
0 Kudos
RobertoPepato
Occasional Contributor II
Thanks for your response.  Yes, I have tried to use this property, however, I cannot seem to get the properties associated with each individual route by name.  I only specify the routeName value for the stops, and am not really sure how this associates with each route segment.  Each route is solved using the following:

routeTask.solve(routeParams);
map.graphics.add(solveResult.routeResults[0].route.setSymbol(routeSymbol));

I can also display the route name and distance in the same alert box using:
var routeLength = solveResult.routeResults[0].directions.totalLength;
var routeName =  solveResult.routeResults[0].routeName;
var routeNameLength = routeName +" "+routeLength;

Do you have any suggestions on how to correspond the name of each route to the length as far as returning a variable is concerned?


You can use a Javascript object in a hastable-like manner. For exemple, inside a loop for your routeResults you could use:

routes[solveResult.routeResults[index].routeName] = solveResult.routeResults[0].directions.totalLength


In the previous sample, index is the for loop indexer and you need to previously declare de routes object (before the loop):

var routes = {};


After that, you can get the total length of a specific route by using its name as an indexer for the routes object:

var distance = routes["your_route_name"];
0 Kudos
WesleyAskew
New Contributor III
You can use a Javascript object in a hastable-like manner. For exemple, inside a loop for your routeResults you could use:

routes[solveResult.routeResults[index].routeName] = solveResult.routeResults[0].directions.totalLength


In the previous sample, index is the for loop indexer and you need to previously declare de routes object (before the loop):

var routes = {};


After that, you can get the total length of a specific route by using its name as an indexer for the routes object:

var distance = routes["your_route_name"];


This method worked like a charm.  Thanks alot for your help!
0 Kudos
RobertoPepato
Occasional Contributor II
This method worked like a charm.  Thanks alot for your help!


Good to hear Wesley. It would be nice if you could please mark the reply that solved your problem as the correct solution and close the post so other people on the forum can benefit from the solution.

Regards.
0 Kudos