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.
Last, add permissions on AndroidManifest.xml and we’re all set.
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.
requestPermissions(PERMISSIONS, PERMISSION_ALL)
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 the user’s device. Define two global double variables to hold lat and lng information. We will use these variables to get sites.
For nearby place parameters, set a string list in a spinner.
You can add more place options according to HMS Site kit HwLocationTypes below:
Location Kit
In onViewCreated method set an adapter for the spinner,
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.
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.
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.
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
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.
Last, add permissions on AndroidManifest.xml and we’re all set.
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.
requestPermissions(PERMISSIONS, PERMISSION_ALL)
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 the user’s device. Define two global double variables to hold lat and lng information. We will use these variables to get sites.
For nearby place parameters, set a string list in a spinner.
You can add more place options according to HMS Site kit HwLocationTypes below:
Location Kit
In onViewCreated method set an adapter for the spinner,
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.
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.
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.
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