Skip to main content

Search by Text

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.

User experience

This use case allows users quickly search for a place by its name, results can be limited to a region and sorted by proximity.


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.

example
Full example of Place Autocomplete integration

Learn how to integrate the Place Autocomplete with Maps SDK and Search UI components in a sample app.

chevron-right

Integration

Place Autocomplete 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.

guide
Installation guide

Before using any of Mapbox's Search products, you need to do some common installation steps. Follow this guide to complete installation, then continue with the steps below.

chevron-right
  1. The main entrypoint class is called PlaceAutocomplete, you will need to instantiate this class first:
val placeAutocomplete = PlaceAutocomplete.create(mapboxAccessToken)
  1. Retrieve autocomplete suggestions. There's only one function called suggestions() which has method overloadings for different use cases. As was mentioned before, the Place Autocomplete provides coroutine-based API, and suggestions() 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's lifecycleScope:
lifecycleScope.launchWhenCreated {
val response = placeAutocomplete.suggestions(query)
}
  • For the Search by name use case you will have to provide a String query:
val response = placeAutocomplete.suggestions("Washington DC")
  1. 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
}
  1. When a user has selected a suggestion, call select() function. This function returns PlaceAutocompleteResult object that provides detailed information about the place. Also, select() function ends billing session, so it's obligatory to call it every time your users select suggestions.
val selectedSuggestion = pickSuggestion(suggestions)

val selectionResponse = placeAutocomplete.select(selectedSuggestion)
selectionResponse.onValue { result ->
// process result
}.onError { e ->
// process error
}
glossary
API Reference

For more information, see the full API reference for the Place Autocomplete for Android.

chevron-right

Search by Text Pricing

There is no separate charge if you choose to use Mapbox Search SDK for Android to access our APIs, you only pay by the Search API usage.

Usage of the Search by Text is measured in monthly API sessions. Details about the number of API requests included in the free tier and the cost per request beyond what is included in the free tier are available on the pricing page.

Was this page helpful?