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 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
Get a Cesium ion access token
All Cesium applications must use an access token provided through Cesium ion. This token allows you to access assets such as Cesium World Terrain in your application.
-
Go to your Cesium ion dashboard to generate an access token. Copy the key to your clipboard.
-
Create a
cesiumvariable and replaceAccess Token YOURwith the access token you copied from the Cesium ion dashboard._CESIUM _ACCESS _TOKEN Use dark colors for code blocks <script> /* 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; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; </script> -
Configure
Cesium.with the Cesium access token to validate the application.Ion.default Access Token Use dark colors for code blocks <script> /* 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; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; Cesium.Ion.defaultAccessToken = cesiumAccessToken; </script>
Add references to ArcGIS REST JS
-
In the
<headelement, reference the> routingandrequestpackages from ArcGIS REST JS.Use dark colors for code blocks <script src="https://cesium.com/downloads/cesiumjs/releases/1.143/Build/Cesium/Cesium.js"></script> <link href="https://cesium.com/downloads/cesiumjs/releases/1.143/Build/Cesium/Widgets/widgets.css" rel="stylesheet"> <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 scene
This routing application uses a 3D object layer San Francisco. Update the scene extent to focus on San Francisco, CA.
-
Update the
setcommand to center on locationView [-122.38329, 37.74015, 16000], San Francisco.Use dark colors for code blocks Copy viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(-122.38329, 37.74015, 16000), orientation: { pitch: Cesium.Math.toRadians(-70.0) } });
Create a starting location graphic
Create a Billboard entity to indicate the location from which a service area will be calculated. You can use Billboard entities to display custom pins and images.
-
Add a new
Billboardentity to the viewer to represent the origin location. Set thepositionof the entity tonull.Use dark colors for code blocks viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(-122.38329, 37.74015, 16000), orientation: { pitch: Cesium.Math.toRadians(-70.0) } }); // Add Esri attribution // Learn more in https://esriurl.com/attribution const poweredByEsri = new Cesium.Credit("Powered by <a href='https://www.esri.com/en-us/home' target='_blank'>Esri</a>", true); viewer.creditDisplay.addStaticCredit(poweredByEsri); const origin = viewer.entities.add({ name: "start", position: null, billboard: { verticalOrigin: Cesium.VerticalOrigin.BOTTOM, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, } }); -
Create a
Pinand use it to set theBuilder imageproperty of the billboard.Use dark colors for code blocks const pinBuilder = new Cesium.PinBuilder(); const origin = viewer.entities.add({ name: "start", position: null, billboard: { verticalOrigin: Cesium.VerticalOrigin.BOTTOM, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, image: pinBuilder.fromColor(Cesium.Color.fromCssColorString("#ffffff"), 48).toDataURL() } });
Add a click handler
Use an event handler to set the location of the origin and when a user clicks.
-
Create a
Screenfor theSpace Event Handler Viewerthat listens for left clicks. Create anonfunction that runs when a click is detected.Left Click Use dark colors for code blocks image: pinBuilder.fromColor(Cesium.Color.fromCssColorString("#ffffff"), 48).toDataURL() } }); viewer.screenSpaceEventHandler.setInputAction((movement) => { }, Cesium.ScreenSpaceEventType.LEFT_CLICK); -
Set the position of the
originentity to the coordinates of the clicked location. Callviewer.datato remove stale service area polygons.Sources.remove All() Use dark colors for code blocks viewer.screenSpaceEventHandler.setInputAction((movement) => { const pickedPosition = viewer.scene.pickPosition(movement.position); origin.position = pickedPosition; viewer.dataSources.removeAll(); }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
Get service areas
To calculate a service area from a point, use the ArcGIS REST JS service function to make a request to the Routing service
-
Create a new
arcgisand set your access token.Rest. Api Key Manager Use dark colors for code blocks /* 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; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken); -
Create a function called
getthat accepts a coordinate pair. Inside, callService Area arcgisand set theRest.service Area facilitiesparameter to the set of coordinates.arcgiscreates a direct request to theRest.service Area solveendpoint of the Routing serviceService Area A routing service is a service that uses network analysis and streets data to calculate the most effective path and turn-by-turn directions on a street network, optimize fleet routing and deliveries, find the closest facilities, calculate service areas, and more. It is hosted by Esri as the ArcGIS Routing service and can also be hosted in ArcGIS Enterprise. . To learn more about thesolveendpoint, go to the Routing service page in the Mapping and location services guide.Service Area Use dark colors for code blocks viewer.screenSpaceEventHandler.setInputAction((movement) => { const pickedPosition = viewer.scene.pickPosition(movement.position); origin.position = pickedPosition; viewer.dataSources.removeAll(); }, Cesium.ScreenSpaceEventType.LEFT_CLICK); function getServiceArea(coordinates) { arcgisRest .serviceArea({ facilities: [coordinates], authentication }) } -
Access the service response and add the returned GeoJSON to your application as a
Geo.Json Data Source Use dark colors for code blocks function getServiceArea(coordinates) { arcgisRest .serviceArea({ facilities: [coordinates], authentication }) .then((response) => { const serviceJSON = response.saPolygons.geoJson; Cesium.GeoJsonDataSource.load(serviceJSON, { clampToGround: true }) .then((dataSource) => { viewer.dataSources.add(dataSource); }); }); } -
Set the
materialof each service area polygon based on its drive time from the origin using theFromproperty.Break Use dark colors for code blocks .then((response) => { const serviceJSON = response.saPolygons.geoJson; Cesium.GeoJsonDataSource.load(serviceJSON, { clampToGround: true }) .then((dataSource) => { viewer.dataSources.add(dataSource); const entities = dataSource.entities.values; for (let i = 0; i < entities.length; i++) { const feature = entities[i]; feature.polygon.outline = false; if (feature.properties.FromBreak == 0) { feature.polygon.material = Cesium.Color.fromHsl(0.5833, 0.8, 0.4, 0.7); feature.polygon.extrudedHeight = 300; } else if (feature.properties.FromBreak == 5) { feature.polygon.material = Cesium.Color.fromHsl(0.5833, 0.8, 0.6, 0.7); feature.polygon.extrudedHeight = 200; } else { feature.polygon.material = Cesium.Color.fromHsl(0.5833, 0.8, 0.8, 0.7); feature.polygon.extrudedHeight = 100; } } }); }); -
In your
onfunction, convert the coordinates of the user's click fromLeft Click Cartesianto longitude and latitude degrees. Callgetwith the longitude and latitudeService Area Use dark colors for code blocks viewer.screenSpaceEventHandler.setInputAction((movement) => { const pickedPosition = viewer.scene.pickPosition(movement.position); origin.position = pickedPosition; viewer.dataSources.removeAll(); const cartographic = Cesium.Cartographic.fromCartesian(pickedPosition); const originLatLng = [Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude)]; getServiceArea(originLatLng); }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
Run the app
Run the app.
When the application loads, you should be able to click on a locations to create an origin point. The Routing service should then calculate drive time polygons representing the areas reachable within a 5, 10, and 15 minute drive.What's next?
Learn how to use additional location services in these tutorials:


