Integrating Location kit in Food Hunt App

In this article we will learn how to use Location Kit in Food Hunt App.

Introduction

Geolocation is the ability to track a device’s using GPS, cell phone towers, Wi-Fi access points or a combination of these. Huawei Location Kit combines the GPS, Wi-Fi, and base station locations to help you quickly obtain precise user locations.

Should have

1. To integrate the location kit in Quick App you should have developer account. Register here.

2. Android version 4.4 or later.

3. HMS core version 3.0.0.300 later has to be installed.

4. Enable HMS Core Location permission, Setting >Apps > HMS Core > Permission.

5. Enable location permission for application to be tested.

6. Location service should be enabled in the notification panel.

7. Test application in good network signal to get the more accurate location information.

Steps to be followed.

1. Create project

2. Add the feature attribute in manifest.json.

{

“name”: “system.geolocation”

}

3. Import the geolocation in the script

import geolocation from ‘@system.geolocation’

4. Design Location kit screen.

1) geolocation.getLocation(cordType, timeout, success, fail, complete)

· cordType: wgs84: This is the default value. It indicates that the geographical location will be returned by invoking the system location ability.

· Timeout: timeout duration in milliseconds. Default value is 30000. It is mandatory

· Success: It is callback function used when interface is successfully called.

· Fail: Callback function used when the interface fails to be called.

· Complete: Callback function used when the interface call is completed.

2) geolocation.subscribe(cordType, callback, fail)

· cordType: wgs84: This is the default value. It indicates that the geographical location will be returned by invoking the system location ability.

· Callback: It is called whenever location information changed.

· Fail: Callback function used when the interface fails to be called.

3) gelocation.unsubscribe() This is cancel the monitoring the geographical location.

4) geolocation.getLocationType(success, fail, complete): To get the location types currently supported by the system.

5) geolocation.openLocation: This is to view the location on the built in map. Currently it is Supported only in Chinese mainland.

6) geolocation.chooseLocation: This it open map and select location. Currently it is supported only in Chinese mainland.

protected GeoFenceActivity(Context context) {

this.mContext = context;

// Create a GeofenceService instance.

geofenceService = LocationServices.getGeofenceService(mContext);

geofenceList = new ArrayList<>();

// Obtain a PendingIntent object.

pendingIntent = getPendingIntent();

TAG = “geoFence”;

geoFenceActivated = false;

}

private GeofenceRequest getAddGeofenceRequest() {

GeofenceRequest.Builder builder = new GeofenceRequest.Builder();

// Trigger callback immediately after a geofence is added if a user is already in the geofence.

builder.setInitConversions(GeofenceRequest.ENTER_INIT_CONVERSION);

builder.createGeofenceList(geofenceList);

return builder.build();

}

// Dynamically register GeoFenceBroadcastReceiver through PendingIntent. A broadcast message will be sent when the geofence is triggered.

private PendingIntent getPendingIntent() {

Intent intent = new Intent(mContext, GeoFenceBroadcastReceiver.class);

intent.setAction(GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION);

return PendingIntent.getBroadcast(mContext,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

public void requestGeoFenceWithNewIntent() {

// Add a geofence by passing PendingIntent, and process response to the geofence addition request.

try {

geofenceService.createGeofenceList(getAddGeofenceRequest(), pendingIntent)

.addOnCompleteListener(task -> {

if (task.isSuccessful()) {

Log.i(TAG, “add geofence success!”);

} else {

Log.w(TAG, “add geofence failed : “ + task.getException().getMessage());

}

});

} catch (Exception e) {

Log.d(TAG, “requestGeoFenceWithNewIntent: “ + e.toString());

}

}

public void removeWithID(Resturant resturant) {

idList = new ArrayList<>();

String id = resturant.getName() + “: “ + resturant.getId();

idList.add(id);

// Remove a geofence based on its ID, and process response to the geofence deletion request.

geofenceService.deleteGeofenceList(idList)

.addOnCompleteListener(task -> {

if (task.isSuccessful()) {

Log.i(TAG, “delete geofence with ID “+ id +” success!”);

// remove geofence with ID

for (Geofence geofence: geofenceList) {

if (geofence.getUniqueId().equals(id)) {

geofenceList.remove(geofence);

}

}

} else {

Log.w(TAG, “delete geofence with ID “+ id + “ failed”);

}

});

}

public void addGeoFenceLocation(Resturant resturant) {

String id = resturant.getName() + “: “ + resturant.getId();

geofenceList.add(new Geofence.Builder()

.setUniqueId(id)

.setValidContinueTime(10000)

// Pass the longitude and latitude of the geofence center, and radius of the geofence (unit: meters).

.setRoundArea(resturant.getLocation().getLat(), resturant.getLocation().getLng(), 2000)

// Trigger callback when a user enters the geofence.

.setConversions(Geofence.ENTER_GEOFENCE_CONVERSION)

.setNotificationInterval(1000)

.build());

Log.d(TAG, “addGeoFenceLocation: number of geofences activated: “+geofenceList.size());

Log.d(TAG, “addGeoFenceLocation: GeoFencing activated”);

requestGeoFenceWithNewIntent();

}

Result

Conclusion

In this article, we have learnt how to integrate the Location kit in Food Hunt Application

Reference

Location kit official document

Open Source

App Link