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
You need an ArcGIS Developer or ArcGIS Online account to access the developer dashboard and create an API key.
Steps
Create a new pen
- If you are using the CDN libraries, to get started.
Get an API Key
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
- Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
- In CodePen, update
api
to use your key.Key
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
api
with the API key from your dashboard.Key -
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.0.0/dist/bundled/request.umd.js"></script>
<script src="https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js"></script>
<script>
/* when including ArcGIS REST JS all exports are available
from the same arcgisRest global */
const apiKey = "YOUR_API_KEY";
const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
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.