Location Kit — Using Mock Location and Getting Location Update for One Time Only

Ali Mert Özhayta
Huawei Developers
Published in
5 min readNov 9, 2020

This article will provide faster integration of Location Kit and will address how to get your current location for one time only instead of getting continuous updates within an interval and how to change your current location to any where in the world by using Mock Location.

HUAWEI Location Kit combines GPS, Wi-Fi, and base station location data to allow app developers to quickly obtain precise user locations.

Integration Preparations

Let’s get started by adding Maven repository and dependencies to the app.

  • add Huawei Maven repository to project’s build.gradle file.
repositories {
...
maven {url 'https://developer.huawei.com/repo/'}
...
}


allprojects {
repositories {
...
maven {url 'https://developer.huawei.com/repo/'}
...

}
}
  • add Location Kit to app’s build.gradle file.
dependencies {
...

implementation 'com.huawei.hms:location:5.0.2.301'
}
  • add permissions to AndroidManifest.xml file. (inside of manifest tag)
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  • Location Kit does not require App Gallery Connect services to operate. So, if you only want to try Location Kit, you don’t need to create an app on AG Connect or do not need to download agconnect-services.json file. Adding graddle dependencies will be enough to use Location Kit.

Development

To drastically reduce number of lines on your activity/fragment and to integrate Location Service easier to your project, you can use the following HMSLocationService class or modify it according to your project’s needs.

HMSLocationService class will provide a fast solution without inflating your activity/fragment by checking and handling location permissions, creating and handling location request and callback. If the location is not available for that moment, the service will try to return the last known location of the user.

Initializing HMSLocationService to get current location for one time or to get continuous location updates.

// initialize on activity or on fragment
val locationService = HMSLocationService(this)
// to get location for one time, initialize with parameter
val locationService = HMSLocationService(this, isContinuous=false)

if isContinuous parameter is set to false, location request’s number of updates parameter will be set to 1 to get the current location for one time that means that the Location Service will update the location of the user for one time only.

mLocationRequest = LocationRequest()// set the priority of the request
mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
// set number of updates
mLocationRequest?.numUpdates = 1

Location Permission

HMSLocationService class will provide a fast solution without inflating your activity/fragment by checking and handling location permission. When you initialize the class, it will check the permissions. To request permissions, please override onRequestPermissionsResult method by adding the following line on your activity or fragment:

Overriding onRequestPermissionsResult method on Activity or Fragment
Request Location Permission

Handling Location Updates

You can handle location updates by setting a callback to setLocationCallback method of the object that is created by HMSLocationService class.

Requesting Location Update

After setting a callback to handle received location update, we can request the location update from Location Service by calling requestLocationUpdatesWithCallback method.

Removing Location Update

If continuous location updates is not needed any more, you can stop receiving location updates by calling removeLocationUpdatesWithCallback method.

Congratulations !! You have integrated Location Kit to your project successfully.

Calculating Distance Between Current Location with Destination Location

Mock Location

For debug and test purposes you may want to use the app on different location maybe on another continent. For that scenarios, you can use Mock Location.

Preparation

First, you should add permission to access mock location to AndroidManifest.xml file. (inside of manifest tag)

<uses-permission
android:name="android.permission.ACCESS_MOCK_LOCATION"
tools:ignore="MockLocation,ProtectedPermissions" />

Development

After initializing the object of HMSLocationService class, you can define a mock location anywhere in your activity or fragment.

val mockLocation = Location("MockLocation")
mockLocation.latitude = 41.028720
mockLocation.longitude = 29.117000
locationServices?.setMockMode(true)
locationServices?.setMockLocation(mockLocation)

In the demo app, I created a custom alert dialog that user can enter mock latitude and longitude to set mock location.

However even though you have successfully implemented setting and enabling the mock location, you will get the following error when you run the project.

Mock Location Error

You have to give permission to your app to use mock location on Developer Options. You have to do this step only once but if you uninstall the app, you have to repeat this step when you install the app again.

Go to settings -> Developer Options -> Select Mock Location App -> Choose your app

Choose your app to use Mock Location

After selecting your app, you can go back to your app and try again.

Set Mock Location

When Mock Location is used, accuracy becomes zero. Because Location Service does not do any operation to calculate the current location of the user.

Disabling Mock Location

To disable mock location, all you have to do is, setting mock mode to false. So that when user requests location update, the service returns the current location of the user instead of the mock location that you defined earlier.

locationService?.setMockMode(false)
Disable Mock Location

You can access the source codes of the ‘HMS-Location Kit’ demo project from the link down below:

References

If you need more details about HMS Location Kit, you can examine below links.

--

--