👨🏼‍💻 Android | Live Location App Using Huawei Location Kit

Besir Karaoglu
Huawei Developers
Published in
5 min readNov 5, 2022

Introduction

Hello,

In this article, we will develop an Android application where we can instantly share locations and see the shared locations from the users who have the application by using the HMS Core and AppGallery Connect services developed by Huawei.

Libraries Used In This Project

  1. Auth Service
  2. Cloud DB
  3. Map Kit
  4. Location Kit

About the Libraries

Auth Service, helps you quickly create a secure and reliable user authentication system for your application by integrating cloud-based Auth Service features directly into your application.

Cloud DB, is a device-to-cloud synergy database product that provides data synergy management capabilities between device and cloud, unified data models, and various data management APIs.

Map Kit, provides powerful and convenient map services for you to easily implement personalized map display and interaction.

Location Kit, helps you build global positioning capabilities by providing capabilities to obtain user device location precisely and quickly.

Note: We skip some integration steps for the simplicity of this post. If you have not used these services before, you can review the links under the References heading for information about these steps or for the steps you think are missing.

Coding

We will use 4 libraries while developing this application. Let’s integrate them first.

Then we need to make the necessary definitions in the AndroidManifest.xml file. So inside the manifest tag:

We add permission tags. These tags provide the permissions we need when using Map Kit and Location Kit. Then, we add the necessary Service and BroadcastReceiver classes to the <application> tag in order to get the location and not disconnect the location when the application is in the background.

After completing AndroidManifest.xml, we add Activities. In this application, we will use LoginActivity to perform AuthService operations and MainActivity for other functions. Let’s create these 2 Activities.

Activities

Auth Service offers a variety of user login options. In this project, we will use Auth Service’s login with Huawei ID. In this way, users will be able to enter data into Cloud DB as well as access information such as uid, name, and photo without the need to collect information from users. Let’s edit the UI of LoginActivity first.

Then let’s add the necessary codes to the LoginActivity.kt file. What we need to do here is to check whether login has been made before using the AGConnectAuth class, and if not, to do the necessary actions for login. Let’s add the following codes to LoginActivity.

After this process, we completed the user login process. Now let’s add the Map Kit components to show the locations on the map.

First for the ui:

In this section, we have added 2 components. MapView will help us show locations on the map. On the other hand, the switch will ensure that the location sharing is handled by the user.

Now let’s determine our Data Class for Cloud DB and write our insert, delete and query functions.

First of all, let’s enter the AGC Console and add our Object Type from Cloud DB. Then let’s export and integrate it into our project.

Object Type

If you haven’t done it before, you can add it by following this link.

Next, let’s create a class called CloudDBWrapper and add our query functions.

We added our functions. All that’s left is the methods needed to get the position.

At the beginning of the article, we added BroadcastReceiver to the manifest file. Let’s create a class called LSSReceiver and add the necessary methods. This class will allow us to get the location of the device in the background and add it to the Cloud DB.

Finally, we will add the necessary methods for the UI in MainActivity. First, let’s add the following codes to our project to manage MapView.

Let’s initialize the Map Kit and call this method in our Activity’s onCreate() method.

Next, let’s add the necessary method to add markers to the map.

In order to get location information on Android, we need to ask the user for permission. To request these permissions, let’s add the following method and call it in the onCreate() method.

In order to use Location Kit, we need to check the permissions. For this, let’s add the following code and call it in the onCreate() method.

For this case, it is enough to see the logs only, but for other cases, you can use OnSuccessListener.

After checking the Location settings, let’s initialize the Fused Location. Then we will use this variable with the switch.

Now, let’s add a listener to the Switch and check the location request status using the variable we added.

We added 3 new methods above. These methods are key methods that allow us to get location. Let’s add these methods as well.

Here we created the LocationRequest variable while calling the requestLocationUpdatesWithIntent() method. The values in this variable can change according to the application's purpose. You can follow this link for more detailed information.

Finally, listening for changes in Cloud DB. For this, let’s add the following method and call it in the onCreate() method.

Thus, we have finished our application. Here are, screenshots of the app:

Screenshots

Conclusion

In this article, we have developed an application where we can share live locations on the map and see other users sharing live locations using Auth Service, Cloud DB, Map Kit, and Location Kit. You can take a look at the references section to access the source code of the project and to have more information about the services used.

Good day, see you in the next articles :)

References

--

--