Map overlay
This example demonstrates using the MapOverlayPlugin
to reframe dynamic map content within an overlay with the Mapbox Maps SDK for Android.
This example creates an overlay layer with text views in each of the 4 corners of the viewport and includes 3 markers loading onto the map. When clicking on the map, a new marker is added at every click location. When a user clicks the FloatingActionButton
in activity_map_overlay.xml
the map moves to reframe all markers within the selected overlay area.
This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.
See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.
package com.mapbox.maps.testapp.examples
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import com.mapbox.geojson.Feature
import com.mapbox.geojson.FeatureCollection
import com.mapbox.geojson.Point
import com.mapbox.maps.MapboxMap
import com.mapbox.maps.Style
import com.mapbox.maps.extension.style.image.image
import com.mapbox.maps.extension.style.layers.generated.symbolLayer
import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
import com.mapbox.maps.extension.style.sources.getSourceAs
import com.mapbox.maps.extension.style.style
import com.mapbox.maps.plugin.animation.MapAnimationOptions.Companion.mapAnimationOptions
import com.mapbox.maps.plugin.animation.camera
import com.mapbox.maps.plugin.gestures.OnMapClickListener
import com.mapbox.maps.plugin.gestures.addOnMapClickListener
import com.mapbox.maps.plugin.overlay.MapOverlayCoordinatesProvider
import com.mapbox.maps.plugin.overlay.MapOverlayPlugin
import com.mapbox.maps.plugin.overlay.mapboxOverlay
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.databinding.ActivityMapOverlayBinding
class MapOverlayActivity : AppCompatActivity(), OnMapClickListener {
private val markerCoordinates = mutableListOf<Point>(
Point.fromLngLat(-71.065634, 42.354950), // Boston Common Park
Point.fromLngLat(-71.097293, 42.346645), // Fenway Park
Point.fromLngLat(-71.053694, 42.363725) // The Paul Revere House
)
private lateinit var mapOverlayPlugin: MapOverlayPlugin
private lateinit var mapboxMap: MapboxMap
private val provider = MapOverlayCoordinatesProvider { markerCoordinates }
private lateinit var binding: ActivityMapOverlayBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMapOverlayBinding.inflate(layoutInflater)
setContentView(binding.root)
mapboxMap = binding.mapView.mapboxMap
mapboxMap.loadStyle(
styleExtension = style(Style.LIGHT) {
+geoJsonSource(sourceId) {
featureCollection(
FeatureCollection.fromFeatures(
markerCoordinates.map {
Feature.fromGeometry(it)
}
)
)
}
// Add the marker image to map
+image(
imageId,
ContextCompat.getDrawable(
this@MapOverlayActivity,
R.drawable.ic_blue_marker
)!!.toBitmap()
)
+symbolLayer(layerId, sourceId) {
iconImage(imageId)
iconAllowOverlap(true)
iconOffset(listOf(0.0, -9.0))
}
}
) { mapboxMap.addOnMapClickListener(this@MapOverlayActivity) }
mapOverlayPlugin = binding.mapView.mapboxOverlay
.apply {
registerMapOverlayCoordinatesProvider(provider)
registerOverlay(binding.locationTopLeft)
registerOverlay(binding.locationTopRight)
registerOverlay(binding.locationBottomLeft)
registerOverlay(binding.locationBottomRight)
registerOverlay(binding.reframeButton)
setDisplayingAreaMargins(100, 50, 50, 50)
}
val cameraAnimationsPlugin = binding.mapView.camera
binding.reframeButton.setOnClickListener {
mapOverlayPlugin.reframe { it?.let { cameraAnimationsPlugin.flyTo(it, mapAnimationOptions { duration(1000L) }) } }
}
}
override fun onMapClick(point: Point): Boolean {
markerCoordinates.add(point)
mapboxMap.style?.getSourceAs<GeoJsonSource>(sourceId)!!.featureCollection(
FeatureCollection.fromFeatures(
markerCoordinates.map {
Feature.fromGeometry(it)
}
)
)
return true
}
companion object {
private const val sourceId = "marker-source"
private const val imageId = "marker-image"
private const val layerId = "marker-layer"
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:mapbox="http://schemas.android.com/apk/res-auto">
<com.mapbox.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
mapbox:mapbox_cameraTargetLat="42.353517"
mapbox:mapbox_cameraTargetLng="-71.078625"
mapbox:mapbox_cameraZoom="12"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/location_top_left"
style="@style/MapOverlayStyle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/location_top_right"
style="@style/MapOverlayStyle"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/location_bottom_left"
style="@style/MapOverlayStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/location_bottom_right"
style="@style/MapOverlayStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/reframe_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="16dp"
android:layout_marginBottom="116dp"
android:src="@drawable/ic_baseline_refresh_24"
app:backgroundTint="@color/primary" />
</androidx.constraintlayout.widget.ConstraintLayout>