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
esrito 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
basemapattribute fromarcgis/topographictoarcgis/navigation. Then change thezoomandcenterattributes to center on Osaka.Use dark colors for code blocks <arcgis-map basemap="arcgis/navigation" center="135.5023,34.6937" zoom="11"> <arcgis-zoom slot="top-left"></arcgis-zoom> </arcgis-map>
Add modules and wait for the Map to be ready
-
Add a
<scripttag in the> <bodyfollowing the> <arcgis-mapcomponent. Use> $arcgis.import()to add theservice,Area Service,Area Parameters Feature, andSet Graphicmodules.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.Use the document.querySelector() method to access the map component.
Use dark colors for code blocks <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> -
Wait for the
viewmethod to resolve to ensure the Map component is loaded and ready to use.On Ready Use dark colors for code blocks <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>
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 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.
-
Add an
arcgisevent 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"; viewElement.addEventListener("arcgisViewClick", (event) => { }); -
Create a
createfunction 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) { viewElement.graphics.removeAll(); const graphic = new Graphic({ geometry: point, symbol: { type: "simple-marker", color: "white", size: 8, }, }); viewElement.graphics.add(graphic); return graphic; } -
Update the
arcgishandler to call theView Click createfunction and store the graphic inGraphic location.Graphic Use dark colors for code blocks viewElement.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
createfunction that takes point graphic, drive time, and spatial reference parameters.Service Area Params Use dark colors for code blocks viewElement.graphics.add(graphic); return graphic; } function createServiceAreaParams(locationGraphic, driveTimeCutoffs, outSpatialReference) { } -
Create a
Featureto set theSet featuresproperty 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
Serviceand return theArea Params taskelement.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
arcgishandler to add drive time cutoffs of 5, 10, and 15 minutes and to call theView Click createfunction.Service Area Params Use dark colors for code blocks viewElement.addEventListener("arcgisViewClick", (event) => { const locationGraphic = createGraphic(event.detail.mapPoint); const driveTimeCutoffs = [5, 10, 15]; // Minutes const serviceAreaParams = createServiceAreaParams( locationGraphic, driveTimeCutoffs, viewElement.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
solvefunction.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
solvemethod 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)", }; viewElement.graphics.add(graphic, 0); }); } }, (error) => { console.log(error); }, ); } -
Update the
arcgishandler to call theView Click solvefunction.Service Area Use dark colors for code blocks 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); });
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 SDK features and ArcGIS services in these tutorials: