HUAWEI Location Kit — Manage Location from single class

Yusuf Ceylan
Huawei Developers
Published in
4 min readSep 8, 2020

Location is essential for many apps and developers need services for getting location data. Huawei Location Kit helps developers in enabling their apps to get quick and accurate user locations and expand global positioning capabilities by using GPS, Wi-Fi, and base station locations.

First of all, we have to get a Huawei Developer Account and create a project from Huawei App Gallery Console but since this article is about to Location Kit, we will not cover these topics.

You can refer developer account documentation for creating account, and follow this codelab for creating a project at App Gallery Console.

After enabling Location Kit from Manage API section at console, we need to add Location Kit dependency.

implementation 'com.huawei.hms:location:5.0.0.301'

After sync project, we are ready to code.

We will cover necessary methods one by one then collect them in a single class for the sake of maintainable and clean code.

First we have to add permissions to our app

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

If app needs to continuously locate the device when it runs in the background in Android Q, apply for the ACCESS_BACKGROUND_LOCATION

After applying for permissions in the AndroidManifest file, apply for the permissions dynamically in the code (according to requirements for dangerous permissions in Android 6.0).

The location function of Huawei Location Kit depends on the device location settings. If location disabled we can not obtain the device location so it is better to check location setting before obtaining the location.

Check Location Settings

private val mSettingsClient: SettingsClient by lazy {
LocationServices.getSettingsClient(context)
}

SettingsClient instance allow us to call checkLocationSettings method for obtain to device location settings.

If the location settings do not meet the requirements, we are showing a system dialog for request location permission and catch result in related Activity or Fragment

After successfully enable device location, we can use location related APIs

private val mFusedLocationProviderClient: FusedLocationProviderClient by lazy {
LocationServices.getFusedLocationProviderClient(context)
}

We are creating FusedLocationProviderClient instance first. We will use it to call location-related APIs like location availability, last known location, register and unregister location updates.

Location Availability

For obtaining location availability from Huawei Location Services, we call locationAvailability method of FusedLocationProviderClient instance and listen success and failure callbacks.

Last Known Location

Just like location availability, we just call lastLocation method of FusedLocationProviderClient instance and listen success and failure callbacks.

If the obtained device location is null, the possible cause is that the system cache has been cleared. In this case, we need to call the requestLocationUpdates method to obtain the device location and update the cache, and then call the lastLocation method to obtain the device location.

Location Updates

For request location updates, we have to create a LocationCallback. This callback will be triggered every time when device get new location.

Also we have to specify location request conditions. In order to do that we create a LocationRequest.

val mLocationRequest = LocationRequest()
// Set the location update interval (in milliseconds).
mLocationRequest.interval = 10000
// Set the weight.
mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

If we want to obtain location once only we can set numUpdates.

// Get location update only once
mLocationRequest.numUpdates = 1

Finally we can start listening location updates by calling requestLocationUpdates method of FusedLocationProviderClient.

If app does not need to receive location updates anymore, stop the location update function to reduce power consumption. To do this, call the removeLocationUpdates method, and pass the LocationCallback.

LocationCallback must be same callback that we used at requestLocationUpdates method.

We have covered necessary location related methods, now lets collect them in a single manager class. In this way we can easily separate logic from view and class will be more maintainable

You can see whole class from this gist

In Activity or Fragment, we can easily use this manager class.

val locationManager = HuaweiLocationManager(this)locationManager.registerLocationUpdates(onSuccess = {
viewModel.updateCurrentLocation(it)
})

We can see location updates in logcat

--

--

Huawei Developers
Huawei Developers

Published in Huawei Developers

As Huawei Developers, our Medium publication where we share information about the use of Huawei Ecosystem