Search

The Mappedin SDK for Android provides a way to search for locations and categories using their names, descriptions and tags. The search is performed locally on the device, meaning it can be used when a device is offline.

A complete class that uses the code snippets in this guide can be found in the Mappedin Android Github repo: Search.kt

Methods for Searching

MPISearchManager offers two different methods to perform a search, MPISearchManager.search and MPISearchManager.suggest.

The search method is used to search for locations and categories based on a query string. Results are returned as a List containing MPIOfflineSearchResultLocations and MPIOfflineSearchResultCategorys, which both extend from MPIOfflineSearchResultCommon.

The suggest method is used to generate suggested search terms for a given query string and returns a List of MPIOfflineSearchSuggestions that contains a String with the suggestion.

Understanding Search Results

Search results contain a score and details about how it was matched. When displaying the results to a user, the results should be sorted to show the highest score first.

Code Example

Below is a code sample that performs the following operations:

  1. Performs a search using the String query.
  2. Filters the results that contain locations (MPIOfflineSearchResultLocation), removing categories (MPIOfflineSearchResultCategory).
  3. Filters locations that contain the type of “tenant”. Note that location types will be unique to each venue and are chosen by the map administrator.
  4. Sorts the results by score with the highest score first.
mapView.searchManager.search(query) { results ->
val searchLocations = results
// Filter the results that contain locations.
?.filterIsInstance<MPIOfflineSearchResultLocation>()
// Filter locations that contain the type of “tenant”.
?.filter { it.location.type == "tenant" }
// Sort the results by score with the highest score first.
?.sortedByDescending { it.score }
}

A complete class that uses the code snippets in this guide can be found in the Mappedin Android Github repo: Search.kt

Was this page helpful?

Next Topic

Outdoor View