Place Autocomplete
The Place Autocomplete SDK for Android is in a private preview. Contact sales team if you are interested in the Place Autocomplete SDK.
The Place Autocomplete returns place suggestions in response to user search queries, such as addresses, businesses and POIs. The Place Autocomplete will help you to implement such functionality in a few lines of code.
Place Autocomplete use cases
- Search by name. This use case allows users quickly search for a place by its name, results can be limited to a region and sorted by proximity.
- Search by coordinate. This use case allows users to search for places by the coordinate and pick appropriate from the returned suggestions.
We have a ready-made sample app that you can try in Android Studio with Place Autocomplete and other scenarios. We encourage you to install and try it for yourself.
Learn how to integrate the Place Autocomplete SDK with Maps SDK and Search UI components in a sample app.
Integration
Place Autocomplete SDK provides coroutine-based API. In the following integration steps, we assume that you are familiar with the Kotlin coroutines and your Android project has Kotlin-ktx
dependencies added. See the Kotlin coroutines guide for more information.
Before using any of Mapbox's Search products, you need to do some common installation steps. Please follow this guide, then continue with the steps below.
- The main entrypoint class is called
PlaceAutocomplete
, you will need to instantiate this class first:
val placeAutocomplete = PlaceAutocomplete.create(mapboxAccessToken)
- Retrieve autocomplete suggestions. There's only one function called
suggestions()
which has overloadings for different use cases. As was mentioned before, the Place Autocomplete SDK provides coroutine-based API, andsuggestions()
is a suspend function which should be called only from a coroutine or another suspend function. In this short example we will assume that we call it from Activity'slifecycleScope
:
lifecycleScope.launchWhenCreated {
val response = placeAutocomplete.suggestions(query)
}
The function parameters will vary depending on the use case.
- For the Search by name use case you will have to provide a String query:
val response = placeAutocomplete.suggestions("Washington DC")
- For the Search by coordinate use case you will have to provide geographic position represented by the
Point
class:
val response = placeAutocomplete.suggestions(Point.fromLngLat(-77.03399849939174, 38.89992081005698))
- Process the Place Autocomplete response. The suggestions() function returns either error or suggestions list:
response.onValue { suggestions: List<PlaceAutocompleteSuggestion> ->
processSuggestions(suggestions)
}.onError { e ->
// process error
}
For more information, see the full API reference for the Place Autocomplete SDK for Android.