The RouteTask can help you find the most efficient path for visiting a given list of stops. This is sometimes known as the "traveling salesperson" problem. To solve it, set RouteParameters.findBestSequence to true. Instead of being visited sequentially, the stops will be visited in the most efficient order. You can optionally designate whether the first and last stops on the list should remain fixed.
Suppose you've specified three stops, like this:
var stop1 = new ESRI.ArcGIS.VE.GraphicFeature();
var stop2 = new ESRI.ArcGIS.VE.GraphicFeature();
var stop3 = new ESRI.ArcGIS.VE.GraphicFeature();
stop1.Geometry = { "x": -117.21, "y": 34.065 };
stop2.Geometry = { "x": -117.185, "y": 34.05 };
stop3.Geometry = { "x": -117.19, "y": 34.062 };
stopSet = new ESRI.ArcGIS.VE.FeatureSet();
stopSet.Features = new Array();
stopSet.Features.push(stop1);
stopSet.Features.push(stop2);
stopSet.Features.push(stop3);
routeParams.Stops = stopSet;
If you want these stops to be visited in the most efficient way, you will need to specify the following parameters:
routeParams.FindBestSequence = true; routeParams.PreserveFirstStop = false; routeParams.PreserveLastStop = false; routeParams.ReturnStops = true;
When you get the results from the solve operation, examine the sequence values on the returned stops. Here's how you could report the sequence programmatically:
function responseHandler(response) {
var result = response.Results[0];
for (var i = 0; i < result.Stops.length; i++) {
alert("Stop " + result.Stops[i].Attributes.Name
+ " visited " + result.Stops[i].Attributes.Sequence);
}
}
In the above code, the alerts report the most efficient sequence of stops. If you change the code so FindBestSequence = false, you'll notice a different sequence because each stop will be visited in the order it was added.