How to perform Basic Positioning with HERE SDK Navigate for Android

This tutorial outlines the steps necessary to implement a minimum viable positioning feature using the HERE SDK for Android. The implementation involves enabling the HERE LocationEngine, receiving location updates, using a LocationIndicator to display the current location and accuracy halo on the map, and configuring the camera to automatically follow the device's location.

1. Permissions and Project Setup
--------------------------------

The first step is declaring the necessary permissions in your AndroidManifest.xml file. These permissions are core requirements for obtaining device location and enhancing precision using network signals:

ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION (Core permissions for getting location)
ACCESS_WIFI_STATE
CHANGE_NETWORK_STATE
CHANGE_WIFI_STATE (Network/Wi-Fi related permissions to enhance precision)

Note: If your application targets Android 12 or higher, the user must explicitly grant 'precise' location permission; otherwise, positioning will fail.

2. Initialize the HERE SDK and MapView
--------------------------------------

If your application has not yet initialized the HERE SDK, you must first create the shared instance of the SDK engine using your credentials. Then, this instance must be bound to the MapView instance.

String accessKeyID = "YOUR_ACCESS_KEY_ID";

String accessKeySecret = "YOUR_ACCESS_KEY_SECRET";

AuthenticationMode authenticationMode = AuthenticationMode.withKeySecret(accessKeyID, accessKeySecret);

SDKOptions options = new SDKOptions(authenticationMode);

try { SDKNativeEngine.makeSharedInstance(getApplicationContext(), options); }

MapView mapView = findViewById(R.id.map_view);

mapView.onCreate(savedInstanceState); // Remember to call the corresponding lifecycle method

3. Confirm Handling of HERE Privacy Notice
------------------------------------------

This is a critical and mandatory step. The LocationEngine collects anonymous characteristics of nearby mobile and Wi-Fi network signals to maintain and improve HERE Positioning services. You must inform the user about this collection in your application's privacy policy and provide a link to the HERE Privacy Notice.

Once this is done, you must call the confirmation method:

locationEngine.confirmHEREPrivacyNoticeInclusion();

The engine will not provide any location updates until this confirmation is made. In special cases pre-approved by HERE, you may call confirmHEREPrivacyNoticeException() instead.

4. Create the LocationEngine and its Listeners
----------------------------------------------

The next step is to integrate the core positioning flow. First, create the LocationEngine instance. This step occurs after creating the instance but before calling start(...).

You must add a LocationListener to receive location updates:

locationEngine.addLocationListener(new LocationListener() {

@Override public void onLocationUpdated(@NonNull Location location) {

onLocation(location); // Pass to the next scene for UI update

}

});

It is also recommended to add a LocationStatusListener to observe the engine's start, stop, and availability status. Finally, start the engine using the desired accuracy level:

locationEngine.start(LocationAccuracy.BEST_AVAILABLE);

5. Show the Current Location on the Map (LocationIndicator)
-----------------------------------------------------------

To visualize the positioning result, use the LocationIndicator. It should be created upon receiving the first location update. You should set its style and enable the accuracy halo to visually represent the quality of the location signal.

Whenever a new Location object is received, call updateLocation() on the indicator to refresh the display.

To keep the map centered on the user, move the camera using the location's coordinates:

locationIndicator.updateLocation(location);

mapView.getCamera().lookAt(

location.coordinates,

new MapMeasure(MapMeasure.Kind.DISTANCE, 600)

);

6. Stopping Positioning and Lifecycle Management (Clean-up)
-----------------------------------------------------------

Proper lifecycle management is essential to prevent unnecessary power consumption and resource leaks. You should manage the LocationEngine and MapView lifecycle methods:

Stop the LocationEngine in onPause() and restart it in onResume().
Call the corresponding lifecycle methods for the MapView (e.g., mapView.onDestroy()).
In onDestroy(), remove listeners (such as locationListener and locationStatusListener) from the LocationEngine to avoid memory leaks.
* Dispose of the HERE SDK (disposeHERESDK()) only when it is guaranteed that no other components of the application require it.

---

Key Takeaway
------------

Implementing basic positioning requires a mandatory sequence of steps: Configure the AndroidManifest with required location and network permissions, initialize the SDKNativeEngine and MapView, instantiate the LocationEngine and crucially confirm the HERE Privacy Notice inclusion to enable location updates. Once confirmed, start the engine and use the LocationIndicator to visualize the real-time position and accuracy halo on the map, ensuring robust resource management through the Activity's lifecycle methods (onResume(), onPause(), onDestroy()).