Learn how to calculate the area that can be reached in a given drive time from a
Click on the map to generate a drive time service area.
A
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
- Go to the Create an API key tutorial and create an
API key with the followingprivilege(s) :
- 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 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 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 serviceArea 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
-
Add an
arcgisViewClickevent handler to add a pointgraphic 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 apoint as a parameter and returns a whiteGraphic. 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 .
Create service area parameters
A serviceArea uses drive times (cutoffs) and spatial reference parameters to calculate the FeatureSet to set the facilities (location) from where the service area arcgisViewClick event handler to set the parameters required to create the service area.
-
Create a
createServiceAreaParamsfunction that takes pointgraphic , drive time, andspatial reference 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 pointgraphic .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
-
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
What’s next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: