Find address

View inJavaKotlinView on GitHubSample viewer app

Find the location for an address.

Image of find address

Use case

A user can input a raw address into your app's search bar and zoom to the address location.

How to use the sample

Type in an address in the search view at the top of the screen. Suggestions will appear as text is entered. Tap a suggestion or enter your own text to see the address marked with a pin. Tapping on the pin will show the address in a callout.

How it works

  1. Create a LocatorTask using the URL to a locator service.
  2. Set the GeocodeParameters for the locator task and specify the geocode's attributes.
  3. Get the matching results from the GeocodeResult using locatorTask.geocodeAsync(addressString, geocodeParameters).
  4. Create a Graphic with the geocode result's location and store the geocode result's attributes in the graphic's attributes.
  5. Show the graphic in a GraphicsOverlay.

Relevant API

  • GeocodeParameters
  • GeocodeResult
  • LocatorTask

Additional information

This sample uses the World Geocoding Service. For more information, see the Geocoding service help topic on the ArcGIS Developer website.

Tags

address, geocode, locator, search

Sample Code

MainActivity.java
Use dark colors for code blocksCopy
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* Copyright 2017 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.esri.arcgisruntime.sample.findaddress;

import android.database.MatrixCursor;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.cursoradapter.widget.SimpleCursorAdapter;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Callout;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.IdentifyGraphicsOverlayResult;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
import com.esri.arcgisruntime.tasks.geocode.SuggestResult;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

  private final String TAG = MainActivity.class.getSimpleName();
  private final String COLUMN_NAME_ADDRESS = "address";
  private final String[] mColumnNames = { BaseColumns._ID, COLUMN_NAME_ADDRESS };
  private SearchView mAddressSearchView;

  private MapView mMapView;
  private LocatorTask mLocatorTask;
  private GraphicsOverlay mGraphicsOverlay;
  private GeocodeParameters mAddressGeocodeParameters;
  private PictureMarkerSymbol mPinSourceSymbol;
  private Callout mCallout;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    // inflate address search view
    mAddressSearchView = findViewById(R.id.addressSearchView);
    mAddressSearchView.setIconified(false);
    mAddressSearchView.setFocusable(false);
    mAddressSearchView.setQueryHint(getResources().getString(R.string.address_search_hint));

    // define pin drawable
    BitmapDrawable pinDrawable = (BitmapDrawable) ContextCompat.getDrawable(this, R.drawable.pin);
    try {
      mPinSourceSymbol = PictureMarkerSymbol.createAsync(pinDrawable).get();
    } catch (InterruptedException | ExecutionException e) {
      Log.e(TAG, "Picture Marker Symbol error: " + e.getMessage());
      Toast.makeText(getApplicationContext(), "Failed to load pin drawable.", Toast.LENGTH_LONG).show();
    }
    // set pin to half of native size
    mPinSourceSymbol.setWidth(19f);
    mPinSourceSymbol.setHeight(72f);

    // create a LocatorTask from an online service
    mLocatorTask = new LocatorTask("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");

    // inflate MapView from layout
    mMapView = findViewById(R.id.mapView);
    // create a map with the BasemapType topographic
    final ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);
    map.setInitialViewpoint(new Viewpoint(40,-100,100000000));

    // once the map has loaded successfully, set up address finding UI
    map.addDoneLoadingListener(() -> {
      if (map.getLoadStatus() == LoadStatus.LOADED) {
        setupAddressSearchView();
      } else {
        Log.e(TAG, "Map failed to load: " + map.getLoadError().getMessage());
        Toast.makeText(
                getApplicationContext(),
                "Map failed to load: " + map.getLoadError().getMessage(),
                Toast.LENGTH_LONG
        ).show();
      }
    });

    // set the map to be displayed in this view
    mMapView.setMap(map);
    // set the map viewpoint to start over North America
    mMapView.setViewpoint(new Viewpoint(40, -100, 100000000));

    // add listener to handle screen taps
    mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
      @Override
      public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        identifyGraphic(motionEvent);
        return true;
      }
    });

    // define the graphics overlay
    mGraphicsOverlay = new GraphicsOverlay();

  }

  /**
   * Sets up the address SearchView. Uses MatrixCursor to show suggestions to the user as the user inputs text.
   */
  private void setupAddressSearchView() {

    mAddressGeocodeParameters = new GeocodeParameters();
    // get place name and address attributes
    mAddressGeocodeParameters.getResultAttributeNames().add("PlaceName");
    mAddressGeocodeParameters.getResultAttributeNames().add("Place_addr");
    // return only the closest result
    mAddressGeocodeParameters.setMaxResults(1);
    mAddressSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

      @Override
      public boolean onQueryTextSubmit(String address) {
        // geocode typed address
        geoCodeTypedAddress(address);
        // clear focus from search views
        mAddressSearchView.clearFocus();
        return true;
      }

      @Override
      public boolean onQueryTextChange(String newText) {
        // as long as newText isn't empty, get suggestions from the locatorTask
        if (!newText.equals("")) {
          final ListenableFuture<List<SuggestResult>> suggestionsFuture = mLocatorTask.suggestAsync(newText);
          suggestionsFuture.addDoneListener(new Runnable() {

            @Override public void run() {
              try {
                // get the results of the async operation
                List<SuggestResult> suggestResults = suggestionsFuture.get();
                MatrixCursor suggestionsCursor = new MatrixCursor(mColumnNames);
                int key = 0;
                // add each address suggestion to a new row
                for (SuggestResult result : suggestResults) {
                  suggestionsCursor.addRow(new Object[] { key++, result.getLabel() });
                }
                // define SimpleCursorAdapter
                String[] cols = new String[] { COLUMN_NAME_ADDRESS };
                int[] to = new int[] { R.id.suggestion_address };
                final SimpleCursorAdapter suggestionAdapter = new SimpleCursorAdapter(MainActivity.this,
                    R.layout.find_address_suggestion, suggestionsCursor, cols, to, 0);
                mAddressSearchView.setSuggestionsAdapter(suggestionAdapter);
                // handle an address suggestion being chosen
                mAddressSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
                  @Override public boolean onSuggestionSelect(int position) {
                    return false;
                  }

                  @Override public boolean onSuggestionClick(int position) {
                    // get the selected row
                    MatrixCursor selectedRow = (MatrixCursor) suggestionAdapter.getItem(position);
                    // get the row's index
                    int selectedCursorIndex = selectedRow.getColumnIndex(COLUMN_NAME_ADDRESS);
                    // get the string from the row at index
                    String address = selectedRow.getString(selectedCursorIndex);
                    // use clicked suggestion as query
                    mAddressSearchView.setQuery(address, true);
                    return true;
                  }
                });
              } catch (Exception e) {
                Log.e(TAG, "Geocode suggestion error: " + e.getMessage());
              }
            }
          });
        }
        return true;
      }
    });
  }

  /**
   * Identifies the Graphic at the tapped point.
   *
   * @param motionEvent containing a tapped screen point
   */
  private void identifyGraphic(MotionEvent motionEvent) {
    // get the screen point
    android.graphics.Point screenPoint = new android.graphics.Point(Math.round(motionEvent.getX()),
        Math.round(motionEvent.getY()));
    // from the graphics overlay, get graphics near the tapped location
    final ListenableFuture<IdentifyGraphicsOverlayResult> identifyResultsFuture = mMapView
        .identifyGraphicsOverlayAsync(mGraphicsOverlay, screenPoint, 10, false);
    identifyResultsFuture.addDoneListener(new Runnable() {
      @Override public void run() {
        try {
          IdentifyGraphicsOverlayResult identifyGraphicsOverlayResult = identifyResultsFuture.get();
          List<Graphic> graphics = identifyGraphicsOverlayResult.getGraphics();
          // if a graphic has been identified
          if (!graphics.isEmpty()) {
            //get the first graphic identified
            Graphic identifiedGraphic = graphics.get(0);
            showCallout(identifiedGraphic);
          } else {
            // if no graphic identified
            mCallout.dismiss();
          }
        } catch (Exception e) {
          Log.e(TAG, "Identify error: " + e.getMessage());
        }
      }
    });
  }

  /**
   * Shows the Graphic's attributes as a Callout.
   *
   * @param graphic containing attributes
   */
  private void showCallout(final Graphic graphic) {
    // create a TextView for the Callout
    TextView calloutContent = new TextView(getApplicationContext());
    calloutContent.setTextColor(Color.BLACK);
    // set the text of the Callout to graphic's attributes
    if (graphic.getAttributes().get("PlaceName").toString().isEmpty()) {
      calloutContent.setText(graphic.getAttributes().get("Place_addr").toString());
    } else {
      calloutContent.setText(graphic.getAttributes().get("PlaceName") + "\n"
          + graphic.getAttributes().get("Place_addr"));
    }
    // get Callout
    mCallout = mMapView.getCallout();
    // set Callout options: animateCallout: true, recenterMap: false, animateRecenter: false
    mCallout.setShowOptions(new Callout.ShowOptions(true, false, false));
    mCallout.setContent(calloutContent);
    // set the leader position and show the callout
    // set the leader position and show the callout
    Point calloutLocation = graphic.computeCalloutLocation(graphic.getGeometry().getExtent().getCenter(), mMapView);
    mCallout.setGeoElement(graphic, calloutLocation);
    mCallout.show();
  }

  /**
   * Geocode an address passed in by the user.
   *
   * @param address read in from searchViews
   */
  private void geoCodeTypedAddress(final String address) {
    // check that address isn't null
    if (address != null) {

      // Execute async task to find the address
      mLocatorTask.addDoneLoadingListener(new Runnable() {
        @Override
        public void run() {
          if (mLocatorTask.getLoadStatus() == LoadStatus.LOADED) {
            // Call geocodeAsync passing in an address
            final ListenableFuture<List<GeocodeResult>> geocodeResultListenableFuture = mLocatorTask
                .geocodeAsync(address, mAddressGeocodeParameters);
            geocodeResultListenableFuture.addDoneListener(new Runnable() {
              @Override
              public void run() {
                try {
                  // Get the results of the async operation
                  List<GeocodeResult> geocodeResults = geocodeResultListenableFuture.get();
                  if (geocodeResults.size() > 0) {
                    displaySearchResult(geocodeResults.get(0));
                  } else {
                    Toast.makeText(getApplicationContext(), getString(R.string.location_not_found) + address,
                        Toast.LENGTH_LONG).show();
                  }
                } catch (InterruptedException | ExecutionException e) {
                  Log.e(TAG, "Geocode error: " + e.getMessage());
                  Toast.makeText(getApplicationContext(), getString(R.string.geo_locate_error), Toast.LENGTH_LONG)
                      .show();
                }
              }
            });
          } else {
            Log.i(TAG, "Trying to reload locator task");
            mLocatorTask.retryLoadAsync();
          }
        }
      });
      mLocatorTask.loadAsync();
    }
  }

  /**
   * Turns a GeocodeResult into a Point and adds it to a GraphicOverlay which is then drawn on the map.
   *
   * @param geocodeResult a single geocode result
   */
  private void displaySearchResult(GeocodeResult geocodeResult) {
    // dismiss any callout
    if (mMapView.getCallout() != null && mMapView.getCallout().isShowing()) {
      mMapView.getCallout().dismiss();
    }
    // clear map of existing graphics
    mMapView.getGraphicsOverlays().clear();
    mGraphicsOverlay.getGraphics().clear();
    // create graphic object for resulting location
    Point resultPoint = geocodeResult.getDisplayLocation();
    Graphic resultLocGraphic = new Graphic(resultPoint, geocodeResult.getAttributes(), mPinSourceSymbol);
    // add graphic to location layer
    mGraphicsOverlay.getGraphics().add(resultLocGraphic);
    // zoom map to result over 3 seconds
    mMapView.setViewpointAsync(new Viewpoint(geocodeResult.getExtent()), 3).addDoneListener(() -> showCallout(resultLocGraphic));
    // set the graphics overlay to the map
    mMapView.getGraphicsOverlays().add(mGraphicsOverlay);

  }

  @Override
  protected void onPause() {
    super.onPause();
    mMapView.pause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mMapView.dispose();
  }
}

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