kotlinmapboxmapbox-androidmapbox-marker

How to remove a single mapbox android annotation?


I only started learning mapbox maps and I'm trying to remove a mapbox annotation every time I create another annotation.

My code:

bitmapFromDrawableRes(
            this@PagePlanRouteMap,
            R.drawable.red_marker

        )?.let {

            val annotationApi = mapView?.annotations
            val pointAnnotationManager = annotationApi?.createPointAnnotationManager(mapView!!)


            val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()

                .withPoint(Point.fromLngLat(lng, lat))
                .withIconImage(it)

            pointAnnotationManager?.create(pointAnnotationOptions)
        }

Can you help me implement the deleting of annotation using --

pointAnnotationManager?.deleteAll()

or is there another better way for this? Thanks.


Solution

  • You can use the delete() method from your pointAnnotationManager. It has 2 different parameters you can pass through: either a PointAnnotation object or a list of PointAnnotation.

    For example:

    val pointAnnotation = pointAnnotationManager?.create(pointAnnotationOptions)
    pointAnnotationManager?.delete(pointAnnotation)
    

    OR

    pointAnnotationManager?.delete(listOf(pointAnnotation))