Create a Search Experience

Now you have simple app showing a map. In this step, you'll create a simple search and display the search results in a list. You'll also learn how to filter the data displayed on the map based on the search results.

Start by creating a new activity or fragment to facilitate searches on your application. Here we will be using a fragment for search and show to search results on, while using a bottom sheet to display the results. We also create a search input field on our main map activity for the user to input the text they want to search for. This is already setup in the basic example app.

To perform a search you will need to have initiated MapsIndoors. This was shown in the previous section of the getting started tutorial how you do this.

For advanced usage of the search functionality read the Search guide and tutorials connected to it: Search Guidearrow-up-right

Show a List of Search Resultsarrow-up-right

Create a search method that takes a search string as a parameter on your MapsActivity class. In this example we only use the setTake on the MPFilter to limit our result to 30 locations. We will expand on this method later.

  • Java

  • Kotlin

MapsActivity.javaarrow-up-right

void search(String searchQuery) {
    //Query with a string to search on
    MPQuery mpQuery = new MPQuery.Builder().setQuery(searchQuery).build();
    //Filter for the search query, only taking 30 locations
    MPFilter mpFilter = new MPFilter.Builder().setTake(30).build();
    //Query for the locations
    MapsIndoors.getLocationsAsync(mpQuery, mpFilter, (list, miError) -> {
      //Implement UI handling of the search result here
    });
}

To be able to search we will use a text input field where a user can write what they want to search for. This is placed at the top of the MapsActivity

To call our search method with the text in the search input field, we then add an EditorActionListener and a OnClickListener to the text input field and the search button in the onCreate of MapsActivity. Find the full onCreate example here: MapsActivity.javaarrow-up-right or MapsActivity.ktarrow-up-right

  • Java

  • Kotlin

MapsActivity.javaarrow-up-right

Find the full onCreate example here: MapsActivity.javaarrow-up-right or MapsActivity.ktarrow-up-right

To accompany this we use the SearchFragment that is already created for you and a BottomSheet to handle the SearchFragment.

Observe that the SearchFragmentis just a simple fragment with a RecyclerView and a SearchItemAdapter added to it

  • Java

  • Kotlin

SearchFragment.javaarrow-up-right

See the full example of SearchFragment here: SearchFragment.javaarrow-up-right or SearchFragment.ktarrow-up-right

Create a getter for your MapControl object on the MapsActivity so that it can be used in the SearchAdapter.

  • Java

  • Kotlin

MapsActivity.javaarrow-up-right

Inside the SearchItemAdapter implement logic to display the locations you get from a search result. Here we show an image of the location marker and show the name of the locations.

  • Java

  • Kotlin

SearchItemAdapter.javaarrow-up-right

See the full example of SearchItemAdapter and accompanying ViewHolder here: SearchItemAdapter.javaarrow-up-right or SearchItemAdapter.ktarrow-up-right

We have already implemented the BottomSheet in the UI. Now we add the search fragment to the BottomSheet in our search query method on our MapsActivity. You can use the addFragmentToBottomSheet too add the created fragment to the BottomSheet. When we have received the search results

  • Java

  • Kotlin

MapsActivity.javaarrow-up-right

See the full example of the search method here: MapsActivity.javaarrow-up-right or MapsActivity.ktarrow-up-right

Filter Locations on Map Based on Search Resultsarrow-up-right

When getting a search result, you might want to only show those search results on the map. You can do this through calling displaySearchResults(List<MPLocation> locations) on MapControl. This method has different parameters to make it easier for you as a developer to fit your exact need in terms of animation and more. This can be read in the JavaDoc of MapControlarrow-up-right.

The standard implementation animates the camera to fit all Locations on the map and show the info window of a Location, if it's a list of only one Location.

When you are done showing the search results you can call clearMap() on MapControl.

Since the default displaySearchResults(List<MPLocation> locations) uses camera animation we will call it from the UI Thread and implement it in our search method inside the getLocationsAsync result with the list from the method.

  • Java

  • Kotlin

MapsActivity.javaarrow-up-right

Expected result:

An animation showing the desired behaviour of this tutorial

The accompanying UI and implementation of this search experience can be found in the getting started app sample. Getting Started App samplearrow-up-right or Getting Started App sample kotlinarrow-up-right.

arrow-up-right

Last updated

Was this helpful?