FORUMS

Developing the HMS Fused Location Service

981 posts
Thanks Meter: 1,057
 
By XDARoni, XDA Community Manager on 22nd May 2020, 04:29 PM
Post Reply Email Thread
Assigning App Permissions
The Android OS provides two location permissions: ACCESS_COARSE_LOCATION (approximate location permission) and ACCESS_FINE_LOCATION (precise location permission). These permissions are already defined in plugin.xml as follows:
Code:
<!--Fused Location-->
<config-file target="AndroidManifest.xml" parent="/*">
    <!--...-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!--...-->
</config-file>
In Android Q, if your app needs to continuously locate the device location when it runs in the background, you need to apply for the ACCESS_BACKGROUND_LOCATION permission. This permission is already defined in plugin.xml as follows:
Code:
<!--Fused Location-->
<config-file target="AndroidManifest.xml" parent="/*">
    <!--...-->
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
    <!--...-->
</config-file>
Using the Location Service Class
The Java class defined for Fused Location service of Location Kit is HMSFusedLocation.

To use it on Cordova, you should initilize Fused Location service of Location Kit with HMSFusedLocation.init() method before using other methods of HMSFusedLocation.
Code:
// Initialize 
LocationKitHMSLocationKit.init();
HMSFusedLocation.init();
Checking the Device Location Settings
Device location settings will affect the location result of HUAWEI Location Kit. For example, if the location function is disabled on a device, the device location cannot be obtained. Therefore, you are advised to check whether the device settings meet the location requirements before continuously obtaining location information. HUAWEI Location Kit provides the capability of checking device location settings.

If the location settings do not meet the location requirements, your app can prompt users to enable related permissions. (The location permission must be enabled for HMS Core (APK). In Android Q, Location must be set to Always for HMS Core.)

You can obtain the location settings using the checkLocationSettings(locationSettingsRequest) method.

The locationSettingsRequest object is an input parameter of the method and has three attributes: locationRequest, alwaysShow, and needBle.

The following is the sample code for creating a locationRequest object:
Code:
const locationRequest = () => { return {
    id: 'locationRequest' + Math.random() * 10000,
    priority: HMSFusedLocation.PriorityConstants.PRIORITY_HIGH_ACCURACY,
    interval: 3,
    numUpdates: 10,
    fastestInterval: 1000.0,
    expirationTime: 200000.0,
    expirationTimeDuration: 200000.0,
    smallestDisplacement: 0.0,
    maxWaitTime: 2000000.0,
    needAddress: true,
    language: 'en',
    countryCode: 'en',
}};
Then, following code can be used to create a locationSettingsRequest object:
Code:
const locationSettingsRequest = () => { return {
    alwaysShow: true,
    needBle: true,
    locationRequests: [locationRequest()]
}};
The needBle attribute indicates whether BLE scanning needs to be enabled. The options are true (yes) and false (no).

The alwaysShow attribute indicates whether a location is required for the app to continue. The options are true (yes) and false (no).

Finally, call the checkLocationSettings() method to obtain the location settings. The method returns a response containing the result.
Code:
const locationSettingsResult = await HMSFusedLocation.checkLocationSettings(locationSettingsRequest());
Obtaining the Last Known Location
An app can use the HUAWEI Location Kit service API to obtain the last known location of a device. In most cases, the last known location is the current location of the device. The following is the sample code for calling the getLastLocation() method to obtain the last known location.

The method has no input parameter and returns a response containing the location data.
Code:
const lastLocation = await HMSFusedLocation.getLastLocation();
Continuously Obtaining the Location Information
To enable your app to continuously obtain the device location, you can use the requestLocationUpdates() and registerHMSEvent(eventName, handler) methods provided by HUAWEI Location Kit.

First, you need to create a locationRequest object as in the example of checking the device location settings. Then, you need to call the requestLocationUpdates() method.

This method is used to request location updates using the callback on the specified Looper thread, and then returns the location request ID, if the request was successful.
Code:
const requestLocationUpdatesResult = await HMSFusedLocation.requestLocationUpdates(locationRequest());
console.log({requestLocationUpdatesResult});
Then, call the method registerHMSEvent(eventName, handler) that handles the HMS update events.
Code:
registerHMSEvent(HMSFusedLocation.Events.SCANNING_RESULT, (result) => {   
    console.log('new location update');
})
Using the Mock Location
Noteļ¼š
To use the mock location function, go to Settings > System & updates > Developer options > Select mock location app and select the app for using the mock location function.

(If Developer options is unavailable, go to Settings > About phone and tap Build number for seven consecutive times. Then, Developer options will be displayed on System & updates.)

Mock location feature can be enabled or disabled with the following method: setMockMode( boolean ).

This takes a boolean as argument, which indicates whether mock mode should be enabled or disabled.

The method returns a promise with the result, which should be resolved.
Code:
const setMockModeResult = await HMSFusedLocation.setMockMode(true);//or "false" to disable
Coordinates of the mock location can be set with the following method: setMockLocation({ latitude, longitude })

The method takes a single object as an argument, which contains the properties latitude and longitude.

Then the method returns the result with a promise, which should be resolved.
Code:
const setMockLocationResult = await HMSFusedLocation.setMockLocation({
    latitude: parseFloat(latitude),
    longitude: parseFloat(longitude)
});
console.log({setMockLocationResult});
Once the mock location function is enabled and set, your app will return the specified location data when trying to obtain the last location information.
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes