Add a marker to the map
Use the Mapbox Annotation API's PointAnnotationManager
to add a single red marker pin to the map.
There are several ways to add markers, annotations, and other shapes to the map using the Maps SDK. To choose the best approach for your application, read the Markers and annotations guide.
Assets
Download the red marker pin image and add it to your Android Studio project.
Download red marker image
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><com.mapbox.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:mapbox="http://schemas.android.com/apk/res-auto"android:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent"mapbox:mapbox_cameraTargetLat="59.31"mapbox:mapbox_cameraTargetLng="18.06"mapbox:mapbox_cameraZoom="9.0"/>
MainActivity.kt
package com.example.maps_carbon_android import android.content.Contextimport android.graphics.Bitmapimport android.graphics.Canvasimport android.graphics.drawable.BitmapDrawableimport android.graphics.drawable.Drawableimport android.os.Bundleimport androidx.annotation.DrawableResimport androidx.appcompat.app.AppCompatActivityimport androidx.appcompat.content.res.AppCompatResourcesimport com.mapbox.geojson.Pointimport com.mapbox.maps.MapViewimport com.mapbox.maps.Styleimport com.mapbox.maps.plugin.annotation.annotationsimport com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptionsimport com.mapbox.maps.plugin.annotation.generated.createPointAnnotationManager class MainActivity : AppCompatActivity() {private var mapView: MapView? = null override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)mapView = findViewById(R.id.mapView)mapView?.getMapboxMap()?.loadStyleUri(Style.MAPBOX_STREETS,object : Style.OnStyleLoaded {override fun onStyleLoaded(style: Style) {addAnnotationToMap()}})} private fun addAnnotationToMap() {// Create an instance of the Annotation API and get the PointAnnotationManager.bitmapFromDrawableRes(this@MainActivity,R.drawable.red_marker)?.let {val annotationApi = mapView?.annotationsval pointAnnotationManager = annotationApi?.createPointAnnotationManager(mapView!!)// Set options for the resulting symbol layer.val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()// Define a geographic coordinate..withPoint(Point.fromLngLat(18.06, 59.31))// Specify the bitmap you assigned to the point annotation// The bitmap will be added to map style automatically..withIconImage(it)// Add the resulting pointAnnotation to the map.pointAnnotationManager?.create(pointAnnotationOptions)}}private fun bitmapFromDrawableRes(context: Context, @DrawableRes resourceId: Int) =convertDrawableToBitmap(AppCompatResources.getDrawable(context, resourceId)) private fun convertDrawableToBitmap(sourceDrawable: Drawable?): Bitmap? {if (sourceDrawable == null) {return null}return if (sourceDrawable is BitmapDrawable) {sourceDrawable.bitmap} else {// copying drawable object to not manipulate on the same referenceval constantState = sourceDrawable.constantState ?: return nullval drawable = constantState.newDrawable().mutate()val bitmap: Bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight,Bitmap.Config.ARGB_8888)val canvas = Canvas(bitmap)drawable.setBounds(0, 0, canvas.width, canvas.height)drawable.draw(canvas)bitmap}}}
Was this example helpful?