Skip To Content

Routing tasks

In this topic

Version 3.7

Working with the RouteTask

The typical workflow when working with the RouteTask is as follows:

  1. Create the task
  2. Configure the parameters
  3. Solve the route
  4. Optionally, display the result

Create the task

When you create an instance of RouteTask, you provide a URL that is the REST endpoint to an ArcGIS Server network analysis service. You can copy and paste the following MXML code into your application:


The equivalent code with ActionScript:

var routeTask:RouteTask = new RouteTask

Configure the parameters

To get the desired results from the route task, you need to specify the details of your routing problem, such as stop locations, barrier locations, the impedance attribute, and so on. You do this using RouteParameters. The following code example shows how to use RouteParameters to define two stops for a route using ActionScript only. (See the Route samples at the bottom of this page for an example of using a combination of MXML and ActionScript.)

var routeParams:RouteParameters = new RouteParameters();
var stop1:Graphic = new Graphic(new MapPoint(-117.21,34.065);
var stop2:Graphic = new Graphic(new MapPoint(-117.185,34.05);
var stops:FeatureSet = new FeatureSet([ stop1, stop2]);
routeParams.stops = stops;

For a complete description of each route parameter, see the RouteParameters entry in the API reference.

Solve the route

Once you've created the route task and configured its parameters, you can call RouteTask.solve() to solve the route.


In addition to calling the solve method, you need to handle the solveComplete and fault events on the RouteTask. These functions define what the application will do with the results, whether the result is a route or an error message.


The equivalent code with ActionScript:

routeTask.addEventListener(RouteEvent.SOLVE_COMPLETE, solveCompleteHandler);
routeTask.addEventListener(FaultEvent.FAULT, faultHandler);

In either case, you'll usually create the actual functions as follows:

private function solveCompleteHandler(event:RouteEvent):void
    var routeResult:RouteResult = event.routeSolveResult.routeResults[0];

private function faultHandler(event:FaultEvent):void
{, "Routing Error");

Optimize the routes

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 and 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.

routeParams.findBestSequence = true;
//routeParams.preserveFirstStop = true;
//routeParams.preserveLastStop = true;

Turn-by-turn directions

The RouteTask can return turn-by-turn driving directions between stops. To get the directions, set RouteParameters.returnDirections to true.

routeParams.returnDirections = true;

Advanced options

  • Batch routing—Solve multiple routes in one request. Stops with the same name will be grouped into sets for solving individual routes. For the batch routing result, you'll get an array of RouteResult containing each route.
  • Time windows—Constrain stop visits to certain times of day.

    Learn more about routing with time windows

Live route samples

The following route samples are available: