Learn how to find a route and directions with the
Click on the map twice to create a route and get directions.
Routing is finding the path from an
In this tutorial, you will define an
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or
.
Get an access token
You need an
- Go to the Create an API key tutorial and create an
API key with the followingprivilege(s) :
- Privileges
- Location services > Basemaps
- Location services > Geocoding
- Location services > Routing
- Privileges
- In CodePen, set the
apiKeyproperty on the globalesriConfigvariable to your access token.var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};
To learn about other ways to get an access token, go to Types of authentication.
Update the map
A streets basemap attribute on the Map component to use the arcgis/navigation basemap layer and change the position of the map to center on Los Angeles.
- Update the
basemap,centerandzoomattributes.30 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map>4 collapsed lines</body></html>
Add HTML to display directions
-
Add a
calcite-panelto thetop-rightslot of the map.34 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"></calcite-panel>6 collapsed lines</arcgis-map></body></html> -
Inside the
calcite-panel, add acalcite-noticethat displays the instructions.34 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice></calcite-panel>6 collapsed lines</arcgis-map></body></html> -
Add a
divelement where the directions will be displayed.34 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel>6 collapsed lines</arcgis-map></body></html> -
Add some CSS styling to set the size of the directions container
divandcalcite-notice.16 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style>24 collapsed lines</head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map></body></html>
Add modules
-
Use
$arcgis.importto add the necessary modules in a new<script>at the bottom of the<body>. For more information, refer to theGraphic,route,RouteParameters,FeatureSet,SimpleLineSymbolandSimpleMarkerSymbolmodules.The
ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK’s different modules, visit the References page.57 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);</script>4 collapsed lines</body></html>
Define the service url
The rest module makes a request to a route module to access the
-
Create a variable called
routeUrlto reference the route service.57 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";</script>4 collapsed lines</body></html>
Get an origin and destination
A route module uses a stops parameter to find a route. Stops are graphics that represent the arcgisViewClick event listener on the arcgis-map to add graphics when clicking on the map. The graphics will define the stops for the route.
-
Add a
arcgisViewClickevent listener to the map to addgraphics to the view.57 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {});</script>4 collapsed lines</body></html> -
Create an
addGraphicfunction to display a white marker for theorigin location and a black marker for thedestination . Add thegraphicto the map component’sview.57 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {});function addGraphic(type, point) {const graphic = new Graphic({symbol: new SimpleMarkerSymbol({color: type === "origin" ? "white" : "black",size: "8px",}),geometry: point,});viewElement.view.graphics.add(graphic);}</script>4 collapsed lines</body></html> -
Update the
arcgisViewClickevent listener to reference theaddGraphicfunction. The first click will create the origin. The second will make the destination and hide the notice. Subsequent clicks will clear the graphics to define a new origin and destination.73 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {const directionsNotice = document.querySelector("#directions-notice");if (viewElement.view.graphics.length === 0) {addGraphic("origin", event.detail.mapPoint);} else if (viewElement.view.graphics.length === 1) {directionsNotice.open = false;addGraphic("destination", event.detail.mapPoint);} else {viewElement.view.graphics.removeAll();directionsNotice.open = true;document.querySelector("#directions-container").innerHTML = "";addGraphic("origin", event.detail.mapPoint);}});18 collapsed linesfunction addGraphic(type, point) {const graphic = new Graphic({symbol: new SimpleMarkerSymbol({color: type === "origin" ? "white" : "black",size: "8px",}),geometry: point,});viewElement.view.graphics.add(graphic);}</script></body></html> -
Click on the map repeatedly in different locations to ensure the graphics are created and the notice is only visible when there are less than two graphics.
Find the route
To solve the route, add the stops parameter as a FeatureSet and then use the solve method. The resulting route will be added to the map as a Graphic.
Input parameters are necessary to solve the route. While there are many
parameters you can add, such as
-
Create a
getRoutefunction to calculate the route and directions. Create an instance ofRouteParametersand use thepoint graphics drawn on the map as the stops.103 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {const directionsNotice = document.querySelector("#directions-notice");if (viewElement.view.graphics.length === 0) {addGraphic("origin", event.detail.mapPoint);} else if (viewElement.view.graphics.length === 1) {directionsNotice.open = false;addGraphic("destination", event.detail.mapPoint);} else {viewElement.view.graphics.removeAll();directionsNotice.open = true;document.querySelector("#directions-container").innerHTML = "";addGraphic("origin", event.detail.mapPoint);}});function addGraphic(type, point) {const graphic = new Graphic({symbol: new SimpleMarkerSymbol({color: type === "origin" ? "white" : "black",size: "8px",}),geometry: point,});viewElement.view.graphics.add(graphic);}async function getRoute() {const routeParams = new RouteParameters({stops: new FeatureSet({features: viewElement.view.graphics.toArray(),}),});}7 collapsed lines</script></body></html> -
Call the
solvemethod to get the route. When the method returns, get the route fromrouteResultsand add it to the view as aGraphicwith a blue line.103 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {const directionsNotice = document.querySelector("#directions-notice");if (viewElement.view.graphics.length === 0) {addGraphic("origin", event.detail.mapPoint);} else if (viewElement.view.graphics.length === 1) {directionsNotice.open = false;addGraphic("destination", event.detail.mapPoint);} else {viewElement.view.graphics.removeAll();directionsNotice.open = true;document.querySelector("#directions-container").innerHTML = "";addGraphic("origin", event.detail.mapPoint);}});function addGraphic(type, point) {const graphic = new Graphic({symbol: new SimpleMarkerSymbol({color: type === "origin" ? "white" : "black",size: "8px",}),geometry: point,});viewElement.view.graphics.add(graphic);}async function getRoute() {const routeParams = new RouteParameters({stops: new FeatureSet({features: viewElement.view.graphics.toArray(),}),});try {const response = await route.solve(routeUrl, routeParams);response.routeResults.forEach((result) => {result.route.symbol = new SimpleLineSymbol({color: [5, 150, 255],width: 3,});viewElement.view.graphics.add(result.route);});} catch (error) {console.log(error);}}7 collapsed lines</script></body></html> -
Update the
arcgisViewClickevent listener to call thegetRoutefunction after creating the destination graphic.73 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {const directionsNotice = document.querySelector("#directions-notice");if (viewElement.view.graphics.length === 0) {addGraphic("origin", event.detail.mapPoint);} else if (viewElement.view.graphics.length === 1) {directionsNotice.open = false;addGraphic("destination", event.detail.mapPoint);getRoute();} else {viewElement.view.graphics.removeAll();directionsNotice.open = true;document.querySelector("#directions-container").innerHTML = "";addGraphic("origin", event.detail.mapPoint);}});43 collapsed linesfunction addGraphic(type, point) {const graphic = new Graphic({symbol: new SimpleMarkerSymbol({color: type === "origin" ? "white" : "black",size: "8px",}),geometry: point,});viewElement.view.graphics.add(graphic);}async function getRoute() {const routeParams = new RouteParameters({stops: new FeatureSet({features: viewElement.view.graphics.toArray(),}),});try {const response = await route.solve(routeUrl, routeParams);response.routeResults.forEach((result) => {result.route.symbol = new SimpleLineSymbol({color: [5, 150, 255],width: 3,});viewElement.view.graphics.add(result.route);});} catch (error) {console.log(error);}}</script></body></html> -
Click two locations on the map to display a route.
Get directions
You can get driving directions from the returnDirections property on RouteParameters. Use this property to return directions and add them to the map as HTML elements.
-
Go back to the
getRoutefunction and set thereturnDirectionsproperty totrue.105 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {const directionsNotice = document.querySelector("#directions-notice");if (viewElement.view.graphics.length === 0) {addGraphic("origin", event.detail.mapPoint);} else if (viewElement.view.graphics.length === 1) {directionsNotice.open = false;addGraphic("destination", event.detail.mapPoint);getRoute();} else {viewElement.view.graphics.removeAll();directionsNotice.open = true;document.querySelector("#directions-container").innerHTML = "";addGraphic("origin", event.detail.mapPoint);}});function addGraphic(type, point) {const graphic = new Graphic({symbol: new SimpleMarkerSymbol({color: type === "origin" ? "white" : "black",size: "8px",}),geometry: point,});viewElement.view.graphics.add(graphic);}async function getRoute() {const routeParams = new RouteParameters({stops: new FeatureSet({features: viewElement.view.graphics.toArray(),}),returnDirections: true,});try {const response = await route.solve(routeUrl, routeParams);response.routeResults.forEach((result) => {result.route.symbol = new SimpleLineSymbol({color: [5, 150, 255],width: 3,});viewElement.view.graphics.add(result.route);});} catch (error) {console.log(error);}}7 collapsed lines</script></body></html> -
After the solve method has been resolved, and if there are results, create a calcite-list that will display the directions and append it to the div we created earlier with the id of
directions-container.105 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {const directionsNotice = document.querySelector("#directions-notice");if (viewElement.view.graphics.length === 0) {addGraphic("origin", event.detail.mapPoint);} else if (viewElement.view.graphics.length === 1) {directionsNotice.open = false;addGraphic("destination", event.detail.mapPoint);getRoute();} else {viewElement.view.graphics.removeAll();directionsNotice.open = true;document.querySelector("#directions-container").innerHTML = "";addGraphic("origin", event.detail.mapPoint);}});function addGraphic(type, point) {const graphic = new Graphic({symbol: new SimpleMarkerSymbol({color: type === "origin" ? "white" : "black",size: "8px",}),geometry: point,});viewElement.view.graphics.add(graphic);}async function getRoute() {const routeParams = new RouteParameters({stops: new FeatureSet({features: viewElement.view.graphics.toArray(),}),returnDirections: true,});try {const response = await route.solve(routeUrl, routeParams);response.routeResults.forEach((result) => {result.route.symbol = new SimpleLineSymbol({color: [5, 150, 255],width: 3,});viewElement.view.graphics.add(result.route);});if (response.routeResults.length > 0) {const directions = document.createElement("calcite-list");document.querySelector("#directions-container").appendChild(directions);}} catch (error) {console.log(error);}}7 collapsed lines</script></body></html> -
Create a
calcite-list-itemfor each route feature to generate an ordered list of directions with their distances in miles. Append eachdirectionto thedirectionslist.105 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Find a route and directions</title><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script><style>html,body {height: 100%;margin: 0;}#directions-container {max-height: 50vh;width: 25vw;overflow-y: auto;}#directions-notice {max-width: 25vw;}</style></head><body><arcgis-map basemap="arcgis/navigation" center="-118.24532, 34.05398" zoom="12"><arcgis-zoom slot="top-left"></arcgis-zoom><calcite-panel slot="top-right" heading="Directions"><calcite-notice id="directions-notice" icon="driving-distance" open><div slot="message">Click on the map in two different locations to solve the route.</div></calcite-notice><div id="directions-container"></div></calcite-panel></arcgis-map><script type="module">const [Graphic, route, RouteParameters, FeatureSet, SimpleLineSymbol, SimpleMarkerSymbol] =await $arcgis.import(["@arcgis/core/Graphic.js","@arcgis/core/rest/route.js","@arcgis/core/rest/support/RouteParameters.js","@arcgis/core/rest/support/FeatureSet.js","@arcgis/core/symbols/SimpleLineSymbol.js","@arcgis/core/symbols/SimpleMarkerSymbol.js",]);const routeUrl ="https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";const viewElement = document.querySelector("arcgis-map");viewElement.addEventListener("arcgisViewClick", (event) => {const directionsNotice = document.querySelector("#directions-notice");if (viewElement.view.graphics.length === 0) {addGraphic("origin", event.detail.mapPoint);} else if (viewElement.view.graphics.length === 1) {directionsNotice.open = false;addGraphic("destination", event.detail.mapPoint);getRoute();} else {viewElement.view.graphics.removeAll();directionsNotice.open = true;document.querySelector("#directions-container").innerHTML = "";addGraphic("origin", event.detail.mapPoint);}});function addGraphic(type, point) {const graphic = new Graphic({symbol: new SimpleMarkerSymbol({color: type === "origin" ? "white" : "black",size: "8px",}),geometry: point,});viewElement.view.graphics.add(graphic);}async function getRoute() {const routeParams = new RouteParameters({stops: new FeatureSet({features: viewElement.view.graphics.toArray(),}),returnDirections: true,});try {const response = await route.solve(routeUrl, routeParams);response.routeResults.forEach((result) => {result.route.symbol = new SimpleLineSymbol({color: [5, 150, 255],width: 3,});viewElement.view.graphics.add(result.route);});if (response.routeResults.length > 0) {const directions = document.createElement("calcite-list");const features = response.routeResults[0].directions.features;features.forEach((feature, index) => {const direction = document.createElement("calcite-list-item");const step = document.createElement("span");step.innerText = `${index + 1}.`;step.slot = "content-start";step.style.marginLeft = "10px";direction.appendChild(step);direction.label = `${feature.attributes.text}`;direction.description = `${feature.attributes.length.toFixed(2)} miles`;directions.appendChild(direction);});document.querySelector("#directions-container").appendChild(directions);}} catch (error) {console.log(error);}}7 collapsed lines</script></body></html>
Run the App
In CodePen, run your code to display the map.
Click on the map twice to display the route directions. The map should support two clicks to create an origin and destination point and then use the
What’s next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: