MarkerView

The MarkerView Plugin for Android provides a simplified way to add map markers that are Android views.

Alternative option
Please be aware that Android views are the less performant and less customizable option for adding icons to a map. The Mapbox Maps SDK's SymbolLayer is the recommended way to add icons. View the Mapbox Annotation Plugin for Android for more information about using a SymbolLayer.

Install the MarkerView plugin

To start developing an application using the MarkerView Plugin, you'll need to add the appropriate dependencies inside your build.gradle file. This dependency includes the Maps SDK for Android. You can find all dependencies given below on MavenCentral.

If your application is close or exceeds the 65k method count limit, you can mitigate this problem by enabling ProGuard inside your application. ProGuard directives are included in the Android dependencies to preserve the required classes.

Add the dependency

  1. Start Android Studio.
  2. Open up your application's build.gradle.
  3. Make sure that your project's minSdkVersion is API 14 or higher.
  4. Under dependencies, add a new build rule for the latest mapbox-android-plugin-markerview-v8.
  5. Click the Sync Project with Gradle Files near the toolbar in Studio.
repositories {
  mavenCentral()
}

dependencies {
  implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v8:0.3.0'
}

Initialize the plugin

The plugin includes a manager class for adding and removing markers. The MarkerViewManager class requires initialized MapboxMap and MapView objects to be created. You should create a MarkerViewManager object within onMapReady() to be sure that the markers can be added to a successfully built map.

mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
MarkerViewManager markerViewManager = new MarkerViewManager(mapView, mapboxMap);
}
});

Adding a marker

The plugin is all about Android views, so you'll need to first create whatever view that you want to use for the marker image. Once you've constructed the view, use it to create a MarkerView class and then eventually pass the MarkerView object to the MarkerViewManager.

MarkerView markerView = new MarkerView(new LatLng(LAT,LONG), customView);
markerViewManager.addMarker(markerView);

Removing a marker

The MarkerViewManager can also remove a specific MarkerView.

markerViewManager.removeMarker(markerView);

Destroying the manager

Don't forget to destroy your MarkerViewManager object. The destruction should happen in onDestroy() if you're using an activity. It should happen in the onDestroyView() method of a fragment.

Activity:

@Override
protected void onDestroy() {
super.onDestroy();
markerViewManager.onDestroy();
mapView.onDestroy();
}

Fragment:

@Override
public void onDestroyView() {
super.onDestroyView();
markerViewManager.onDestroy();
mapView.onDestroy();
}
Was this page helpful?