Learn how to calculate the area that can be reached in a given driving 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 routing 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 use ArcGIS REST JS to access the routing service to 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.
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
In CodePen, update apiKey to use your key.
Use dark colors for code blocks
Change line
1
2
3
4
5
6
7
8
9
10
const apiKey = "YOUR_API_KEY";
const basemapEnum = "ArcGIS:Streets";
const map = new maplibregl.Map({
container: "map", // the id of the div elementstyle: `https://basemaps-api.arcgis.com/arcgis/rest/services/styles/${basemapEnum}?type=style&token=${apiKey}`,
zoom: 12, // starting zoomcenter: [-118.805, 34.027] // starting location [longitude, latitude]});
Reference the ArcGIS REST JS
Add links to the ArcGIS REST JS libraries in the <script> section.
Add a GeoJSON source with id start. Set the data attribute to an empty FeatureCollection.
This source will later contain the geometry information of the start or end point that the user has selected. For now, you can simply provide it with an empty piece of GeoJSON: a feature collection that contains no features.
To show the service area, you will use a fill layer to display a GeoJSON source. Each polygon has a property called FromBreak which contains the lower bound of the number of minutes of travel: 0, 5 and 10 minutes. You can use an expression for the fill-color to display each polygon in a different shade of blue.
Start a new function called addServiceAreaLayer().
Add a GeoJSON source for the service area, with id servicearea. Set the data attribute of each source to be an empty FeatureCollection.
This source will later contain the geometry information of the service area. For now, you can simply provide it with an empty piece of GeoJSON: a feature collection that contains no features.
Add a layer of type fill to display the service area polygons, connected to the servicearea source. Use a match expression for the fill-color to show each polygon in a different shade of blue: darkest for shortest time, and lightest for longest time.
The match expression is like a switch statement: it compares the value of the FromBreak property of each polygon feature, and compares it against the values 0, 5 or 10. If it matches, the corresponding color becomes the fill-color. Otherwise, the last value, transparent is used.
You need to use the load event to ensure the map is fully loaded, before adding your layers.
Add an event handler to the map load event. Call the addServiceAreaLayer() and addStartingPointLayer() functions here.
The order in which layers are added to the map is the order in which they will be displayed: layers added later are displayed over the top. Therefore, to have the starting point layer remain visible, you should add it after the service area layer.
When you click on the map, the white circle should move to each location that you click.
Get the service area
With the coordinates of the click event, you can now call the serviceArea function in the route service to get the service area.
Inside the click handler, create a new arcgisRest.ApiKeyManager to access the route service. Call the serviceArea method. Set the facilities parameter with the clicked coordinates to calculate the service area.
The facilities parameter 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 defaultBreaks parameter.
The response to the request contains the geographic information of the service areas. Use the saPolygons.geoJson property to update the servicearea source.
Use Map.getSource to access the servicearea source. Call setData to update the data with the GeoJSON returned from the API.
When you click on the map, three service areas are shown as concentric polygons around a white circle. These indicate the areas that can be reached by driving for 5, 10 or 15 minutes.