Learn how to calculate the area
A service area
In this tutorial, you create and display five, ten, and fifteen-minute drive time service areas when the map is clicked. You use data-driven styling to give each polygon a different shade of blue.
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Get the starter app
Select a type of authentication and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications
A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. that access ArcGIS Location ServicesArcGIS 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 Static Maps 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. and secure itemsAn item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. and access resources their behalf. - Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Use the API key or OAuth developer credentials
Add references to ArcGIS REST JS
-
Reference the
routingandrequestpackages from ArcGIS REST JS.Use dark colors for code blocks <!-- Load Leaflet from CDN --> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" crossorigin=""> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" crossorigin=""></script> <!-- Load Esri Leaflet from CDN --> <script src="https://unpkg.com/esri-leaflet@3.0.19/dist/esri-leaflet.js"></script> <script src="https://unpkg.com/esri-leaflet-vector@4.3.2/dist/esri-leaflet-vector.js"></script> <!-- Load ArcGIS REST JS from CDN --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-routing@4/dist/bundled/routing.umd.js"></script>
Update the map
A navigation basemap layerarcgis/navigation.
-
Update the basemap and the map initialization to center on location
[100.5231,13.7367], Bangkok.Use dark colors for code blocks Copy /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapEnum = "arcgis/streets"; const map = L.map("map", { minZoom: 2 }); map.setView([13.7367, 100.5231], 13); // Bangkok L.esri.Vector.vectorBasemapLayer(basemapEnum, { token: accessToken }).addTo(map);
Add layer groups
In this app, you display a marker for the starting point location and polygons for the service areas. Add a Layer to display the point and polygons on the map.
-
Add a
Layerfor theGroup clickedand another for thePoint service.Areas Use dark colors for code blocks L.esri.Vector.vectorBasemapLayer(basemapEnum, { token: accessToken }).addTo(map); const clickedPoints = L.layerGroup().addTo(map); const serviceAreas = L.layerGroup().addTo(map);
Add a click handler
When you click on the map, you will update the location of the clicked and return a new service area
-
Add a
clickevent handler. Inside, remove the data from the previous click withclear.Layers Use dark colors for code blocks const clickedPoints = L.layerGroup().addTo(map); const serviceAreas = L.layerGroup().addTo(map); map.on("click", (e) => { clickedPoints.clearLayers(); serviceAreas.clearLayers(); }); -
Display a
Markerat the clicked location.Use dark colors for code blocks map.on("click", (e) => { clickedPoints.clearLayers(); serviceAreas.clearLayers(); L.marker(e.latlng).addTo(clickedPoints); });
Get the service area
With the longitude and latitude of the click event, you can call the service function from ArcGIS REST JS to get the service area
-
Create a new
Apito access the route service.Key Manager Use dark colors for code blocks const clickedPoints = L.layerGroup().addTo(map); const serviceAreas = L.layerGroup().addTo(map); const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken); map.on("click", (e) => { clickedPoints.clearLayers(); serviceAreas.clearLayers(); L.marker(e.latlng).addTo(clickedPoints); }); -
Call the
serviceoperation. Set theArea facilitiesparameter with the clicked coordinates to calculate the service area.The
facilitiesparameter lets you pass in multiple locations around which the service area is calculated. In this case, you are only passing one.By default, the three drive times that are requested are 5, 10 and 15 minutes. You can change these by passing the
defaultparameter.Breaks Use dark colors for code blocks const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken); map.on("click", (e) => { clickedPoints.clearLayers(); serviceAreas.clearLayers(); L.marker(e.latlng).addTo(clickedPoints); arcgisRest .serviceArea({ endpoint: "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea", authentication, facilities: [[e.latlng.lng, e.latlng.lat]] }) }); -
Add an error handler. Inside, show a message and write the error to the console.
Use dark colors for code blocks arcgisRest .serviceArea({ endpoint: "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea", authentication, facilities: [[e.latlng.lng, e.latlng.lat]] }) .catch((error) => { console.error(error); alert("There was a problem using the route service. See the console for details."); });
Display the service area on the map
The response to the request contains the geographic information of the service areas that you can access and display on the map.
-
Add a response handler. Inside, add a
Geolayer that adds the features to theJSON servicelayer.Areas Use dark colors for code blocks arcgisRest .serviceArea({ endpoint: "https://route-api.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea", authentication, facilities: [[e.latlng.lng, e.latlng.lat]] }) .then((response) => { L.geoJSON(response.saPolygons.geoJson, { } ).addTo(serviceAreas); }) .catch((error) => { console.error(error); alert("There was a problem using the route service. See the console for details."); }); -
Style the different polygons using the
Fromproperty to distinguish the 5, 10, and 15 minute drive time polygons.Break Use dark colors for code blocks .then((response) => { L.geoJSON(response.saPolygons.geoJson, { style: (feature) => { const style = { fillOpacity: 0.5, weight: 1 }; if (feature.properties.FromBreak === 0) { style.color = "hsl(210, 80%, 40%)"; } else if (feature.properties.FromBreak === 5) { style.color = "hsl(210, 80%, 60%)"; } else { style.color = "hsl(210, 80%, 80%)"; } return style; } } ).addTo(serviceAreas); })
Run the app
Run the app.
When you click on the map, three service areas are shown as concentric polygons around a marker. These indicate the areas that can be reached by driving for 5, 10 or 15 minutes.What's next?
Learn how to use additional location services in these tutorials:


