Find address

View inJavaKotlin
View on GitHub
Sample 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

For simplicity, the sample comes loaded with a set of suggested addresses. Choose an address from the suggestions list above the search view, or type in an address in the search view. Suggestions will appear as text is entered. Tap a suggestion or submit your own address to show its location on the map 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

About the data

This sample uses the World Geocoding Service.

Tags

address, geocode, locator, search

Sample Code

MainActivity.kt
                                                                                                                                                                                                                                                                                                                                                           
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
/*
 * Copyright 2020 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.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
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.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.symbology.PictureMarkerSymbol
import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters
import com.esri.arcgisruntime.tasks.geocode.GeocodeResult
import com.esri.arcgisruntime.tasks.geocode.LocatorTask
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.ExecutionException
import kotlin.math.roundToInt

class MainActivity : AppCompatActivity() {

  private val TAG: String = MainActivity::class.java.simpleName
  private var callout: Callout? = null
  private var addressGeocodeParameters: GeocodeParameters? = null
  // create a picture marker symbol
  private val pinSourceSymbol: PictureMarkerSymbol? by lazy { createPinSymbol() }
  // create a locator task from an online service
  private val locatorTask: LocatorTask by lazy { LocatorTask(getString(R.string.locator_task_uri)) }
  // create a new Graphics Overlay
  private val graphicsOverlay: GraphicsOverlay by lazy { GraphicsOverlay() }

  override fun onCreate(savedInstanceState: Bundle?) {
    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)

    // create a map with the streets vector basemap type
    val topographicMap = ArcGISMap(BasemapStyle.ARCGIS_STREETS)

    mapView.apply {
      // set the map to be displayed in the mapview
      map = topographicMap
      // set the map viewpoint to start over North America
      setViewpoint(Viewpoint(40.0, -100.0, 100000000.0))
      // define the graphics overlay and add it to the map view
      graphicsOverlays.add(graphicsOverlay)
      // add listener to handle screen taps
      onTouchListener = object : DefaultMapViewOnTouchListener(this@MainActivity, mapView) {
        override fun onSingleTapConfirmed(motionEvent: MotionEvent): Boolean {
          identifyGraphic(motionEvent)
          return true
        }
      }
    }

    // create the picture marker symbol to show address location
    createPinSymbol()

    // populate the spinner list of address suggestions
    val examples = arrayOf("277 N Avenida Caballeros, Palm Springs, " +
        "CA", "380 New York St, Redlands, CA 92373", "Београд", "Москва", "北京")
    // initialize an adapter for the suggestions spinner
    val suggestionAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, examples)
    suggestionSpinner.adapter = suggestionAdapter

    // when an item is selected in the spinner set, go to that address
    suggestionSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
      override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        geoCodeTypedAddress(suggestionSpinner.selectedItem.toString())
      }
      override fun onNothingSelected(parent: AdapterView<*>) {}
    }

    setupAddressSearchView()
  }

  /**
   * Sets up the address SearchView and uses MatrixCursor to show suggestions to the user as text is entered.
   */
  private fun setupAddressSearchView() {
    addressGeocodeParameters = GeocodeParameters().apply {
      // get place name and street address attributes
      resultAttributeNames.addAll(listOf("PlaceName", "Place_addr"))
      // return only the closest result
      maxResults = 1

      addressSearchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(address: String): Boolean {
          // geocode typed address
          geoCodeTypedAddress(address)
          // clear focus from search views
          addressSearchView.clearFocus()
          return true
        }

        override fun onQueryTextChange(newText: String): Boolean {
         // if the newText string isn't empty, get suggestions from the locator task
          if (newText.isNotEmpty()){
            val suggestionsFuture = locatorTask.suggestAsync(newText)
            suggestionsFuture.addDoneListener{
              try {
                // get the results of the async operation
                val suggestResults = suggestionsFuture.get()
                // set up parameters for searching with MatrixCursor
                val address = "address"
                val columnNames = arrayOf(BaseColumns._ID, address)
                val suggestionsCursor = MatrixCursor(columnNames)

                // add each address suggestion to a new row
                for ((key, result) in suggestResults.withIndex()) {
                  suggestionsCursor.addRow(arrayOf<Any>(key, result.label))
                }
                // column names for the adapter to look at when mapping data
                val cols = arrayOf(address)
                // ids that show where data should be assigned in the layout
                val to = intArrayOf(R.id.suggestion_address)
                // define SimpleCursorAdapter
                val suggestionAdapter = SimpleCursorAdapter(this@MainActivity,
                  R.layout.suggestion, suggestionsCursor, cols, to, 0)

                addressSearchView.suggestionsAdapter = suggestionAdapter
                // handle an address suggestion being chosen
                addressSearchView.setOnSuggestionListener(object: SearchView.OnSuggestionListener{
                  override fun onSuggestionSelect(position: Int): Boolean { return false }
                  override fun onSuggestionClick(position: Int): Boolean {
                    // get the selected row
                    (suggestionAdapter.getItem(position) as? MatrixCursor)?.let { selectedRow ->
                      // get the row's index
                      val selectedCursorIndex = selectedRow.getColumnIndex(address)
                      // get the string from the row at index
                      val selectedAddress = selectedRow.getString(selectedCursorIndex)
                      // use clicked suggestion as query
                      addressSearchView.setQuery(selectedAddress, true)
                    }
                    return true
                  }
                })
              } catch (e: Exception) {
                Log.e(TAG, "Geocode suggestion error: " + e.message)
                Toast.makeText(applicationContext, "Geocode suggestion error", Toast.LENGTH_LONG)
                  .show()
              }
            }
          }
          return true
        }
      })
    }
  }

  /**
   * Geocode an address passed in by the user.
   *
   * @param address the address read in from searchViews
   */
  private fun geoCodeTypedAddress(address: String) {
    locatorTask.addDoneLoadingListener {
      if (locatorTask.loadStatus == LoadStatus.LOADED) {
        // run the locatorTask geocode task, passing in the address
        val geocodeResultFuture =
          locatorTask.geocodeAsync(address, addressGeocodeParameters)
        geocodeResultFuture.addDoneListener {
          try {
            // get the results of the async operation
            val geocodeResults = geocodeResultFuture.get()
            if (geocodeResults.isNotEmpty()) {
              displaySearchResultOnMap(geocodeResults[0])
            } else {
              Toast.makeText(
                applicationContext, getString(R.string.location_not_found) + address, Toast.LENGTH_LONG).show()
            }
          } catch (e: Exception) {
            when (e) {
              is ExecutionException, is InterruptedException -> {
                Log.e(TAG, "Geocode error: " + e.message)
                Toast.makeText(applicationContext, getString(R.string.geo_locate_error), Toast.LENGTH_LONG)
                  .show()
              }
              else -> throw e
            }
          }
        }
      } else {
        locatorTask.retryLoadAsync()
      }
    }
    locatorTask.loadAsync()
  }

  /**
   * Turns a GeocodeResult into a Point and adds it to a graphic overlay on the map.
   *
   * @param geocodeResult a single geocode result
   */
  private fun displaySearchResultOnMap(geocodeResult: GeocodeResult) {
    // clear graphics overlay of existing graphics
    graphicsOverlay.graphics.clear()
    // create graphic object for resulting location
    val resultPoint = geocodeResult.displayLocation
    val resultLocationGraphic = Graphic(resultPoint, geocodeResult.attributes, pinSourceSymbol)
    // add graphic to location layer
    graphicsOverlay.graphics.add(resultLocationGraphic)
    mapView.setViewpointAsync(Viewpoint(geocodeResult.extent), 1f)
    showCallout(resultLocationGraphic)
  }

  /**
   * Identifies and shows a call out on a tapped graphic.
   *
   * @param motionEvent the motion event containing a tapped screen point
   */
  private fun identifyGraphic(motionEvent: MotionEvent) {
    // get the screen point
    val screenPoint: android.graphics.Point = android.graphics.Point(
      motionEvent.x.roundToInt(), motionEvent.y.roundToInt()
    )
    // from the graphics overlay, get the graphics near the tapped location
    val identifyResultsFuture: ListenableFuture<IdentifyGraphicsOverlayResult> =
      mapView.identifyGraphicsOverlayAsync(graphicsOverlay, screenPoint, 10.0, false)
    identifyResultsFuture.addDoneListener{
      try {
        val identifyGraphicsOverlayResult: IdentifyGraphicsOverlayResult = identifyResultsFuture.get()
        val graphics = identifyGraphicsOverlayResult.graphics
        // get the first graphic identified
        if (graphics.isNotEmpty()) {
          val identifiedGraphic: Graphic = graphics[0]
          // show the callout of the identified graphic
          showCallout(identifiedGraphic)
        } else {
          // dismiss the callout if no graphic is identified (e.g. tapping away from the graphic)
          callout?.dismiss()
        }
      } catch (e: Exception) {
        Log.e(TAG, "Identify error: " + e.message)
      }
    }
  }

  /**
   * Shows the given graphic's attributes as a call out.
   *
   * @param graphic the graphic containing the attributes to be displayed
   */
  private fun showCallout(graphic: Graphic) {
    // create a text view for the callout
    val calloutContent = TextView(applicationContext).apply {
      setTextColor(Color.BLACK)
      // get the graphic attributes for place name and street address, and display them as text in the callout
      this.text = if (graphic.attributes["PlaceName"].toString().isNotEmpty()) {
        graphic.attributes["PlaceName"].toString() + "\n" + graphic.attributes["Place_addr"].toString()
      } else {
        graphic.attributes["Place_addr"].toString()
      }
    }
    // get the center of the graphic to set the callout location
    val centerOfGraphic = graphic.geometry.extent.center
    val calloutLocation = graphic.computeCalloutLocation(centerOfGraphic, mapView)

    callout = mapView.callout.apply {
      showOptions = Callout.ShowOptions(true, true, true)
      content = calloutContent
      // set the leader position using the callout location
      setGeoElement(graphic, calloutLocation)
      // show callout beneath graphic
      style.leaderPosition = Callout.Style.LeaderPosition.UPPER_MIDDLE
      // show the callout
      show()
    }
  }

  /**
   *  Creates a picture marker symbol from the pin icon, and sets it to half of its original size.
   */
  private fun createPinSymbol(): PictureMarkerSymbol? {
    val pinDrawable = ContextCompat.getDrawable(this, R.drawable.pin) as BitmapDrawable?
    val pinSymbol: PictureMarkerSymbol
    try {
      pinSymbol = PictureMarkerSymbol.createAsync(pinDrawable).get()
      pinSymbol.width = 19f
      pinSymbol.height = 72f
      return pinSymbol
    } catch (e: Exception) {
      when (e) {
        is ExecutionException, is InterruptedException -> {
          Log.e(TAG, "Picture Marker Symbol error: " + e.message)
          Toast.makeText(applicationContext, "Failed to load pin drawable.", Toast.LENGTH_LONG)
            .show()
        }
        else -> throw e
      }
    }
    return null
  }

  override fun onResume() {
    super.onResume()
    mapView.resume()
  }

  override fun onPause() {
    mapView.pause()
    super.onPause()
  }

  override fun onDestroy() {
    mapView.dispose()
    super.onDestroy()
  }
}

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