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.
Prerequisites You need an ArcGIS Developer or ArcGIS Online account to access the developer dashboard and create an API key .
Steps Create a new pen To get started, either complete the Display a map tutorial or use this pen . Set the API key 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 api Key
to use your key.
Use dark colors for code blocks
Change line
1
2
3
4
const apiKey = "YOUR_API_KEY" ;
const basemapId = "ArcGIS:Streets" ;
const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
olms.apply(map, basemapURL);
Add references to ArcGIS REST JS In the <head>
element, add references to the ArcGIS REST JS library.
Expand
Use dark colors for code blocks
Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.15.1/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.15.1/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@10.5.0/dist/olms.js" type = "text/javascript" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js" > </ script >
< script src = "https://unpkg.com/@esri/arcgis-rest-routing@4.0.0/dist/bundled/routing.umd.js" > </ script >
Update the map A navigation basemap layer is typically used in geocoding and routing applications. Update the basemap layer to use ArcGIS: Navigation
.
Update the basemap and the map initialization to center on location [100.5231,13.7367]
, Bangkok.
Expand
Use dark colors for code blocks
Change line Change line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
< script >
const apiKey = "YOUR_API_KEY" ;
const map = new ol.Map({
target : "map"
});
const view = new ol.View({
center : ol.proj.fromLonLat([ 100.5231 , 13.7367 ]), // Bangkok
zoom : 12
});
map.setView(view);
const basemapId = "ArcGIS:Navigation" ;
const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
olms.apply(map, basemapURL).then( function ( map ) {
});
</ script >
Create a starting point layer Add a Vector Layer
to show the user where they clicked, with a white circle and black outline.
Add a function called add Starting Point Layer
. Inside, create a Vector Layer
layer. Use a Circle
style, with white Fill
and black Stroke
. Store the layer in a variable called starting Point Layer
;
Expand
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
map.setView(view);
let startingPointLayer;
function addStartingPointLayer ( ) {
startingPointLayer = new ol.layer.Vector({
style : new ol.style.Style({
image : new ol.style.Circle({
radius : 6 ,
fill : new ol.style.Fill({ color : "white" }),
stroke : new ol.style.Stroke({ color : "black" , width : 2 })
})
})
});
}
After creating the Vector Layer
, add it to the map using the add Layer
function.
Expand
Use dark colors for code blocks
Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
style : new ol.style.Style({
image : new ol.style.Circle({
radius : 6 ,
fill : new ol.style.Fill({ color : "white" }),
stroke : new ol.style.Stroke({ color : "black" , width : 2 })
})
})
});
map.addLayer(startingPointLayer);
window .s = startingPointLayer;
Create service area layer To show the service areas , you will use another Vector Layer
. Each service area feature has a property called From Break
which contains the lower bound of the number of minutes of travel: 0, 5 and 10 minutes. You will use a style function to choose a different shade of blue based on this property.
Add a function called add Service Area Layer()
. Store the layer as a variable called 'serviceAreaLayer'.
Expand
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
map.setView(view);
let serviceAreaLayer;
function addServiceAreaLayer ( ) {
serviceAreaLayer = new ol.layer.Vector({
});
}
Create a function which takes a feature and returns a fill style. Use the From Break
property to choose a shade of blue. Use a black stroke. Pass the style function to the Vector Layer
constructor.
Expand
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
let serviceAreaLayer;
function addServiceAreaLayer ( ) {
const style = ( feature ) => {
const fillColors = {
0 : "hsla(210, 80%, 40%, 50%)" ,
5 : "hsla(210, 80%, 60%, 50%)" ,
10 : "hsla(210, 80%, 80%, 50%)"
};
return new ol.style.Style({
fill : new ol.style.Fill({
color : fillColors[feature.get( "FromBreak" )]
}),
stroke : new ol.style.Stroke({
color : "black"
})
});
};
serviceAreaLayer = new ol.layer.Vector({
style : style
});
}
After creating the Vector Layer
, add it to the map using the add Layer
function.
Expand
Use dark colors for code blocks
Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
serviceAreaLayer = new ol.layer.Vector({
style : style
});
map.addLayer(serviceAreaLayer);
Display layers Use a load handler to display your layers when the map is ready.
Add a then
handler to the existing olms.apply(map, basemap URL)
call. Inside, call add Service Area Layer
and add Starting Point Layer
.
Expand
Use dark colors for code blocks
Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const basemapId = "ArcGIS:Navigation" ;
const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
olms.apply(map, basemapURL).then( function ( map ) {
addServiceAreaLayer();
addStartingPointLayer();
});
Get click location Before you call the Service area service , you need the location of the clicked point. You can use ol.proj.transform
to convert this into a latitude and longitude. Use the location to set a new Vector Source
of the starting Point Layer
.
Add a click handler to the map. Inside, convert the clicked coordinates from the event object into latitude and longitude.
Expand
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
olms.apply(map, basemapURL).then( function ( map ) {
addServiceAreaLayer();
addStartingPointLayer();
});
map.on( "click" , ( e ) => {
const coordinates = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
});
Create a new Vector Source
for starting Point Layer
, containing one feature. Construct this Feature
with a ol.geom.Point
, passing in the coordinate of the mouse click.
More info The mouse click is in the same coordinate system as the map, so you do not need to transform.
Expand
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const coordinates = ol.proj.transform(e.coordinate, "EPSG:3857" , "EPSG:4326" );
startingPointLayer.setSource(
new ol.source.Vector({
features : [ new ol.Feature( new ol.geom.Point(e.coordinate))]
})
);
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 longitude and latitude of the click event, you can now call the service Area
function in the route service to get the service area .
Inside the click handler, create a new arcgis Rest.Api Key
to access the route service. Call the argis Rest.service Area
with the transformed coordinates to calculate the service area .
In order to access ArcGIS location services with ArcGIS REST JS , you need an access token , either an API key or a token created by OAuth 2.0 authentication. API keys have scopes, so make sure to scope your API key with permissions to access the service.
More info 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 default Breaks
parameter.
Expand
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line. Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
startingPointLayer.setSource(
new ol.source.Vector({
features : [ new ol.Feature( new ol.geom.Point(e.coordinate))]
})
);
const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
arcgisRest
.serviceArea({
facilities : [coordinates],
authentication
})
Display the service area on the map The response to the request contains the geographic information of the service area s. Use the s a Polygons.geo Json
property to update the source of the service Area Layer
.
Add a then
handler. Inside, create a Geo JSON
feature format that projects from EPSG:4326 to EPSG:3857. Use its read Features
function to convert s a Polygons.geo Json
to an array of Feature s. Create a Vector Source
with these features, and call set Source
on service Area Layer
to apply it.
Expand
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
arcgisRest
.serviceArea({
facilities : [coordinates],
authentication
})
.then( ( response ) => {
const geojson = new ol.format.GeoJSON({
defaultDataProjection : "EPSG:4326" ,
featureProjection : "EPSG:3857"
});
const features = geojson.readFeatures(response.saPolygons.geoJson);
const source = new ol.source.Vector({ features : features });
serviceAreaLayer.setSource(source);
})
Add an error handler. Inside, show a message and write the error to the console.
Expand
Use dark colors for code blocks
Add line. Add line. Add line. Add line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
serviceAreaLayer.setSource(source);
})
.catch( ( error ) => {
alert( "There was a problem using the geocoder. See the console for details." );
console .error(error);
});
Run the app In CodePen , run your code to display the map.
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 ArcGIS location services in these tutorials: