Beta
Search SDK for Android

Search

Public beta

The Mapbox Search SDK for Android is in public beta. All features and workflows are subject to change.

Search engines help users find specific addresses, points of interest (POIs), and places. The Mapbox Search SDK for Android offers three types of search engines:

Forward geocoding is likely what comes to mind when you think about a typical search experience: for the given String query a user gets geographic coordinates of an address or place. For example, you can type in Lincoln Memorial and retrieve the geographic location of a place that matches that query (-77.050,38.889).

Reverse geocoding is an opposite operation of forward geocoding: for the given geographic coordinates a user gets place names or addresses. For example, turning -77.050, 38.889 into 2 Lincoln Memorial Circle NW address.

Category search allows you to select a category (for example, restaurants) and retrieve a list of POIs that belong to that category. For example, if you want to display many restaurants within walking distance of a theater on a map, you can use category search with the restaurants category.

Forward geocoding

The Mapbox Search SDK for Android provides a SearchEngine class for executing forward geocoding search requests. After you’ve initialized a MapboxSearchSdk object, you can obtain an instance of the engine via the MapboxSearchSdk.getSearchEngine() method.

val searchEngine = MapboxSearchSdk.getSearchEngine()
example
Forward geocoding

Forward geocoding allows you to search for places by name.

Forward geocoding in the Search SDK consists of two steps: suggestions search and suggestions selection.

Restricted search options in private beta

Options marked with the @Reserved(SBS) annotation are available in private beta. If you're interested in using these options, contact sales to enable access. If you try to use these options without access, SearchEngine will ignore them.

When you provide a String query and set of options, SearchOptions, to SearchEngine.search(), it returns a list of suggestions List<SearchSuggestion> that contain information about an address or a place (including name, categories, description, and type).

val searchRequestTask = searchEngine.search(
    "Paris Eiffel Tower",
    SearchOptions(limit = 5),
    searchCallback
)

Resulting suggestions will be passed to the SearchSuggestionsCallback.onSuggestions() callback:

val searchCallback = object : SearchSuggestionsCallback {
    override fun onSuggestions(suggestions: List<SearchSuggestion>, responseInfo: ResponseInfo) {
        val suggestion = suggestions.firstOrNull()
    }

    override fun onError(e: Exception) {
    }
}

Additional information, like geographic coordinates of a place, is not available at this step. To get more information about a suggestion you need to make a suggestion selection.

Suggestion selection

To retrieve more information for a specific suggestion, use the SearchEngine.select() method:

val selectRequestTask = searchEngine.select(suggestion, selectCallback)

The result of the select operation will be passed to one of the SearchSelectionCallback callback functions:

val selectCallback = object : SearchSelectionCallback {
    override fun onResult(
        suggestion: SearchSuggestion,
        result: SearchResult,
        responseInfo: ResponseInfo
    ) {
    }

    override fun onSuggestions(suggestions: List<SearchSuggestion>, responseInfo: ResponseInfo) {
    }

    override fun onCategoryResult(
        suggestion: SearchSuggestion,
        results: List<SearchResult>,
        responseInfo: ResponseInfo
    ) {
    }

    override fun onError(e: Exception) {
    }
}

Depending on the type of suggestion, SearchSuggestion.type, different selection logic is applied:

Suggestion typeResult of select operationDescription
SearchResultSuggestionSearchResultSuggestion has a place or address associated with it. Search result contains information about geographic coordinates.

Source of information: server
Result of operation is passed to: SearchSelectionCallback.onResult()
Category
Private beta
List<SearchResult>Suggestion indicates that the SDK found a category name for category search. Selection of such suggestion results in a category search request with the result being list of POIs. Each search result contains information about geographic coordinates.

Source of information: server
Result of operation is passed to: SearchSelectionCallback.onCategoryResult()
Query
Private beta
List<SearchSuggestion>Suggestion indicates that a user has likely misspelled a query during the first step of forward geocoding. Selection of such suggestion results in the same search request from the first step with a changed query. The result of the operation is new list of suggestions.

Source of information: server
Result of operation is passed to: SearchSelectionCallback.onSuggestions()
IndexableRecordItemSearchResultSuggestion has a place or address associated with it. Search result contains information about geographic coordinates.

Source of information: data provider (read more in the Data providers guide)
Result of operation is passed to: SearchSelectionCallback.onResult()

If the passed suggestion is of the type SearchResultSuggestion or IndexableRecordItem, then you receive information about coordinates in the SearchSelectionCallback.onResult() method:

val selectCallback = object : SearchSelectionCallback {
    override fun onResult(
        suggestion: SearchSuggestion,
        result: SearchResult,
        responseInfo: ResponseInfo
    ) {
        val coordinates = result.coordinate
    }

    override fun onSuggestions(suggestions: List<SearchSuggestion>, responseInfo: ResponseInfo) {
    }

    override fun onCategoryResult(
        suggestion: SearchSuggestion,
        results: List<SearchResult>,
        responseInfo: ResponseInfo
    ) {
    }

    override fun onError(e: Exception) {
    }
}
Automatic history record creation

A successfully completed SearchEngine.select() method adds HistoryRecord, created from SearchResult, to HistoryDataProvider by default (read more about history data provider in the Data Providers guide). If you want to turn off the default functionality, specify extra parameter options: SelectOptions with the following options:

val options = SelectOptions(addResultToHistory = false)
val selectRequestTask = searchEngine.select(suggestion, options, selectCallback)

Batch suggestions selection

You can also select multiple suggestions at once with the SearchEngine.select() method. The only requirements are:

  • All suggestions must originate from the same search request
  • All suggestions should be allowed for batch selection

Invoke SearchSuggestion.isBatchResolveSupported to check if a suggestion can be used for batch selection.

val batchSuggestions = suggestions.filter { it.isBatchResolveSupported }
val selectRequestTask = searchEngine.select(batchSuggestions, multipleSelectCallback)

Provided results will be passed to the SearchMultipleSelectionCallback.onResult() callback.

val multipleSelectCallback = object : SearchMultipleSelectionCallback {
    override fun onResult(
        suggestions: List<SearchSuggestion>,
        results: List<SearchResult>,
        responseInfo: ResponseInfo
    ) {
    }

    override fun onError(e: Exception) {
    }
}
example
Batch search

Forward geocoding with batch selection allows you to get several search results using fewer requests.

Reverse geocoding

The Mapbox Search SDK for Android provides a ReverseGeocodingSearchEngine class for making reverse geocoding search requests. After you’ve initialized a MapboxSearchSdk object, you can obtain an instance of the engine via the MapboxSearchSdk.getReverseGeocodingSearchEngine() method.

val reverseEngine = MapboxSearchSdk.getReverseGeocodingSearchEngine()

When you provide a Point location and set of options, ReverseGeoOptions, to ReverseGeocodingSearchEngine, it returns a list of places, addresses, and POIs List<SearchResult>.

val searchRequestTask = searchEngine.search(
    ReverseGeoOptions(
        center = Point.fromLngLat(2.294434, 48.858349)
    ),
    reverseSearchCallback
)

Resulting suggestions will be passed to the SearchCallback.onResults() callback:

val reverseSearchCallback = object : SearchCallback {
    override fun onResults(results: List<SearchResult>, responseInfo: ResponseInfo) {
    }

    override fun onError(e: Exception) {
    }
}
example
Reverse geocoding

Reverse geocoding allows you to enter a geographic coordinate and receive the name of one or more places that exist at that location.

The Mapbox Search SDK for Android provides a CategorySearchEngine class for making category search requests. After you’ve initialized a MapboxSearchSdk object, you can obtain an instance of the engine via the MapboxSearchSdk.getCategorySearchEngine() method.

val categorySearchEngine = MapboxSearchSdk.getCategorySearchEngine()
Restricted search options in private beta

Options marked with the @Reserved(SBS) annotation are available in private beta. If you're interested in using these options, contact sales to enable access. If you try to use these options without access, CategorySearchEngine will ignore them.

When you provide a String query and set of options, CategorySearchOptions, to CategorySearchEngine, it returns a list of POIs List<SearchResult>. You can find a list of common category names in the Mapbox Geocoding API documentation.

val searchRequestTask = categorySearchEngine.search(
    "cafe",
    CategorySearchOptions(limit = 1),
    categorySearchCallback
)

Resulting suggestions will be passed to the SearchCallback.onResults() callback:

val categorySearchCallback = object : SearchCallback {
    override fun onResults(results: List<SearchResult>, responseInfo: ResponseInfo) {
    }

    override fun onError(e: Exception) {
    }
}
example
Category search

Category search allows you to look for places that belong to a specified category.