Hide Table of Contents
esri
Map
toolbars
Class: RouteTask
[ AMD Module Require | Legacy Module Require ]

Module Require

require(["esri/tasks/RouteTask", ... ], function(RouteTask, ... ){ ... });

Description

Class added 1.4
Solves a route on a route layer resource in a Network Analyst service exposed by the ArcGIS Server REST API. For more information on using RouteTask, see An overview of routing with the ArcGIS JavaScript API and Working with the RouteTask.

Samples

Search for samples that use this class.

Class hierarchy

esri.tasks.RouteTask

Constructors

ConstructorDescription
new RouteTask(url)Creates a new RouteTask object.

Properties

PropertyTypeDescription
urlStringURL to the ArcGIS Server REST resource that represents a network analysis service. To obtain the URL, use Services Directory.

Methods

MethodReturn ValueDescription
solve(params,callback?,errback?)dojo.DeferredSolves the route against the route layer with the route parameters.

Events

[ On Style Events | Connect Style Events ]

All On Style event listeners receive a single event object. The event properties listed below are accessed from this event object. Additionally, the event object also contains a 'target' property whose value is the object which fired the event.
On EventEvent PropertiesDescription
error<Error> errorFires when an error occurs when executing the task. Should be used in favor of onError.
solve-complete<Object> resultFires when RouteTask.solve() has completed. Should be used in favor of onSolveComplete
Constructor Details

new RouteTask(url)

Creates a new RouteTask object.
Parameters:
<String> urlRequiredURL to the ArcGIS Server REST resource that represents a network analysis service. To obtain the URL, use Services Directory.
Code Snippet:
require([
  "esri/tasks/RouteTask", ... 
], function(RouteTask, ... ) {
  var routeTask = new RouteTask("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route");
  ...
});
Property Details

<String> url

URL to the ArcGIS Server REST resource that represents a network analysis service. To obtain the URL, use Services Directory.
Method Details

solve(params,callback?,errback?)

Solves the route against the route layer with the route parameters.
Return Value: dojo.Deferred
Input Parameters:
<RouteParameters> paramsRequiredRoute parameters used as input to generate the route.
<Function> callbackOptionalThe function to call when the method has completed. The arguments in the function are the same as the onSolveComplete event.
<Function> errbackOptionalAn error object is returned if an error occurs during task execution.
Code Snippet:
require([
  "esri/tasks/RouteParameters", "esri/tasks/FeatureSet", "esri/SpatialReference",
  "esri/units", "dojo/_base/connect", ... 
], function(RouteParameters, FeatureSet, SpatialReference, Units, connect, ... ) {
  var routeParams = new RouteParameters();
  routeParams.stops = new FeatureSet();
  routeParams.returnRoutes = false;
  routeParams.returnDirections = true;
  routeParams.directionsLengthUnits = Units.MILES;
  routeParams.outSpatialReference = new SpatialReference({ wkid:102100 });
  
  connect.connect(routeTask, "onSolveComplete", showRoute);
  ...
});
See Also: onSolveComplete
Event Details

[ On Style Events | Connect Style Events ]

error

Fires when an error occurs when executing the task. Should be used in favor of onError. (Added at v3.5)
Event Properties:
<Error> errorError message returned in a JavaScript error object.

solve-complete

Fires when RouteTask.solve() has completed. Should be used in favor of onSolveComplete (Added at v3.5)
Event Properties:
<Object> result

At version 2.0 the signature changed to return an anonymous solveResult object. The solveResult object contains the following properties:

<RouteResult[]> routeResults Array of route results.
<Graphic[]> barriers Array of graphics representing the barriers. Barriers are returned only if RouteParameters.returnBarriers is true. For the list of attributes returned for each barrier, see the "Barrier properties" section in Finding the best route.
<Graphic[]> polygonBarriers Array of graphics representing the polygon barriers. Barriers are returned only if RouteParameters.returnPolygonBarriers is true.
<Graphic[]> polylineBarriers Array of graphics representing the polyline barriers. Barriers are returned only if RouteParameters.returnPolylineBarriers is true.
<NAMessage[]> message Message received when solve is completed. If a route cannot be solved, the message returned by the server identifies the route that could not be solved.
Code Snippet:
function showRoute(evt) {
  var solveResult = evt.result;
  var routeResults = solveResult.routeResults;
  var barriers = solveResult.barriers;
  var polygonBarriers = solveResult.polygonBarriers;
  var polylineBarriers = solveResult.polylineBarriers;
  var messages = solveResult.messages;
  …
}