Learn how to calculate the area that can be reached in a given drive time from a location.
A service area, also known as an isochrone, is a polygon that represents the area that can be reached when driving or walking on a street network. The area that can be reached is restricted by either time or distance. To calculate service areas, you can use the Service area service. You provide a start location (facilities), one or more time or distance values, and a spatial reference. 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
- To get started, either complete the Display a map tutorial or .
Get an access token
You need an access token with the correct privileges to access the location services 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 > Basemaps
- Location services > Routing
- Privileges
- In CodePen, set
esri
to your access token.Config.api Key Use dark colors for code blocks 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 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.
- Update the
basemap
attribute fromarcgis/topographic
toarcgis/navigation
. Then change thezoom
andcenter
attributes to center on Osaka.Use dark colors for code blocks <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11"> <arcgis-zoom position="top-left"></arcgis-zoom> </arcgis-map>
Add modules and event listeners
-
Add a
<script
tag in the> <body
following the> <arcgis-map
component with a> require
statement. In therequire
statement, add theservice
,Area Service
,Area Parameters Feature
, andSet Graphic
modules.The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD
require
function uses references to determine which modules will be loaded – for example, you can specify"esri/layers/
for loading the FeatureLayer module. After the modules are loaded, they are passed as parameters (e.g.Feature Layer" Feature
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. To learn more about the API's different modules visit the Overview Guide page.Layer Within the require statement, use the document.querySelector() method to access the map component.
Use dark colors for code blocks <script> require([ "esri/rest/serviceArea", "esri/rest/support/ServiceAreaParameters", "esri/rest/support/FeatureSet", "esri/Graphic" ], (serviceArea, ServiceAreaParams, FeatureSet, Graphic) => { const arcgisMap = document.querySelector("arcgis-map"); }); </script>
-
Create an event listener to listen for the map component's
arcgis
event.View Ready Change Use dark colors for code blocks <script> require([ "esri/rest/serviceArea", "esri/rest/support/ServiceAreaParameters", "esri/rest/support/FeatureSet", "esri/Graphic" ], (serviceArea, ServiceAreaParams, FeatureSet, Graphic) => { const arcgisMap = document.querySelector("arcgis-map"); arcgisMap.addEventListener("arcgisViewReadyChange", () => { }); }); </script>
Define the service url
The service
module makes a request to a service and returns the results. Use the service
class to access the Service area service.
- Define a property,
service
, to reference the service url.Area Url Use dark colors for code blocks arcgisMap.addEventListener("arcgisViewReadyChange", () => { const serviceAreaUrl = "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea"; });
Add a point graphic
Use a point graphic to display the location (facility) for the service area starting point.
-
In the
arcgis
event listener, add anView Ready Change arcgis
event handler to add a point graphic to the map.View Click Use dark colors for code blocks const serviceAreaUrl = "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea"; arcgisMap.addEventListener("arcgisViewClick", (event) => { });
-
Create a
create
function that takes a point as a parameter and returns a whiteGraphic Graphic
. It should remove all graphics each time.Use dark colors for code blocks // Create the location graphic function createGraphic(point) { arcgisMap.graphics.removeAll(); const graphic = new Graphic({ geometry: point, symbol: { type: "simple-marker", color: "white", size: 8 } }); arcgisMap.graphics.add(graphic); return graphic; }
-
Update the
arcgis
handler to call theView Click create
function and store the graphic inGraphic location
.Graphic Use dark colors for code blocks arcgisMap.addEventListener("arcgisViewClick", (event) => { const locationGraphic = createGraphic(event.detail.mapPoint); });
-
Click on the map to see a point graphic.
Create service area parameters
A service
uses drive times (cutoffs) and spatial reference parameters to calculate the service area. It also uses a Feature
to set the facilities
(location) from where the service area polygon will be drawn.
Use an arcgis
event handler to set the parameters required to create the service area.
-
Create a
create
function that takes point graphic, drive time, and spatial reference parameters.Service Area Params Use dark colors for code blocks arcgisMap.graphics.add(graphic); return graphic; } function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) { }
-
Create a
Feature
to set theSet features
property with the point graphic.Use dark colors for code blocks function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) { // Create one or more locations (facilities) to solve for const featureSet = new FeatureSet({ features: [locationGraphic] }); }
-
Create a
Service
and return theArea Params task
element.Parameters Use dark colors for code blocks 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; }
-
Update the
arcgis
handler to add drive time cutoffs of 5, 10, and 15 minutes and to call theView Click create
function.Service Area Params Use dark colors for code blocks arcgisMap.addEventListener("arcgisViewClick", (event) => { const locationGraphic = createGraphic(event.detail.mapPoint); const driveTimeCutoffs = [5, 10, 15]; // Minutes const serviceAreaParams = createServiceAreaParams( locationGraphic, driveTimeCutoffs, arcgisMap.spatialReference ); });
Solve the service area
To solve the service area, pass the service
to the solve
method. Use a solve
function to find the service area polygon and add the resulting graphic to the map.
-
Create a
solve
function.Service Area Use dark colors for code blocks // 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) { }
-
Call the
solve
method to find the service area and add the results to the map.Use dark colors for code blocks 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)" }; arcgisMap.graphics.add(graphic, 0); }); } }, (error) => { console.log(error); } ); }
-
Update the
arcgis
handler to call theView Click solve
function.Service Area Use dark colors for code blocks arcgisMap.addEventListener("arcgisViewClick", (event) => { const locationGraphic = createGraphic(event.detail.mapPoint); const driveTimeCutoffs = [5, 10, 15]; // Minutes const serviceAreaParams = createServiceAreaParams( locationGraphic, driveTimeCutoffs, arcgisMap.spatialReference ); solveServiceArea(serviceAreaUrl, serviceAreaParams); });
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 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 API features and ArcGIS services in these tutorials: