All docschevron-rightSearch SDK for Androidchevron-rightarrow-leftExampleschevron-rightReverse geocoding

Reverse geocoding

This example does not contain a UI. Results will be logged in Android Studio.

book
NOTE

This example is a part of the Search SDK for Android sample app. You can find the values for all referenced resources in the res directory. For example, see res/values/strings.xml for R.string.* references used in this example.

MainActivity.kt
package com.mapbox.search.sample.api
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.search.ResponseInfo
import com.mapbox.search.ReverseGeoOptions
import com.mapbox.search.SearchCallback
import com.mapbox.search.SearchEngine
import com.mapbox.search.SearchEngineSettings
import com.mapbox.search.common.AsyncOperationTask
import com.mapbox.search.result.SearchResult
import com.mapbox.search.sample.R
class ReverseGeocodingKotlinExampleActivity : AppCompatActivity() {
private lateinit var searchEngine: SearchEngine
private lateinit var searchRequestTask: AsyncOperationTask
private val searchCallback = object : SearchCallback {
override fun onResults(results: List<SearchResult>, responseInfo: ResponseInfo) {
if (results.isEmpty()) {
Log.i("SearchApiExample", "No reverse geocoding results")
} else {
Log.i("SearchApiExample", "Reverse geocoding results: $results")
}
}
override fun onError(e: Exception) {
Log.i("SearchApiExample", "Reverse geocoding error", e)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
searchEngine = SearchEngine.createSearchEngineWithBuiltInDataProviders(
SearchEngineSettings(getString(R.string.mapbox_access_token))
)
val options = ReverseGeoOptions(
center = Point.fromLngLat(2.294434, 48.858349),
limit = 1
)
searchRequestTask = searchEngine.search(options, searchCallback)
}
override fun onDestroy() {
searchRequestTask.cancel()
super.onDestroy()
}
}
Was this example helpful?