SDK for Android Developer's Guide

MapEngine Class

MapEngine is a singleton class used to manage active mapping resources used in applications developed with HERE SDK. MapEngine must be initialized before Map and map related objects such as MapMarker and Places, can be instantiated and retrieved from the API. A runtime exception occurs if MapEngine is not properly initialized before map related objects are used.

Initialization

MapEngine must be initialized before it can be used. It should be done in the main thread. MapEngine is automatically initialized for your application by using AndroidXMapFragment. AndroidXMapFragment is a fragment class applications can use as a UI module in an activity for map display. However, if your application does not use AndroidXMapFragment classes, then the application should initialize the MapEngine directly before using any HERE APIs. You can do this by calling MapEngine.init(ApplicationContext, OnEngineInitListener) as shown below:

MapEngine mapEngine = MapEngine.getInstance();
ApplicationContext appContext = new ApplicationContext(context);
mapEngine.init(appContext, new OnEngineInitListener() {
  @Override
  public void onEngineInitializationCompleted(Error error) {
    if (error == OnEngineInitListener.Error.NONE) {
      // Post initialization code goes here
    } else {
      // handle factory initialization failure
    }
  }
});

If map engine initialization is in progress or has failed, calling any other HERE SDK APIs fails because invalid objects cannot be created. To avoid this problem, check for MapEngine.isInitialized() in your app lifecycle callbacks. For example, the following example avoids problems with using the PositionManager before an instance can be properly created:

public void onDestroy()
{
  // Set initComplete using MapEngine.isInitialized()
  if (initComplete) {
    PositioningManager.getInstance().removeListener(this);
  }
  super.onDestroy();
}

For examples of typical scenarios using the AndroidXMapFragment that automatically initializes the MapEngine, see Maps.