Add a view annotation
Add a ViewAnnotation to a MapView when the map is clicked.
<?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.os.Bundleimport android.view.Menuimport android.view.MenuItemimport android.view.Viewimport android.view.ViewGroupimport android.widget.Buttonimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivityimport com.mapbox.geojson.Pointimport com.mapbox.maps.*import com.mapbox.maps.plugin.animation.flyToimport com.mapbox.maps.plugin.gestures.*import com.mapbox.maps.testapp.Rimport com.mapbox.maps.testapp.databinding.ActivityViewAnnotationShowcaseBindingimport com.mapbox.maps.testapp.databinding.ItemCalloutViewBindingimport com.mapbox.maps.viewannotation.ViewAnnotationManagerimport com.mapbox.maps.viewannotation.ViewAnnotationUpdateModeimport com.mapbox.maps.viewannotation.viewAnnotationOptions /*** Example how to add view annotations by clicking on the map.*/class ViewAnnotationBasicAddActivity : AppCompatActivity(), OnMapClickListener { private lateinit var mapboxMap: MapboxMapprivate lateinit var viewAnnotationManager: ViewAnnotationManagerprivate val viewAnnotationViews = mutableListOf<View>() override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivityViewAnnotationShowcaseBinding.inflate(layoutInflater)setContentView(binding.root) viewAnnotationManager = binding.mapView.viewAnnotationManager mapboxMap = binding.mapView.getMapboxMap().apply {loadStyleUri(Style.MAPBOX_STREETS) {addOnMapClickListener(this@ViewAnnotationBasicAddActivity)binding.fabStyleToggle.setOnClickListener {when (getStyle()?.styleURI) {Style.MAPBOX_STREETS -> loadStyleUri(Style.SATELLITE_STREETS)Style.SATELLITE_STREETS -> loadStyleUri(Style.MAPBOX_STREETS)}}Toast.makeText(this@ViewAnnotationBasicAddActivity, STARTUP_TEXT, Toast.LENGTH_LONG).show()}} binding.fabReframe.setOnClickListener {if (viewAnnotationViews.isNotEmpty()) {val cameraOptions = viewAnnotationManager.cameraForAnnotations(viewAnnotationViews)cameraOptions?.let {mapboxMap.flyTo(it)}} else {Toast.makeText(this@ViewAnnotationBasicAddActivity, ADD_VIEW_ANNOTATION_TEXT, Toast.LENGTH_LONG).show()}}} 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)}} override fun onMapClick(point: Point): Boolean {addViewAnnotation(point)return true} @SuppressLint("SetTextI18n")private fun addViewAnnotation(point: Point) {val viewAnnotation = viewAnnotationManager.addViewAnnotation(resId = R.layout.item_callout_view,options = viewAnnotationOptions {geometry(point)allowOverlap(true)})viewAnnotationViews.add(viewAnnotation)ItemCalloutViewBinding.bind(viewAnnotation).apply {textNativeView.text = "lat=%.2f\nlon=%.2f".format(point.latitude(), point.longitude())closeNativeView.setOnClickListener {viewAnnotationManager.removeViewAnnotation(viewAnnotation)viewAnnotationViews.remove(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 companion object {const val SELECTED_ADD_COEF_PX = 25const val STARTUP_TEXT = "Click on a map to add a view annotation."const val ADD_VIEW_ANNOTATION_TEXT = "Add view annotations to re-frame map camera"}}