メインコンテンツまでスキップ

MarkerView

Not compatible with the Maps SDK v10
This plugin is not compatible with the Maps SDK v10 or higher. The Maps SDK v10 and higher comes with view annotation functionality built in. See the View annotations guide.

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

Alternative option
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.

Android's 64K method count limit
If your application is over the 64K method limit, you can shrink, obfuscate, and optimize your code with R8 or ProGuard. If those steps do not lower your method count below 64K, you can also enable multidex.

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-v9.
  5. Click the Sync Project with Gradle Files near the toolbar in Studio.
repositories {
mavenCentral()
}

dependencies {
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v9:0.4.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();
}
この{Type}は役に立ちましたか?