Learn how to calculate the area that can be reached in a given driving time from a location.
The ArcGIS Routing service allows you to calculate service areas. 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.
In this tutorial, use the MapLibre ArcGIS plugin to display a map and 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.
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 that access ArcGIS Location Services and secure items.
- 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 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 so your application can access ArcGIS services.
Reference the ArcGIS REST JS
-
Add links to the ArcGIS REST JS libraries in the
<scriptsection.> Use dark colors for code blocks <head> <title>MapLibre ArcGIS tutorial: Find service areas</title> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"> <!-- Load MapLibre GL JS from CDN --> <script src="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.js"></script> <link href="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- Load MapLibre ArcGIS from CDN --> <script src="https://unpkg.com/@esri/maplibre-arcgis@1.1.0/dist/umd/maplibre-arcgis.min.js"></script> <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> <style> html, body, #map { padding: 0; margin: 0; height: 100%; width: 100%; font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #323232; } </style> </head>
Update the map
An arcgis/navigation basemap style is typically used in geocoding and routing applications. Update the basemap layer to use the style.
-
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 map = new maplibregl.Map({ container: "map", // the id of the div element zoom: 12, // starting zoom center: [100.5231, 13.7367] // Bangkok }); const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/navigation", token: accessToken });
Add a starting point layer
Add a source and a layer to show a white circle for the starting point of the calculation. This will show the user where they clicked.
-
Start a new function called
addStarting Point Layer() Use dark colors for code blocks center: [100.5231, 13.7367] // Bangkok }); const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/navigation", token: accessToken }); function addStartingPointLayer() { } -
Add a GeoJSON source with id
start. Set thedataattribute 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.
Use dark colors for code blocks function addStartingPointLayer() { map.addSource("start", { type: "geojson", data: { type: "FeatureCollection", features: [] } } -
Add a circle layer with id
start-circle, connected to thestartsource. Set the paint properties to make it white with a black outline.Use dark colors for code blocks function addStartingPointLayer() { map.addSource("start", { type: "geojson", data: { type: "FeatureCollection", features: [] } }); map.addLayer({ id: "start-circle", type: "circle", source: "start", paint: { "circle-radius": 6, "circle-color": "white", "circle-stroke-color": "black", "circle-stroke-width": 2 } }); }
Add service area layer
To show the service area, you will use a fill layer to display a GeoJSON source. Each polygon has a property called From 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
add.Service Area Layer() Use dark colors for code blocks center: [100.5231, 13.7367] // Bangkok }); const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/navigation", token: accessToken }); function addServiceAreaLayer() { } -
Add a GeoJSON source for the service area, with id
servicearea. Set thedataattribute 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.
Use dark colors for code blocks function addServiceAreaLayer() { map.addSource("servicearea", { type: "geojson", data: { type: "FeatureCollection", features: [] } }); } -
Add a layer of type
fillto display the service area polygons, connected to theserviceareasource. Use amatchexpression for thefill-colorto show each polygon in a different shade of blue: darkest for shortest time, and lightest for longest time.The
matchexpression is like aswitchstatement: it compares the value of theFromproperty of each polygon feature, and compares it against the values 0, 5 or 10. If it matches, the corresponding color becomes theBreak fill-color. Otherwise, the last value,transparentis used.Use dark colors for code blocks function addServiceAreaLayer() { map.addSource("servicearea", { type: "geojson", data: { type: "FeatureCollection", features: [] } }); map.addLayer({ id: "servicearea-fill", type: "fill", source: "servicearea", paint: { "fill-color": [ "match", ["get", "FromBreak"], 0, "hsl(210, 80%, 40%)", 5, "hsl(210, 80%, 60%)", 10, "hsl(210, 80%, 80%)", "transparent" ], "fill-outline-color": "black", "fill-opacity": 0.5 } }); }
Add a load event handler
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
loadevent. Call theaddandService Area Layer() addfunctions here.Starting Point Layer() 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.
Use dark colors for code blocks paint: { "circle-radius": 6, "circle-color": "white", "circle-stroke-color": "black", "circle-stroke-width": 2 } }); } map.on("load", () => { addServiceAreaLayer(); addStartingPointLayer(); });
Add a click handler
When you click on the map, you will update the location of the startingpoint source data to show the starting point location.
-
Add a click handler to the map.
Use dark colors for code blocks map.on("load", () => { addServiceAreaLayer(); addStartingPointLayer(); }); map.on("click", (e) => { }); -
Use the
lngproperty of the event parameter to construct a GeoJSON point. Update the geometry information of theLat startsource with it.The object passed to the
clickevent contains several useful properties, including:lng: the map location where the click took place.Lat point: the screen location in pixels where the click took place.original: the DOM event, which contains information about modifier keys.Event
For more information, see the MapLibre GL JS documentation.
Use dark colors for code blocks map.on("click", (e) => { const coordinates = e.lngLat.toArray(); const point = { type: "Point", coordinates }; map.getSource("start").setData(point); }); -
At the top right, click Run.
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 service function in the route service to get the service area.
-
Inside the click handler, create a new
arcgisto access the route service. Call theRest. Api Key Manager servicemethod. 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); arcgisRest .serviceArea({ authentication, facilities: [coordinates] })
Display the service area on the map
The response to the request contains the geographic information of the service areas. Use the sa property to update the servicearea source.
-
Use
Map.getto access theSource serviceareasource. Callsetto update the data with the GeoJSON returned from the API.Data Use dark colors for code blocks arcgisRest .serviceArea({ authentication, facilities: [coordinates] }) .then((response) => { map.getSource("servicearea").setData(response.saPolygons.geoJson); }); -
At the top right, click Run.
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.
What's next?
Learn how to use additional location services in these tutorials:


