The ArcGIS JavaScript API's RouteTask allows you to find routes between two or more locations and optionally get driving directions. The RouteTask uses ArcGIS Server network analysis services to calculate routes. Network analysis services allow you to solve simple routing problems as well as complex ones that take into account multiple stops, barriers, and time windows.
Note: The ability to expose network analysis services through REST (and therefore the ArcGIS JavaScript API) begins at ArcGIS Server version 9.3.1. Network analysis services published from prior versions of ArcGIS Server cannot be consumed using the ArcGIS JavaScript API.
To get a network analysis service, you'll need to do the following:
The above steps show that a network analysis service really originates from a map service and a map document. It's a good practice for performance to maintain the network analysis service separate from other map services in your Web application. In other words, don't put your network analysis layer in the map that will primarily be used for display.
If you're new to ESRI network analysis, the following reading will give you some more background on Network Analyst and network analysis services:
The RouteTask helps you solve a routing problem, meaning that it finds a least-cost path between multiple locations that lie on a network. Cost is calculated based on an impedance attribute which you define in a network dataset. Impedance can be time or distance, or even a more abstract variable such as "scenic value".
The RouteTask can help you find the best sequence of stops. For example, if the cost of traveling from Stop A to Stop B is higher than the opposite direction because of one-way streets, you will get a solution where Stop B is visited first. Also, if you have multiple stops that can be visited in any order, the RouteTask will find the most efficient sequence. You can optionally choose to keep the first or last stop fixed in case the route needs to start or end at a certain point.
The RouteTask can also work with time windows, which are designated intervals of time in which stops must be made (for example 3:00 - 4:00 AM for Stop A). If your stops have time windows defined, the RouteTask will find a sequence of stops where the time windows are met and the overall cost is minimized. Additionally, the RouteTask can consider barriers which block particular road segments, as well as restrictions of use such as one-way streets.
The RouteTask can return a route with its geometry, cost, turn-by-turn directions, and other accumulated values that you specify such as "walk time".