Learn how to add the Directions component to a map.

Search for addresses and get directions between them using the directions component.

Routing is finding the path from an origin An origin is a point that defines the start of a route. Learn more to a destination A destination is a point that defines the final stop in a route. Learn more in a street network A network is a set of features that are typically interconnected to each other within a region. Learn more . You can use the Directions component to find routes and directions between two or more locations. The component provides a user-friendly interface to find routes and directions. You can also use the route module to find routes and directions programmatically. See the Find a route and directions tutorial for more information on using the route service.

Prerequisites

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

Get an access token

You need an access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more with the correct privileges to access the location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key An API key is a long-lived access token created using API key credentials. They are valid for up to one year and are typically embedded directly into client applications. Learn more with the following privilege(s) Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more :
    • Privileges
      • Location services > Basemaps
      • Location services > Geocoding
      • Location services > Routing
  2. In CodePen, set the apiKey property on the global esriConfig variable 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 layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more is typically used in routing applications. Update the basemap attribute to use the arcgis/navigation basemap layer and change the position of the map to center on Los Angeles.

  1. Update the basemap, center and zoom attributes.
    28 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: Display 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 the Directions component

  1. Add the arcgis-directions component with its position set to be the top-right corner of the map. Adding the use-default-route-layer attribute will automatically create and associate a default RouteLayer with the map. Alternatively, you can manually set the layer property to an existing RouteLayer instance in the map.

    28 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: Display 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-directions slot="top-right" use-default-route-layer></arcgis-directions>
    </arcgis-map>
    4 collapsed lines
    </body>
    </html>

Run the App

In CodePen, run your code to display the map.

You can add stops by searching for addresses or clicking a location on the map and set other options to find the appropriate route and directions between the stops.

What’s next?

Learn how to use additional SDK features and ArcGIS services in these tutorials: