Guides & References

Explore the guides and references for implementing Location Kit in Unity with HMS Unity Plugin.

Developing the Fused Location Service

Assigning App Permissions

1-) Declare the required permissions in the AndroidManifest.xml file. The Android OS provides two location permissions: ACCESS_COARSE_LOCATION (approximate location permission) and ACCESS_FINE_LOCATION (precise location permission).

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

2-) (Optional) If your app needs to continuously locate the device when it runs in the background in Android 10 or later, declare the ACCESS_BACKGROUND_LOCATION permission in the AndroidManifest.xml file.

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

Dynamically apply for related location permissions (according to requirements for dangerous permissions in Android 6.0 or later).

HMSLocationManager.Instance.RequestFineLocationPermission();
HMSLocationManager.Instance.RequestCoarseLocationPermission();
HMSLocationManager.Instance.RequestBackgroundLocationPermissions();

Requesting Location Updates

By using DefineLocationCallback() you can listen for onLocationResult and onLocationAvailability.

locationCallback = HMSLocationManager.Instance.DefineLocationCallback();

Then you can simply listen to the actions to get the locationResult and locationAvailability.

HMSLocationManager.Instance.onLocationResult += OnLocationResult;
HMSLocationManager.Instance.onLocationAvailability += OnLocationAvailability;

void OnLocationResult(LocationResult locationResult) {
    ...
}
void OnLocationAvailability(LocationAvailability locationAvailability) {
    ...
}

Developing Activity Identification Service

To use the activity identification service in versions earlier than Android 10, declare the following permission in the AndroidManifest.xml file:

<uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION"/>

To use the activity identification service in Android 10 and later versions, declare the following permission in the AndroidManifest.xml file:

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

Note: The android.permission.ACTIVITY_RECOGNITION and com.huawei.hms.permission.ACTIVITY_RECOGNITION permissions are dangerous permissions and need to be dynamically applied for.

HMSLocationManager.Instance.RequestActivityRecognitionPermissions();

Create a PendingIntent object

HMSLocationManager.Instance.GetPendingIntentFromLocation();

RequestActivityIdentification / ActivityConversionUpdates

//If you are planning to use more than one Activity update make sure to add and remove its listeners when necessary from HMSLocationManager
 HMSLocationManager.AddIdentificationListener();
            
_activityIdentificationService.CreateActivityConversionUpdates(_request, _pendingIntent)
.AddOnSuccessListener(type => { ... })
.AddOnFailureListener(exception =>{ ...});

How to Listen OnReceive from BroadcastReceiver

You can listen OnReceive by using the following line and adding your OnReceive Method

LocationReceiver.Instance.onReceive += OnReceive;
LocationReceiver.Instance.SetLocationBroadcastListener();

private void OnReceive(AndroidIntent intent){
 if (LocationReceiver.isListenActivityConversion)
 {   
    var activityIdentificationResponse = ActivityIdentificationResponse.GetDataFromIntent(intent);
        foreach (var activityIdentificationData in activityIdentificationResponse.GetActivityIdentificationDatas())
        {
         ...
        }
 }
}

Developing the Geofence Service

To use the geofence service APIs of Location Kit, declare the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in the AndroidManifest.xml file.

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

In Android 10, declare the ACCESS_BACKGROUND_LOCATION permission in the AndroidManifest.xml file so that your app can obtain the device location when it runs in the background.

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

Note: The preceding permissions are dangerous permissions and need to be dynamically applied for.

HMSLocationManager.Instance.RequestFineLocationPermission();
HMSLocationManager.Instance.RequestCoarseLocationPermission();
HMSLocationManager.Instance.RequestBackgroundLocationPermissions();

Initialize Geofence Receiver and LocationResult

Note: By listening GeofenceReceiver's onReceive method you can get intent result from GeoFenceBroadcastReceiver which extends BroadcastReceiver.

private void Start() {
GeofenceReceiver.Instance.onReceive += OnReceive;
HMSLocationManager.Instance.onLocationResult += OnLocationResult;
GeofenceReceiver.Instance.SetGeofenceBroadcastListener();
}

InitGeofenceServiceClient

// Create a GeofenceService instance.
geofenceService = LocationServices.GetGeofenceService();

// Obtain a PendingIntent object.
pendingIntent = HMSLocationManager.Instance.GetPendingIntentFromGeofence();

Send the request to add a geofence

geofenceService.CreateGeofenceList(geofenceRequest, pendingIntent)
.AddOnSuccessListener(type => { Debug.Log($"{TAG} CreateGeofenceList Successful"); })
.AddOnFailureListener(exception => { 
Debug.LogError($"{TAG} CreateGeofenceList Exception {exception.WrappedCauseMessage} with error code: {exception.ErrorCode}");
});

Set your onReceive Method

private void OnReceive(AndroidIntent intent){
GeofenceData geofenceData = GeofenceData.GetDataFromIntent(intent);
DisplayGeofenceData(geofenceData);
}

Last updated