SDK for Android Developer's Guide

Basic Positioning

SDK for Android provides the following interfaces for users to retrieve location updates and to display their current location on a map:
  • PositioningManager
  • OnPositionChangedListener
  • PositionIndicator
Note: Android permission android.permission.ACCESS_FINE_LOCATION is required when your app calls PositioningManager.start(LocationMethod). Otherwise, the method returns false. In addition, to ensure that the app receives location updates, the user needs to have the Location permission enabled (toggled to "on") during runtime.

PositioningManager Class

A PositioningManager class provides information related to the device geographical location like the current position and the average speed. Applications can register to receive position updates using one of the positioning mechanisms described in the LocationMethod:

  • GPS - positioning using the real GPS available on the device
  • GPS_NETWORK - positioning is provided using a wireless network or the real GPS available on the device
  • NETWORK - positioning using a wireless network

The current status of a particular location method is represented by the LocationStatus value returned from the PositioningManager.getLocationStatus(LocationMethod) method.

PositioningManager can be accessed by calling PositioningManager.getInstance(). An application can start receiving real time positioning updates by calling PositioningManager.start(LocationMethod) with one of the location methods listed above and can stop positioning updates by calling PositioningManager.stop(). While position updates are being received, an application can retrieve the current position of the client device via PositioningManager.getPosition() method.

OnPositionChangedListener Interface

In addition to the PositioningManager's getPosition() method applications can subscribe to position update notifications from the PositioningManager through the PositioningManager.OnPositionChangedListener interface. To add or remove OnPositionChangedListener, applications can use the following methods:

PositioningManager.addListener(WeakReference<OnPositionChangedListener>)
PositioningManager.removeListener(OnPositionChangedListener)

The positioning manager enhances your application with the current position of the user's device. Registration of the positioning listener should be performed after AndroidXMapFragment, MapView, or MapEngine is initialized as described in the following code snippet.

// Define positioning listener
private OnPositionChangedListener positionListener = new
  OnPositionChangedListener() {

    public void onPositionUpdated(LocationMethod method,
        GeoPosition position, boolean isMapMatched) {
      // set the center only when the app is in the foreground
      // to reduce CPU consumption
      if (!paused) {
        map.setCenter(position.getCoordinate(),
            Map.Animation.NONE);
      }
    }

    public void onPositionFixChanged(LocationMethod method,
        LocationStatus status) {
    }
};

// Register positioning listener
PositioningManager.getInstance().addListener(
  new WeakReference<OnPositionChangedListener>(positionListener));
...

In order to avoid unnecessary position updates while the activity is in the background, you need to start or stop the PositioningManager within your activity's onResume() and onPause() methods.

// Set this to PositioningManager.getInstance() upon Engine Initialization
private PositioningManager posManager;
...

// Resume positioning listener on wake up
public void onResume() {
  super.onResume();
  paused = false;
  if (posManager != null) {
    posManager.start(
        PositioningManager.LocationMethod.GPS_NETWORK);
  }
}

// To pause positioning listener
public void onPause() {
  if (posManager != null) {
    posManager.stop();
  }
  super.onPause();
  paused = true;
}

// To remove the positioning listener
public void onDestroy() {
  if (posManager != null) {
    // Cleanup
    posManager.removeListener(
        positionListener);
  }
  map = null;
  super.onDestroy();
}

PositionIndicator Class

PositionIndicator is a special map marker object that allows the current client device position to be shown on a map. Every HERE SDK Map object has an integrated position indicator set to invisible by default. The indicator can be retrieved and set to visible by calling AndroidXMapFragment.getPositionIndicator() and PositionIndicator.setVisible() as follows:

// display position indicator
mapFragment.getPositionIndicator().setVisible(true);

By default the position indicator is rendered as a marker surrounded by a circle, the diameter of which illustrates the accuracy of the indicated position. You can change this marker by calling PositionIndicator.setMarker(Image).

Figure 1. A PositionIndicator
Note: For the position indicator to stay in the center of the map and illustrate real-time updates of the device position, it is necessary to update the map center whenever a new location update is received.
Note: PositionIndicator only works if the application has started the PositioningManager.

Position Simulation and Creating Position Logs

You can use PositionSimulator to simulate device position by injecting locations into Android LocationManager. Locations are read from GPX log files. After calling PositionSimulator.startPlayback(String) positions in the log file are processed until the end of the log is reached or stopPlayback() is called.

Important: Starting from Android Q (10.0) in order to use PositionSimulator, Location Services need to be enabled.

You can also use HERE SDK to create the GPX logs that can be replayed by PositionSimulator. To do this, call setLogType(EnumSet<LogType>) in PositioningManager to include LogType.DATA_SOURCE. GPX logs are written to a "gpx" sub-directory of your appication's data directory — for example, "/sdcard/Android/data/com.companyName.appName/files/gpx/". To disable logging, call setLogType(EnumSet.noneOf(LogType.class)).

Note: This feature is only intended for debugging purposes. Do not use Position Logging in a production application.
Note: PositionSimulator does not support indoor positioning.