Learn how to access the geocoding service.
Search for an address using API key authentication
Search for an address using user authenticationIn this tutorial, you find the location of an address using an input control that has autosuggest enabled.
Mapping and location services guideTo learn more about the Geocoding service, go to Geocoding.
PrerequisitesAn ArcGIS Location Platform or ArcGIS Online account.
Steps Get the starter appSelect a type of authentication below and follow the steps to create a new application.
Create developer credentials in your portal for the type of authentication you selected.
Create a new API key credential with the correct privileges to access the resources used in this tutorial.
Create a new OAuth credential to register the application.
To use this application, users must have an ArcGIS account with the necessary privileges to access all resources and services.
Set developer credentialsUse the API key or OAuth developer credentials so your application can access location services.
Update the accessToken
variable to use your API key.
Use dark colors for code blocks
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
const accessToken = "YOUR_ACCESS_TOKEN";
In both the index.html
and callback.html
files, replace YOUR_CLIENT_ID
and YOUR_REDIRECT_URL
with the client ID and redirect URL of your OAuth credentials.
index.html
Use dark colors for code blocks
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
/* Use for user authentication */
const clientId = "YOUR_CLIENT_ID"; // Your client ID from OAuth credentials
const redirectUri = "YOUR_REDIRECT_URI"; // The redirect URL registered in your OAuth credentials
const authentication = await arcgisRest.ArcGISIdentityManager.beginOAuth2({
clientId,
redirectUri,
portal: "https://www.arcgis.com/sharing/rest" // Your portal URL
})
callback.html
Use dark colors for code blocks
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
arcgisRest.ArcGISIdentityManager.completeOAuth2({
clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials
redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials
portal: "https://www.arcgis.com/sharing/rest" // Your portal URL
})
Run the app and ensure you can sign in successfully.
If you are unable to sign in, make sure you have the correct redirect URL and port. This URL varies based on your application and typically takes the format of https://<server>[:port]/callback.html
or http://my-arcgis-app:/auth
. For example, if you are running an application on http://127.0.0.1:5500/
, set http://127.0.0.1:5500/callback.html
as your redirect URL in the index.html and callback.html file and your developer credential. They all have to match!
Copy and paste the code below, following the steps to make a request to the Geocoding service.
Reference the arcgis-rest-request
and arcgis-rest-geocoding
libraries either through CDN, ES Modules, or Node JS.
Define the parameters needed for the request.
Make a request to the Geocoding service and handle the results.
Expand
Use dark colors for code blocks1
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
function performGeocode(magicKey) {
arcgisRest.geocode({
magicKey,
authentication
}).then((response) => {
console.log("Autosuggest results:", response);
result.textContent = JSON.stringify(response, null, 2);
});
}
input.addEventListener("input", () => {
selectedIndex = -1;
const text = input.value;
if (!text) {
suggestionsList.style.display = "none";
return;
}
arcgisRest.suggest(text, {
authentication,
params: {
location: {
x: -123.102,
y: 49.234,
spatialReference: { wkid: 4326 }
},
maxSuggestions: 5
}
}).then((response) => {
suggestionsList.innerHTML = "";
if (!response.suggestions || response.suggestions.length === 0) {
suggestionsList.style.display = "none";
return;
}
response.suggestions.forEach((s) => {
const li = document.createElement("li");
li.textContent = s.text;
li.addEventListener("click", () => {
input.value = s.text;
suggestionsList.style.display = "none";
performGeocode(s.magicKey)
});
suggestionsList.appendChild(li);
});
suggestionsList.style.display = "block";
});
});
Run the app
Run the app.
The result should look similar to
this.
What's next?Learn how to use additional ArcGIS Location Services in these tutorials:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4