Learn how to calculate the area that can be reached in a given drive time from a location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more .

Click on the map to generate a drive time service area.

A service area A service area is a polygon representing the distance that can be reached within a specified length of time while traveling in a street network. Learn more , also known as an isochrone, is a polygon A polygon is a type of geometry containing an array of rings and a spatial reference. Each ring contains an array of point coordinates, where the first and last point are the same. Learn more that represents the area that can be reached when driving or walking on a street network A network is a set of features that are typically interconnected to each other within a region. Learn more . The area that can be reached is restricted by either time or distance. To calculate service areas, you can use the Service area service A routing service is a service that uses network analysis and streets data to calculate the most effective path and turn-by-turn directions on a street network, optimize fleet routing and deliveries, find the closest facilities, calculate service areas, and more. It is hosted by Esri as the ArcGIS Routing service and can also be hosted in ArcGIS Enterprise. Learn more . You provide a start location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more (facilities), one or more time or distance values, and a spatial reference A spatial reference is a set of parameters, typically defined by a WKID, that define the coordinate system and spatial properties for geographic data. Applications use a spatial reference to correctly display the position of geographic data in a map or scene. Learn more . Once processed, the service returns the service areas that can be reached.

In this tutorial, you will create and display five, ten, and fifteen minute drive time service areas when the map is clicked.

To learn how to find a route and directions to different locations, visit the Get a route and directions tutorial.

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 > 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 Osaka.

  1. Update the basemap attribute from arcgis/topographic to arcgis/navigation. Then change the zoom and center attributes to center on Osaka.
    29 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    5 collapsed lines
    </body>
    </html>

Add modules and wait for the Map to be ready

  1. Add a <script> tag in the <body> following the <arcgis-map> component. Use $arcgis.import() to add the serviceArea, ServiceAreaParameters, FeatureSet, and Graphic modules.

    Use the document.querySelector() method to access the map component.

    35 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    </script>
    5 collapsed lines
    </body>
    </html>
  2. Wait for the viewOnReady method to resolve to ensure the Map component is loaded and ready to use.

    35 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    </script>
    5 collapsed lines
    </body>
    </html>

Define the service url

The serviceArea module makes a request to a service A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more and returns the results. Use the serviceArea class to access the Service area service.

  1. Define a property, serviceAreaUrl, to reference the service url.
    48 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    6 collapsed lines
    </script>
    </body>
    </html>

Add a point graphic

Use a point graphic A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more to display the location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more (facility) for the service area starting point.

  1. Add an arcgisViewClick event handler to add a point graphic A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more to the map.

    48 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    });
    7 collapsed lines
    </script>
    </body>
    </html>
  2. Create a createGraphic function that takes a point A point is a type of geometry containing a single set of x,y coordinates and a spatial reference. Learn more as a parameter and returns a white Graphic. It should remove all graphics each time.

    55 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    });
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  3. Update the arcgisViewClick handler to call the createGraphic function and store the graphic in locationGraphic.

    51 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const locationGraphic = createGraphic(event.detail.mapPoint);
    });
    23 collapsed lines
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    </script>
    </body>
    </html>
  4. Click on the map to see a point graphic A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more .

Create service area parameters

A serviceArea uses drive times (cutoffs) and spatial reference parameters to calculate the service area A service area is a polygon representing the distance that can be reached within a specified length of time while traveling in a street network. Learn more . It also uses a FeatureSet to set the facilities (location) from where the service area polygon A polygon is a type of geometry containing an array of rings and a spatial reference. Each ring contains an array of point coordinates, where the first and last point are the same. Learn more will be drawn. Use an arcgisViewClick event handler to set the parameters required to create the service area.

  1. Create a createServiceAreaParams function that takes point graphic A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more , drive time, and spatial reference A spatial reference is a set of parameters, typically defined by a WKID, that define the coordinate system and spatial properties for geographic data. Applications use a spatial reference to correctly display the position of geographic data in a map or scene. Learn more parameters.

    69 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const locationGraphic = createGraphic(event.detail.mapPoint);
    });
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) {
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  2. Create a FeatureSet to set the features property with the point graphic A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more .

    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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const locationGraphic = createGraphic(event.detail.mapPoint);
    });
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) {
    // Create one or more locations (facilities) to solve for
    const featureSet = new FeatureSet({
    features: [locationGraphic],
    });
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  3. Create a ServiceAreaParams and return the taskParameters element.

    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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const locationGraphic = createGraphic(event.detail.mapPoint);
    });
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) {
    // Create one or more locations (facilities) to solve for
    const featureSet = new FeatureSet({
    features: [locationGraphic],
    });
    // Set all of the input parameters for the service
    const taskParameters = new ServiceAreaParams({
    facilities: featureSet,
    defaultBreaks: driveTimeCutoffs,
    trimOuterPolygon: true,
    outSpatialReference: outSpatialReference,
    });
    return taskParameters;
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  4. Update the arcgisViewClick handler to add drive time cutoffs of 5, 10, and 15 minutes and to call the createServiceAreaParams function.

    51 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const locationGraphic = createGraphic(event.detail.mapPoint);
    const driveTimeCutoffs = [5, 10, 15]; // Minutes
    const serviceAreaParams = createServiceAreaParams(
    locationGraphic,
    driveTimeCutoffs,
    viewElement.spatialReference,
    );
    });
    41 collapsed lines
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) {
    // Create one or more locations (facilities) to solve for
    const featureSet = new FeatureSet({
    features: [locationGraphic],
    });
    // Set all of the input parameters for the service
    const taskParameters = new ServiceAreaParams({
    facilities: featureSet,
    defaultBreaks: driveTimeCutoffs,
    trimOuterPolygon: true,
    outSpatialReference: outSpatialReference,
    });
    return taskParameters;
    }
    </script>
    </body>
    </html>

