Find Near & Around places based on your location: HMS Site, Location, Map Kit

BusraGungor

New member
Jan 22, 2021
1
0
1
r
1_fnd5m4ei66qa3zgukwe_yg_optimized.png

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 the user’s location information with the Location kit. According to the location, we will search nearby places with one of the site kit’s features. Then we will display the received data on the map.

Get User's Location
First, you need to register as a Huawei Developer and complete the verification process. For detailed information please refer to the link below.
Register as a Developer

In AppGallery Connect console create a project and enable the Map, Site, and Location kits. You can follow the steps below:
AppGalleryConnect

To complete the implementation of kits we need to add dependencies on build.gradle file.
x1.png


Last, add permissions on AndroidManifest.xml and we’re all set.
x2.png


We need a permissionHandler class to manage location permissions.

x3.png

x4.png

x5.png


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.

x6.png

requestPermissions(PERMISSIONS, PERMISSION_ALL)

Create a FusedLocationProviderClient instance in onViewCreated() method :
x7.png


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.
x8.png


Now we have latitude and longitude information of the user’s device. Define two global double variables to hold lat and lng information. We will use these variables to get sites.
x9.png


For nearby place parameters, set a string list in a spinner.
x10.png


You can add more place options according to HMS Site kit HwLocationTypes below:
Location Kit

In onViewCreated method set an adapter for the spinner,
x11.png


We will mark sites on the map according to the changed spinner item, first, we need to create a getSites() method to receive site information.

x12.png



In this method, we request site data for the current location with the HwLocationType parameter that we received from the spinner onItemSelectedListener. And we marked each site location on the map with distance information. You can change NearbySearchRequest() parameters in your own way. For example, I set a radius of 5000 to get sites around 5K km from the user’s device location. But the only hwPoiType needs to be a dynamic value. Let’s set the dynamic value.

First, initialize map in onViewCreated method and extend OnMapReadyCallback.
x13.png


Once you extend the map callback to the fragment, it asks you to add onMapReady method. In this method set the onItemSelected of the spinner.

x14.png


Conclusion

So we get the user’s device's last known location, asked the user to choose a place to search for sites and we marked the received places on the map.

References
Demo Project