Add view annotation to a point annotation
Add a ViewAnnotation to a MapView and anchor it to a point annotation.
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"> <com.mapbox.maps.MapViewandroid:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent" /> <com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/fab_style_toggle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="16dp"android:src="@drawable/ic_layers_24dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"tools:backgroundTint="@color/blue"/> <com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/fab_reframe"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="16dp"android:src="@drawable/ic_baseline_refresh_24"app:layout_constraintBottom_toTopOf="@+id/fab_style_toggle"app:layout_constraintEnd_toEndOf="parent"tools:backgroundTint="@color/blue"/> </androidx.constraintlayout.widget.ConstraintLayout>
package com.mapbox.maps.testapp.examples.markersandcallouts import android.annotation.SuppressLintimport android.graphics.Bitmapimport android.os.Bundleimport android.view.Menuimport android.view.MenuItemimport android.view.Viewimport android.view.ViewGroupimport android.widget.Buttonimport androidx.appcompat.app.AppCompatActivityimport com.mapbox.geojson.Pointimport com.mapbox.maps.MapViewimport com.mapbox.maps.Styleimport com.mapbox.maps.ViewAnnotationAnchorimport com.mapbox.maps.extension.style.layers.properties.generated.IconAnchorimport com.mapbox.maps.plugin.annotation.Annotationimport com.mapbox.maps.plugin.annotation.annotationsimport com.mapbox.maps.plugin.annotation.generated.*import com.mapbox.maps.testapp.Rimport com.mapbox.maps.testapp.databinding.ActivityViewAnnotationShowcaseBindingimport com.mapbox.maps.testapp.databinding.ItemCalloutViewBindingimport com.mapbox.maps.testapp.utils.BitmapUtilsimport com.mapbox.maps.viewannotation.ViewAnnotationManagerimport com.mapbox.maps.viewannotation.ViewAnnotationUpdateModeimport com.mapbox.maps.viewannotation.viewAnnotationOptions /*** Example how to add view annotation to the point annotation.*/class ViewAnnotationWithPointAnnotationActivity : AppCompatActivity() { private lateinit var viewAnnotationManager: ViewAnnotationManagerprivate lateinit var pointAnnotationManager: PointAnnotationManagerprivate lateinit var pointAnnotation: PointAnnotationprivate lateinit var viewAnnotation: View @SuppressLint("SetTextI18n")override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivityViewAnnotationShowcaseBinding.inflate(layoutInflater)setContentView(binding.root) val iconBitmap = BitmapUtils.bitmapFromDrawableRes(this@ViewAnnotationWithPointAnnotationActivity,R.drawable.blue_marker_view)!! viewAnnotationManager = binding.mapView.viewAnnotationManager binding.mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS) {prepareAnnotationMarker(binding.mapView, iconBitmap)prepareViewAnnotation()// show / hide view annotation based on a marker clickpointAnnotationManager.addClickListener { clickedAnnotation ->if (pointAnnotation == clickedAnnotation) {viewAnnotation.toggleViewVisibility()}true}// show / hide view annotation based on marker visibilitybinding.fabStyleToggle.setOnClickListener {pointAnnotation.iconImageBitmap = if (pointAnnotation.iconImage == null) iconBitmap else nullpointAnnotationManager.update(pointAnnotation)}// update view annotation geometry if dragging the markerpointAnnotationManager.addDragListener(object : OnPointAnnotationDragListener {override fun onAnnotationDragStarted(annotation: Annotation<*>) {} override fun onAnnotationDrag(annotation: Annotation<*>) {if (annotation == pointAnnotation) {binding.mapView.viewAnnotationManager.updateViewAnnotation(viewAnnotation,viewAnnotationOptions {geometry(pointAnnotation.geometry)})ItemCalloutViewBinding.bind(viewAnnotation).apply {textNativeView.text = "lat=%.2f\nlon=%.2f".format(pointAnnotation.geometry.latitude(),pointAnnotation.geometry.longitude())}}} override fun onAnnotationDragFinished(annotation: Annotation<*>) {}})}} override fun onCreateOptionsMenu(menu: Menu): Boolean {menuInflater.inflate(R.menu.menu_view_annotation, menu)return true} override fun onOptionsItemSelected(item: MenuItem): Boolean {return when (item.itemId) {R.id.action_view_annotation_fixed_delay -> {viewAnnotationManager.setViewAnnotationUpdateMode(ViewAnnotationUpdateMode.MAP_FIXED_DELAY)true}R.id.action_view_annotation_map_synchronized -> {viewAnnotationManager.setViewAnnotationUpdateMode(ViewAnnotationUpdateMode.MAP_SYNCHRONIZED)true}else -> super.onOptionsItemSelected(item)}} private fun View.toggleViewVisibility() {visibility = if (visibility == View.VISIBLE) View.GONE else View.VISIBLE} @SuppressLint("SetTextI18n")private fun prepareViewAnnotation() {viewAnnotation = viewAnnotationManager.addViewAnnotation(resId = R.layout.item_callout_view,options = viewAnnotationOptions {geometry(POINT)associatedFeatureId(pointAnnotation.featureIdentifier)anchor(ViewAnnotationAnchor.BOTTOM)offsetY((pointAnnotation.iconImageBitmap?.height!!).toInt())})ItemCalloutViewBinding.bind(viewAnnotation).apply {textNativeView.text = "lat=%.2f\nlon=%.2f".format(POINT.latitude(), POINT.longitude())closeNativeView.setOnClickListener {viewAnnotationManager.removeViewAnnotation(viewAnnotation)}selectButton.setOnClickListener { b ->val button = b as Buttonval isSelected = button.text.toString().equals("SELECT", true)val pxDelta = if (isSelected) SELECTED_ADD_COEF_PX else -SELECTED_ADD_COEF_PXbutton.text = if (isSelected) "DESELECT" else "SELECT"viewAnnotationManager.updateViewAnnotation(viewAnnotation,viewAnnotationOptions {selected(isSelected)})(button.layoutParams as ViewGroup.MarginLayoutParams).apply {bottomMargin += pxDeltarightMargin += pxDeltaleftMargin += pxDelta}button.requestLayout()}}} private fun prepareAnnotationMarker(mapView: MapView, iconBitmap: Bitmap) {val annotationPlugin = mapView.annotationsval pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions().withPoint(POINT).withIconImage(iconBitmap).withIconAnchor(IconAnchor.BOTTOM).withDraggable(true)pointAnnotationManager = annotationPlugin.createPointAnnotationManager()pointAnnotation = pointAnnotationManager.create(pointAnnotationOptions)} private companion object {const val SELECTED_ADD_COEF_PX = 25val POINT: Point = Point.fromLngLat(0.381457, 6.687337)}}