Solve the service area

To solve the service area, pass the serviceAreaParams to the solve method. Use a solveServiceArea function to find the service area polygon A polygon is a type of geometry containing an array of rings and a spatial reference. Each ring contains an array of point coordinates, where the first and last point are the same. Learn more and add the resulting graphic to the map.

  1. Create a solveServiceArea function.

    89 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const locationGraphic = createGraphic(event.detail.mapPoint);
    const driveTimeCutoffs = [5, 10, 15]; // Minutes
    const serviceAreaParams = createServiceAreaParams(
    locationGraphic,
    driveTimeCutoffs,
    viewElement.spatialReference,
    );
    solveServiceArea(serviceAreaUrl, serviceAreaParams);
    });
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) {
    // Create one or more locations (facilities) to solve for
    const featureSet = new FeatureSet({
    features: [locationGraphic],
    });
    // Set all of the input parameters for the service
    const taskParameters = new ServiceAreaParams({
    facilities: featureSet,
    defaultBreaks: driveTimeCutoffs,
    trimOuterPolygon: true,
    outSpatialReference: outSpatialReference,
    });
    return taskParameters;
    }
    function solveServiceArea(url, serviceAreaParams) {
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  2. Call the solve method to find the service area and add the results to the map.

    100 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const locationGraphic = createGraphic(event.detail.mapPoint);
    const driveTimeCutoffs = [5, 10, 15]; // Minutes
    const serviceAreaParams = createServiceAreaParams(
    locationGraphic,
    driveTimeCutoffs,
    viewElement.spatialReference,
    );
    solveServiceArea(serviceAreaUrl, serviceAreaParams);
    });
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) {
    // Create one or more locations (facilities) to solve for
    const featureSet = new FeatureSet({
    features: [locationGraphic],
    });
    // Set all of the input parameters for the service
    const taskParameters = new ServiceAreaParams({
    facilities: featureSet,
    defaultBreaks: driveTimeCutoffs,
    trimOuterPolygon: true,
    outSpatialReference: outSpatialReference,
    });
    return taskParameters;
    }
    function solveServiceArea(url, serviceAreaParams) {
    return serviceArea.solve(url, serviceAreaParams).then(
    (result) => {
    if (result.serviceAreaPolygons.features.length) {
    // Draw each service area polygon
    result.serviceAreaPolygons.features.forEach((graphic) => {
    graphic.symbol = {
    type: "simple-fill",
    color: "rgba(255,50,50,.25)",
    };
    viewElement.graphics.add(graphic, 0);
    });
    }
    },
    (error) => {
    console.log(error);
    },
    );
    }
    7 collapsed lines
    </script>
    </body>
    </html>
  3. Update the arcgisViewClick handler to call the solveServiceArea function.

    51 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 service areas</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <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>
    </head>
    <body>
    <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    const [serviceArea, ServiceAreaParams, FeatureSet, Graphic] = await $arcgis.import([
    "@arcgis/core/rest/serviceArea.js",
    "@arcgis/core/rest/support/ServiceAreaParameters.js",
    "@arcgis/core/rest/support/FeatureSet.js",
    "@arcgis/core/Graphic.js",
    ]);
    const viewElement = document.querySelector("arcgis-map");
    await viewElement.viewOnReady();
    const serviceAreaUrl =
    "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea";
    viewElement.addEventListener("arcgisViewClick", (event) => {
    const locationGraphic = createGraphic(event.detail.mapPoint);
    const driveTimeCutoffs = [5, 10, 15]; // Minutes
    const serviceAreaParams = createServiceAreaParams(
    locationGraphic,
    driveTimeCutoffs,
    viewElement.spatialReference,
    );
    solveServiceArea(serviceAreaUrl, serviceAreaParams);
    });
    63 collapsed lines
    // Create the location graphic
    function createGraphic(point) {
    viewElement.graphics.removeAll();
    const graphic = new Graphic({
    geometry: point,
    symbol: {
    type: "simple-marker",
    color: "white",
    size: 8,
    },
    });
    viewElement.graphics.add(graphic);
    return graphic;
    }
    function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) {
    // Create one or more locations (facilities) to solve for
    const featureSet = new FeatureSet({
    features: [locationGraphic],
    });
    // Set all of the input parameters for the service
    const taskParameters = new ServiceAreaParams({
    facilities: featureSet,
    defaultBreaks: driveTimeCutoffs,
    trimOuterPolygon: true,
    outSpatialReference: outSpatialReference,
    });
    return taskParameters;
    }
    function solveServiceArea(url, serviceAreaParams) {
    return serviceArea.solve(url, serviceAreaParams).then(
    (result) => {
    if (result.serviceAreaPolygons.features.length) {
    // Draw each service area polygon
    result.serviceAreaPolygons.features.forEach((graphic) => {
    graphic.symbol = {
    type: "simple-fill",
    color: "rgba(255,50,50,.25)",
    };
    viewElement.graphics.add(graphic, 0);
    });
    }
    },
    (error) => {
    console.log(error);
    },
    );
    }
    </script>
    </body>
    </html>

Run the app

In CodePen, run your code to display the map.

Click on the map to create service areas. When you click on the map, you will see a point graphic A graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. Learn more along with drive time service areas. The service areas represent the area that can be reached within 5, 10, and 15 minutes.

What’s next?

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