# MarkerView

> **Note (legacy): 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](https://docs.mapbox.com/android/ja/maps/guides/annotations/view-annotations/) guide.

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

> **Note (warning): 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](https://docs.mapbox.com/android/ja/legacy/plugins/guides/annotation/) 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.

> **Note: Android's 64K method count limit**
> 
> If your application is over the 64K method limit, you can [shrink, obfuscate, and optimize your code](https://developer.android.com/studio/build/shrink-code) with R8 or ProGuard. If those steps do not lower your method count below 64K, you can also [enable multidex](https://developer.android.com/studio/build/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.

```groovy
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.

**Java**

```java

mapView.getMapAsync(new OnMapReadyCallback() {
	@Override
	public void onMapReady(MapboxMap mapboxMap) {

		MarkerViewManager markerViewManager = new MarkerViewManager(mapView, mapboxMap);

	}
});
```

**Kotlin**

```kt

mapView?.getMapAsync { mapboxMap ->

 val markerViewManager = 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`.

**Java**

```java

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

markerViewManager.addMarker(markerView);
```

**Kotlin**

```kt

val marker = MarkerView(LatLng(LAT.toDouble(),LONG.toDouble()), customView)

marker?.let {
	markerViewManager?.addMarker(it)
}
```

## Removing a marker

The `MarkerViewManager` can also remove a specific `MarkerView`.

**Java**

```java

markerViewManager.removeMarker(markerView);
```

**Kotlin**

```kt

marker?.let {
	markerViewManager?.removeMarker(it)
}
```

## 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:

**Java**

```java

@Override
protected void onDestroy() {
	super.onDestroy();

		markerViewManager.onDestroy();

		mapView.onDestroy();
}
```

**Kotlin**

```kt

override fun onDestroy() {
	super.onDestroy()

		markerViewManager?.onDestroy()

		mapView.onDestroy()
}
```

Fragment:

**Java**

```java

@Override
public void onDestroyView() {
	super.onDestroyView();

		markerViewManager.onDestroy();

		mapView.onDestroy();
}
```

**Kotlin**

```kt

override fun onDestroyView() {
super.onDestroyView()

	markerViewManager?.onDestroy()

	mapView.onDestroy()
}
```