Develop Location Based Applications with Huawei Mobile Services — 3

Mustafa Sürücü
Huawei Developers
Published in
4 min readJul 10, 2020

Hi friends,

In the third article of the series, I will explain HMS Site Kit Place Search Suggestion and Keyword Search functions to show how you may use these services in your Android application easily.

If you have any questions about how you can integrate HMS Core into your project, please take a look at below post before beginning.

We examined HMS Site Kit Nearby Search and Map Kit Directions API in previous articles of the series. If you have not read yet, you can reach them from the links below.

Place Search Suggestion enables to display suggested places in real time according to user input.

With Keyword Search function, user can specify keywords, coordinate bounds, and other information to search for places as tourist attractions or enterprises.

Let’s start by creating a search bar as a sample for our application.

Search Bar

AutoCompleteTextView is used in our layout to show place search suggestion results. Now we will start to use Site Kit functions by creating SearchService, QuerySuggestionRequest and TextSearchRequest objects.

Note: “API Key” is required to use search service. You can find it in agconnect-services.json file.

When users enter some inputs, our mSearchText object should listen text change status and call Place Search Suggestion service according to specified criteria. In our demo, If user enters more than 2 letters we will call autoComplete() method and show suggestions to the user.

In autoComplete() method, we will prepare suggestion request body and send this via searchService object. After we got the suggestionResults, I will give all information about suggested places to an adapter object which is called “searchAdapter”. This adapter will help us to fetch and use suggestion results.

I want to show suggestion results in a listview after user enters some keywords. To do that another adapter is created inside autoComplete() method which provides a listview layout and an array list that contains names and addresses of places.

Note: Location of sugRequest has been set as my current location thanks to FusedLocationProviderClient which explained in first article.

List Suggestion Results with AutoCompleteTextView

As you see above picture when user enters inputs TextChangeListener is triggered and autoComplete() method is called. Suggestion results have been shown with simple_list_item_1 layout thanks to our adapter.

To make our adapter more usable, we can add FocusChangeListener to mSearchText. In this way, adapter will be cleared when user deletes old keyword and enter a new keyword. After that, we will add ItemClicklistener to select one of the suggested places.

When user choose one of the suggested options, updateCamera() method has been called. It expects a LatLng object, zoom, name and addresses of selected place to animate camera towards it and to display related information.

Congrats !! We showed related information and location of selected place on map.

Move Camera to Selected Place
Move Camera to Selected Place

Users may want to search for a keyword instead of choosing a suggested location. We will use an EditorActionlistener to activate this function in our demo application. When user enters a keyword and press enter, search() method will be called.

In search() method, we will set parameters of textSearchRequest which is defined in the beginning and send this request with our searchService object. searchAdapter will be used again to keep text search results. For all item of the adapter we will call our updateCamera method and display all results on map together.

Display all Results of Keyword Search

In this article, I explained Place Search Suggestion and Keyword Search services of HMS Site Kit over a demo application.

I hope that it will guide you to enhance features of your applications.

Thank you !

--

--