Search with autosuggest

Learn how to search for an address with autosuggest using the geocoding service.

Search for addresses with autosuggest using API key authentication

Autosuggest, also known as autocomplete, is a feature that predicts and displays location-based suggestions as a user types into a search box. In this tutorial, you use ArcGIS REST JS to access the geocoding service. You also create an input control to accept text and suggest address results. When a suggestion is selected, a pop-up will appear with the name, location, and address, and the map will pan to it.

Prerequisites

Steps

Get the starter app

Select a type of authentication below and follow the steps to create a new application.

  1. If you are using the CDN libraries, to get started.

Set up authentication

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.

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s):
    • Privileges:
      • Location services > Geocoding
  2. Copy the API key access token to your clipboard when prompted.

Set developer credentials

Use the API key or OAuth developer credentials so your application can access location services.

  1. 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";
    

Make the request

Copy and paste the code below, following the steps to make a request to the Geocoding service.

  1. Reference the arcgis-rest-request and arcgis-rest-geocoding libraries either through CDN, ES Modules, or Node JS.

  2. (Optional) Add input controls to display address suggestions as the user types.

  3. Call arcgisRest.suggest to return address suggestions based on the partial input text.

  4. Retrieve the magicKey from the selected suggestion and call arcgisRest.geocode to get the location of the address.

  5. Display the geocoding results.

Expand
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
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";
      });
    });
Expand

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:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.