Learn how to search for coffee shops, gas stations, restaurants and other nearby places with the geocoding service .
Place addresses for businesses by category with the geocoding service. View in CodePen
Place finding is the process of searching for a place name or POI to find its address and location . You can use the geocoding service to find places such as coffee shops, gas stations, or restaurants for any geographic location around the world. You can search for places by name or by using categories. You can search near a location or you can search globally.
In this tutorial, you use ArcGIS REST JS to access the geocoding service and find places by place category.
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
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const apiKey = "YOUR_API_KEY" ;
const basemapEnum = "arcgis/streets" ;
const map = new maplibregl.Map({
container : "map" , // the id of the div element
style : `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/ ${basemapEnum} ?token= ${apiKey} ` ,
zoom : 12 , // starting zoom
center : [- 118.805 , 34.027 ] // starting location [longitude, latitude]
});
Add API references This tutorial uses ArcGIS REST JS for place finding.
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
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1,maximum-scale=1,user-scalable=no" />
< script src = https://unpkg.com/maplibre-gl@3.2.1/dist/maplibre-gl.js > </ script >
< link href = https://unpkg.com/maplibre-gl@3.2.1/dist/maplibre-gl.css rel = "stylesheet" />
< 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-geocoding@4.0.0/dist/bundled/geocoding.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 ;
}
.places {
position : absolute;
top : 20px ;
left : 20px ;
}
</ style >
</ head >
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 [-122.4194,37.7749]
, San Francisco.
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
< script >
const apiKey = "YOUR_API_KEY" ;
const basemapEnum = "arcgis/navigation" ;
const map = new maplibregl.Map({
container : "map" ,
style : `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/ ${basemapEnum} ?token= ${apiKey} ` ,
zoom : 13 ,
center : [- 122.4194 , 37.7749 ] // San Francisco
});
Add controls Use a select
element to create the drop-down element. Each category is an option
element inside it. The select
element needs to have absolute
positioning in order to appear in front of the map element.
Add a select control, with options for "Coffee shop", "Gas station", "Food", "Hotel", "Parks and outdoors". Use inline styling to position the control.
The option values have special meaning to the geocoding service so ensure the spelling is correct. To learn more, go to Category filtering in the ArcGIS services reference.
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
< body >
< div id = "map" > </ div >
< select id = "places-select" style = "left: 20px; top: 20px; position: absolute; font-size: 16px; padding: 4px 8px" >
< option value = "" > Choose a category... </ option >
< option value = "Coffee shop" > Coffee shops </ option >
< option value = "Gas station" > Gas stations </ option >
< option value = "Food" > Food </ option >
< option value = "Hotel" > Hotels </ option >
< option value = "Parks and Outdoors" > Parks and Outdoors </ option >
</ select >
At the top right, click Run to verify that the select
control is created and contains the different categories.
Add a load event handler You need to wait for the map to be completely loaded before adding any layers.
Add an event handler to the map load
event.
Expand
Use dark colors for code blocks
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
< script >
const apiKey = "YOUR_API_KEY" ;
const basemapEnum = "arcgis/navigation" ;
const map = new maplibregl.Map({
container : "map" ,
style : `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/ ${basemapEnum} ?token= ${apiKey} ` ,
zoom : 13 ,
center : [- 122.4194 , 37.7749 ] // San Francisco
});
map.once( "load" , () => {
});
</ script >
Add a map layer To display data on the map, there must be a source that contains the data, and a layer that determines how the source will be shown.
Add a geojson
source that will contain the returned results. Set its data
attribute to be an empty GeoJSON Feature Collection
.
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
map.once( "load" , () => {
map.addSource( "places" , {
type : "geojson" ,
data : {
type : "FeatureCollection" ,
features : []
}
});
});
Add a circle
layer that will display the source.
More info The geojson
source must have data provided. You can give it a Feature Collection
that does not contain any features. You will provide data later.
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.
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
map.once( "load" , () => {
map.addSource( "places" , {
type : "geojson" ,
data : {
type : "FeatureCollection" ,
features : []
}
});
map.addLayer({
id : "places-circle" ,
source : "places" ,
type : "circle" ,
paint : {
"circle-color" : "hsla(200, 80%, 80%, 0.7)" ,
"circle-stroke-color" : "hsl(200, 80%, 40%)" ,
"circle-stroke-width" : 1 ,
"circle-radius" : 3
}
});
});
Call the geocoding service You can use the arcgis Rest.geocode
function to retrieve places near a location . Use map.get Center
to obtain the center of the viewport in longitude and latitude.
You will also pass the category selected by the user. Use document.get Element By I d
to get the control, then use its value
property.
Create a function called show Places
. Inside, create a new arcgis Rest.Api K e y Manager
to access the geocoding service . Call arcgis Rest.geocode()
to set the API key and to call the service endpoint. A location
with Place_ addr
and Place Name
information will be returned based on the category
selected. A maximum of 25 locations will display nearest to the center of the map.
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.
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. 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
paint : {
"circle-color" : "hsla(200, 80%, 80%, 0.7)" ,
"circle-stroke-color" : "hsl(200, 80%, 40%)" ,
"circle-stroke-width" : 1 ,
"circle-radius" : 3
}
});
});
function showPlaces ( ) {
const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
const category = document .getElementById( "places-select" ).value;
if (!category) {
return ;
}
arcgisRest
.geocode({
authentication,
outFields : "Place_addr,PlaceName" ,
params : {
category,
location : map.getCenter().toArray().join( "," ),
maxLocations : 25
}
})
}
Display results The geo Json
property of the response object contains a GeoJSON feature collection of points. You can use this to update the data of your places
source .
Add a response handler. Inside, use the map's set Data
method to set the returned value as GeoJSON.
Expand
Use dark colors for code blocks
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
params : {
category,
location : map.getCenter().toArray().join( "," ),
maxLocations : 25
}
})
.then( ( response ) => {
map.getSource( "places" ).setData(response.geoJson);
})
Handle errors It is good practice to handle situations where there is a problem accessing the service. This could happen due to network disruption or a problem with your API key, for instance.
If there is an exception accessing the geocoding service , alert the user and log the error.
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
.then( ( response ) => {
map.getSource( "places" ).setData(response.geoJson);
})
.catch( ( error ) => {
alert( "There was a problem using the geocoder. See the console for details." );
console .error(error);
});
}
Add a change event handler To update the map when the user chooses a category, you must add an event handler for the change
event on the <select>
control.
Update the load event handler to call the show Places()
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
map.addLayer({
id : "places-text" ,
source : "places" ,
type : "symbol" ,
layout : {
"text-field" : [ "get" , "PlaceName" ],
"text-font" : [ "Arial Bold" ],
"text-variable-anchor" : [ "left" , "right" ],
"text-justify" : "left" ,
"text-radial-offset" : 0.5
},
paint : {
"text-color" : "hsl(200, 80%,40%)" ,
"text-halo-color" : "white" ,
"text-halo-width" : 2
}
});
showPlaces();
After the map initialization code, make the <select>
control control call show Places()
when the user chooses an option.
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
.catch( ( error ) => {
alert( "There was a problem using the geocoder. See the console for details." );
console .error(error);
});
}
document .getElementById( "places-select" ).addEventListener( "change" , showPlaces);
At the top right, click Run . When you choose a place category, circles should be shown for places.
Display place labels You can show the name of each location by using a symbol
layer with text properties.
In the load handler, add a symbol
layer.
More info It is important to use a font whose glyphs are present within the map style. This style contains several variants of Arial
, which makes that a safe choice.
By default, MapLibre GL JS centers text labels over their center point. Since there is a circle showing already, it is better to treat the point as the left
anchor, and also set a text-justify
of `left.
When labels are close together, not all will be shown. You could change the default settings of text-allow-overlap
if you want to alter this behavior.
For more information about available style properties, see the MapLibre GL JS documentation .
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. 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
map.addLayer({
id : "places-circle" ,
source : "places" ,
type : "circle" ,
paint : {
"circle-color" : "hsla(200, 80%, 80%, 0.7)" ,
"circle-stroke-color" : "hsl(200, 80%, 40%)" ,
"circle-stroke-width" : 1 ,
"circle-radius" : 3
}
});
map.addLayer({
id : "places-text" ,
source : "places" ,
type : "symbol" ,
layout : {
"text-field" : [ "get" , "PlaceName" ],
"text-font" : [ "Arial Bold" ],
"text-variable-anchor" : [ "left" , "right" ],
"text-justify" : "left" ,
"text-radial-offset" : 0.5
},
paint : {
"text-color" : "hsl(200, 80%,40%)" ,
"text-halo-color" : "white" ,
"text-halo-width" : 2
}
});
showPlaces();
Run the app In CodePen , run your code to display the map.
When you choose a place category, you should see the name of each place shown as a label, next to a white circle with a blue outline.
What's next? Learn how to use additional ArcGIS location services in these tutorials: