Routing is the process of finding the path from an origin to a destination in a street network. You can use the route service to find routes, get driving directions, calculate drive times, and solve complicated, multiple vehicle routing problems. To create a route, you typically define a set of stops (origin and one or more destinations) and use the service to find a route with directions. You can also use a number of additional parameters such as barriers and mode of travel to refine the results.
In this tutorial, you find a route and directions for an origin and destination by accessing the route service.
Prerequisites
This tutorial requires an ArcGIS Location Platform or ArcGIS Online account.
Steps
Create a new pen
- If you are using the CDN libraries, to get started.
Get an access token
You need an access token with the correct privileges to access the resources used in this tutorial.
-
Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges:
- Location services > Routing
- Privileges:
-
Copy the API key access token to your clipboard when prompted.
-
In CodePen, update the
access
variable to use your access token.Token Use dark colors for code blocks Copy const accessToken = "YOUR_ACCESS_TOKEN"; const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken);
To learn about the other types of authentication available, go to Types of authentication.
Make the request
Copy and paste the code below, following the steps to make a request to the Routing service.
-
Reference the ArcGIS REST JS libraries either through CDN, ES Modules, or Node JS.
-
- Set the
access
with an API key access token from API key credentials.Token
- Set the
-
Define the parameters needed for the request.
-
Call the Routing service and handle the results.
<!-- require ArcGIS REST JS libraries from https://unpkg.com -->
<script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script>
<script src="https://unpkg.com/@esri/arcgis-rest-routing@4/dist/bundled/routing.umd.js"></script>
<script>
/* when including ArcGIS REST JS all exports are available
from the same arcgisRest global */
const accessToken = "YOUR_ACCESS_TOKEN";
const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken);
arcgisRest
.solveRoute({
stops: [
[-117.195677, 34.056383],
[-117.918976, 33.812092]
],
authentication
})
.then((response) => {
console.log(response);
document.getElementById("result").textContent = JSON.stringify(response, null, 2);
});
</script>
Result
Below is the response from the service:
{
"messages": [
{
"type": 50,
"description": "Network elements with avoid-restrictions are traversed in the output (restriction attribute names: \"Through Traffic Prohibited\")."
}
],
"checksum": "m5iVAAV2igA.",
"routes": {
"fieldAliases": {
"ObjectID": "ObjectID",
"Name": "Name",
"FirstStopID": "FirstStopID",
"LastStopID": "LastStopID",
"StopCount": "StopCount",
"Total_TravelTime": "Total_TravelTime",
"Total_Miles": "Total_Miles",
"Total_Kilometers": "Total_Kilometers",
"Shape_Length": "Shape_Length"
},
"geometryType": "esriGeometryPolyline",
"spatialReference": {
"wkid": 4326,
"latestWkid": 4326
},
"features": [
{
"attributes": {
"ObjectID": 1,
"Name": "Location 1 - Location 2",
What's next?
Learn how to use additional ArcGIS location services in these tutorials:
Search for an address
Find an address or place by accessing the Geocoding service.
Find place addresses
Find coffee shops, gas stations, restaurants and other nearby places by accessing the Geocoding service.
Query a feature layer (spatial)
Access and query a hosted feature layer with a geometry and spatial operator.