Learn how to calculate the area that can be reached in a given drive time from a location
Click on the map to generate a drive time service area.
A service area
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
- 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. 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. :
- Privileges
- Location services > Basemaps
- 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 layerbasemap 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.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
-
Add a
<script>tag in the<body>following the<arcgis-map>component. Use$arcgis.import()to add theserviceArea,ServiceAreaParameters,FeatureSet, andGraphicmodules.The ArcGIS Maps SDK for JavaScript
ArcGIS Maps SDK for JavaScript, previously known as ArcGIS API for JavaScript, is a developer product for building mapping and spatial analysis applications for the web. 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.
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> -
Wait for the
viewOnReadymethod 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 serviceserviceArea class to access the Service area service.
- 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
-
Add an
arcgisViewClickevent handler to add a point graphicA graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. 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> -
Create a
createGraphicfunction that takes a pointA point is a type of geometry containing a single set of as a parameter and returns a whitex,ycoordinates and a spatial reference.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 graphicfunction 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> -
Update the
arcgisViewClickhandler to call thecreateGraphicfunction and store the graphic inlocationGraphic.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 graphicfunction 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> -
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. .
Create service area parameters
A serviceArea uses drive times (cutoffs) and spatial reference parameters to calculate the service areaFeatureSet to set the facilities (location) from where the service area polygonarcgisViewClick event handler to set the parameters required to create the service area.
-
Create a
createServiceAreaParamsfunction that takes point graphicA graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. , drive time, and spatial referenceA 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. 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 graphicfunction 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> -
Create a
FeatureSetto set thefeaturesproperty with the point graphicA graphic is a visual element composed of a geometry, symbol, and attributes that is displayed on a map or scene. .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 graphicfunction 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 forconst featureSet = new FeatureSet({features: [locationGraphic],});}7 collapsed lines</script></body></html> -
Create a
ServiceAreaParamsand return thetaskParameterselement.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 graphicfunction 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 forconst featureSet = new FeatureSet({features: [locationGraphic],});// Set all of the input parameters for the serviceconst taskParameters = new ServiceAreaParams({facilities: featureSet,defaultBreaks: driveTimeCutoffs,trimOuterPolygon: true,outSpatialReference: outSpatialReference,});return taskParameters;}7 collapsed lines</script></body></html> -
Update the
arcgisViewClickhandler to add drive time cutoffs of 5, 10, and 15 minutes and to call thecreateServiceAreaParamsfunction.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]; // Minutesconst serviceAreaParams = createServiceAreaParams(locationGraphic,driveTimeCutoffs,viewElement.spatialReference,);});41 collapsed lines// Create the location graphicfunction 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 forconst featureSet = new FeatureSet({features: [locationGraphic],});// Set all of the input parameters for the serviceconst 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
-
Create a
solveServiceAreafunction.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]; // Minutesconst serviceAreaParams = createServiceAreaParams(locationGraphic,driveTimeCutoffs,viewElement.spatialReference,);solveServiceArea(serviceAreaUrl, serviceAreaParams);});// Create the location graphicfunction 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 forconst featureSet = new FeatureSet({features: [locationGraphic],});// Set all of the input parameters for the serviceconst taskParameters = new ServiceAreaParams({facilities: featureSet,defaultBreaks: driveTimeCutoffs,trimOuterPolygon: true,outSpatialReference: outSpatialReference,});return taskParameters;}function solveServiceArea(url, serviceAreaParams) {}7 collapsed lines</script></body></html> -
Call the
solvemethod 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]; // Minutesconst serviceAreaParams = createServiceAreaParams(locationGraphic,driveTimeCutoffs,viewElement.spatialReference,);solveServiceArea(serviceAreaUrl, serviceAreaParams);});// Create the location graphicfunction 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 forconst featureSet = new FeatureSet({features: [locationGraphic],});// Set all of the input parameters for the serviceconst 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 polygonresult.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> -
Update the
arcgisViewClickhandler to call thesolveServiceAreafunction.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]; // Minutesconst serviceAreaParams = createServiceAreaParams(locationGraphic,driveTimeCutoffs,viewElement.spatialReference,);solveServiceArea(serviceAreaUrl, serviceAreaParams);});63 collapsed lines// Create the location graphicfunction 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 forconst featureSet = new FeatureSet({features: [locationGraphic],});// Set all of the input parameters for the serviceconst 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 polygonresult.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
What’s next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: