Find Near & Around places based on your location: HMS Site, Location, Map Kit
Hello, in this story I will give you an example of how to combine HMS Site, Location, and Map Kits. This application will allow you to display nearby restaurants, schools, shopping centers, etc. on the map based on your current location.
First, will get user’s location information with Location kit. According to the location we will search nearby places with one of the site kit’s feature. Then we will display the received data on map.
HMS Integration
First, you need to register as a Huawei Developer and complete the verification process. For detailed information please refer to the link below.
In AppGallery Connect console create a project and enable the Map, Site, and Location kits. You can follow the steps below :
To complete the implementation of kits we need to add dependencies on build.gradle file.
Last, add permissions on AndroidManifest.xml and we’re all set.
Get User’s Location
First, we need a permissionHandler class to manage location permissions.
In this app, I used the Android Navigation Component, that’s why all transactions will be in the MainFragment.
To get user’s permission for location, call requestPermissions() method in onViewCreated(). We need 3 runtime permissions for location, so declare an array of permissions.
Create a FusedLocationProviderClient instance in onViewCreated() method :
Then call the getLastLocation() method right after instance. This method provides us user’s last known location. In most cases, the last known location is the current location of the device.
Now we have latitude and longitude information of user’s device. Define two global double variables to hold lat and lng information. We will use these variables to get sites.
Select Place Parameter and Get Sites
In this app scenario, user is allowed to select parameter of nearby places from Spinner in MainFragment.xml. So we need a spinner and a map in main screen of our application.
Accordingly here is an example of XML file :
For nearby place parameters, set string list in a spinner.
You can add more place options according to HMS Site kit HwLocationTypes below:
In onViewCreated method set an adapter for spinner,
We will mark sites on map according to changed spinner item, first we need to create a getSites() method to receive site information.
In this method, we request site data for current location with HwLocationType parameter that we received from spinner onItemSelectedListener. And we marked each site location on map with distance information. You can change NearbySearchRequest() parameters in your own way. For example, i set radius 5000 to get sites around 5K km from user’s device location. But only hwPoiType needs to be a dynamic value. Let’s set the dynamic value.
First initialize map in onViewCreated method and extend OnMapReadyCallback.
Once you extend map callback to the fragment, it asks you to add onMapReady method. In this method set the onItemSelected of the spinner
So we get the user’s device last known location, asked the user choose a place to search for sites and we marked the received places on map.
Hope to see you in the next article.