• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!

Location Kit- Developing the HMS Geofence Service

Search This thread

XDARoni

XDA Community Manager
Assigning App Permissions
To use the geofence service API of HUAWEI Location Kit, ACCESS_FINE_LOCATION permission is needed. This permission is already defined in plugin.xml file as follows:
Code:
<!--Geofence-->
<config-file target="AndroidManifest.xml" parent="/*">
    <!--...-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!--...-->
</config-file>
In Android Q, ACCESS_BACKGROUND_LOCATION permission is needed. This permission is already defined in plugin.xml file as follows:
Code:
<!--Geofence-->
<config-file target="AndroidManifest.xml" parent="/*">
    <!--...-->
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <!--...-->
</config-file>
Using the Geofence Service Class
The Java class defined for Geofence service of Location Kit is HMSGeofence.

To use it on Cordova, you should initilize Geofence service of Location Kit with HMSGeofence.init() method before using other methods of HMSGeofence.
Code:
// Initialize LocationKit
HMSLocationKit.init();
HMSGeofence.init();
Creating and Adding a Geofence
You can create a geofence instance and construct a request to add a geofence.
  • You can create a geofence list with the following method:
    createGeofenceList( geofences, conversions, coordinateType )
The method takes three arguments:

- Geofences: List of geofences to be added

- Conversions: Initial conversions

- Coordinate Type: Type of coordinate

The method returns an id created for the sent request as a promise, which then should be resolved.

You can create geofence instances and construct a request to add a geofence, as shown below:
Code:
const geofence1 = {
    longitude: 42.0,
    latitude: 29.0,
    radius: 20.0,
    uniqueId: 'geofence' + Math.random() * 10000,
    conversions: 1,
    validContinueTime: 10000.0,
    dwellDelayTime: 10,
    notificationInterval: 1,
};
const geofence2 = {
    longitude: 41.0,
    latitude: 27.0,
    radius: 340.0,
    uniqueId: 'geofence' + Math.random() * 10000,
    conversions: 2,
    validContinueTime: 1000.0,
    dwellDelayTime: 10,
    notificationInterval: 1,
};
const createGeofenceListResult = await HMSGeofence.createGeofenceList(
    [geofence1, geofence2],
    HMSGeofence.GeofenceRequestConstants.ENTER_INIT_CONVERSION,
    HMSGeofence.GeofenceRequestConstants.COORDINATE_TYPE_WGS_84
);
console.log({createGeofenceListResult});
  • You can use registerHMSEvent(eventName, handler) method to listen to and receive data from activity geofence events:
Code:
registerHMSEvent(HMSGeofence.Events.GEOFENCE_RESULT, (result) => {
    console.log('new geofence update');
});
  • Created geofences can be removed with deleteGeofence(requestCode) method by using the request code which is created by createGeofenceList(geofences, conversions, coordinateType) method.
Code:
const result = await HMSGeofence.deleteGeofenceList(requestCode);
console.log({requestCode, result});
 

Top Liked Posts

  • There are no posts matching your filters.
  • 7
    Assigning App Permissions
    To use the geofence service API of HUAWEI Location Kit, ACCESS_FINE_LOCATION permission is needed. This permission is already defined in plugin.xml file as follows:
    Code:
    <!--Geofence-->
    <config-file target="AndroidManifest.xml" parent="/*">
        <!--...-->
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <!--...-->
    </config-file>
    In Android Q, ACCESS_BACKGROUND_LOCATION permission is needed. This permission is already defined in plugin.xml file as follows:
    Code:
    <!--Geofence-->
    <config-file target="AndroidManifest.xml" parent="/*">
        <!--...-->
        <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
        <!--...-->
    </config-file>
    Using the Geofence Service Class
    The Java class defined for Geofence service of Location Kit is HMSGeofence.

    To use it on Cordova, you should initilize Geofence service of Location Kit with HMSGeofence.init() method before using other methods of HMSGeofence.
    Code:
    // Initialize LocationKit
    HMSLocationKit.init();
    HMSGeofence.init();
    Creating and Adding a Geofence
    You can create a geofence instance and construct a request to add a geofence.
    • You can create a geofence list with the following method:
      createGeofenceList( geofences, conversions, coordinateType )
    The method takes three arguments:

    - Geofences: List of geofences to be added

    - Conversions: Initial conversions

    - Coordinate Type: Type of coordinate

    The method returns an id created for the sent request as a promise, which then should be resolved.

    You can create geofence instances and construct a request to add a geofence, as shown below:
    Code:
    const geofence1 = {
        longitude: 42.0,
        latitude: 29.0,
        radius: 20.0,
        uniqueId: 'geofence' + Math.random() * 10000,
        conversions: 1,
        validContinueTime: 10000.0,
        dwellDelayTime: 10,
        notificationInterval: 1,
    };
    const geofence2 = {
        longitude: 41.0,
        latitude: 27.0,
        radius: 340.0,
        uniqueId: 'geofence' + Math.random() * 10000,
        conversions: 2,
        validContinueTime: 1000.0,
        dwellDelayTime: 10,
        notificationInterval: 1,
    };
    const createGeofenceListResult = await HMSGeofence.createGeofenceList(
        [geofence1, geofence2],
        HMSGeofence.GeofenceRequestConstants.ENTER_INIT_CONVERSION,
        HMSGeofence.GeofenceRequestConstants.COORDINATE_TYPE_WGS_84
    );
    console.log({createGeofenceListResult});
    • You can use registerHMSEvent(eventName, handler) method to listen to and receive data from activity geofence events:
    Code:
    registerHMSEvent(HMSGeofence.Events.GEOFENCE_RESULT, (result) => {
        console.log('new geofence update');
    });
    • Created geofences can be removed with deleteGeofence(requestCode) method by using the request code which is created by createGeofenceList(geofences, conversions, coordinateType) method.
    Code:
    const result = await HMSGeofence.deleteGeofenceList(requestCode);
    console.log({requestCode, result});