Why is the annotation still visible?

Hi so I am trying to code right now in mapbox android kotlin. I am trying to have pins (or annotations) appear on the screen and give the user the ability to delete a pin from it if they want. The pins are numbered, however I’m running into 2 problems. First the numbers are not being updated correctly on the screen and secondly the pin itself is not being removed. Meaning if there are 4 pins, and I were to remove 1 of them, just 3 pins should remain. These are the relevant parts of my code that deal with the annotations.

private val viewAnnotationsMap = mutableMapOf<Long, View>() // Map to store view annotations by their IDs
private val annotationsList = mutableListOf() // List to store annotations

private fun addAnnotation(latitude: Double, longitude: Double, markerCounter:Int) {
    val drawableResId = getDrawableResId(this@ShowMarkersActivity, markerCounter)
    if (drawableResId != 0) {  // Check if the drawable resource exists
        bitmapFromDrawableRes(
            this@ShowMarkersActivity,
            drawableResId
        )?.let { iconBitmap ->
            val annotationApi = binding.mapView.annotations
            val pointAnnotationManager = annotationApi?.createPointAnnotationManager()
            val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()
                .withPoint(Point.fromLngLat(longitude, latitude))
                .withIconImage(iconBitmap)
                .withIconAnchor(IconAnchor.BOTTOM)

            val pointAnnotation = pointAnnotationManager?.create(pointAnnotationOptions)

            pointAnnotation?.let { annotation ->
                // Add view annotation attached to the point
                val viewAnnotationManager = binding.mapView.viewAnnotationManager
                val viewAnnotation = viewAnnotationManager.addViewAnnotation(
                    resId = R.layout.annotation_text,
                    options = viewAnnotationOptions {
                        geometry(Point.fromLngLat(longitude, latitude))
                    }
                )
                viewAnnotation.id = markerCounter

                // Store the view annotation and point annotation
                viewAnnotationsMap[annotation.id] = viewAnnotation
                annotationsList.add(annotation)

                viewAnnotation.visibility = View.GONE

                // Set click listener to show/hide the view annotation and show a popup dialog for deletion
                pointAnnotationManager.addClickListener { clickedAnnotation ->
                    // If the clicked annotation matches, toggle its visibility
                    if (clickedAnnotation.id == annotation.id) {
                        viewAnnotation.visibility = if (viewAnnotation.visibility == View.VISIBLE) View.GONE else View.VISIBLE

                        // Show a popup dialog to confirm deletion
                        if (viewAnnotation.visibility == View.VISIBLE) {
                            showDeletePopup(annotation)
                        }
                    } else {
                        // Hide other annotations
                        viewAnnotationsMap.values.forEach { it.visibility = View.GONE }
                    }
                    true  // Return true to indicate the click was handled
                }
            }
        }
    }
}


private fun showDeletePopup(annotation: PointAnnotation) {
    val builder = AlertDialog.Builder(this@ShowMarkersActivity)
    builder.setTitle("Delete Annotation")
    builder.setMessage("Do you want to delete this annotation?")
    builder.setPositiveButton("Yes") { dialog, _ ->
        val indexToRemove = annotationsList.indexOfFirst { it.id == annotation.id }
        if (indexToRemove != -1) {
            tour.stops.removeAt(indexToRemove)

            val annotationToRemove = annotationsList[indexToRemove]
            val pointAnnotationManager = binding.mapView.annotations?.createPointAnnotationManager()
            pointAnnotationManager?.delete(annotationToRemove)
            // Remove the view annotation
            viewAnnotationsMap.remove(annotation.id)?.let { it.visibility = View.GONE }
            // Remove from the list
            annotationsList.removeAt(indexToRemove)
            // Update remaining annotations
            updateAnnotations()
            Log.e("numberofannotationsleft", tour.stops.size.toString())
        }
        dialog.dismiss()
    }
    builder.setNegativeButton("No") { dialog, _ ->
        dialog.dismiss()
    }
    builder.create().show()
}

private fun updateAnnotations() {
    // Clear all annotations
    viewAnnotationsMap.clear()
    annotationsList.clear()
    var markerCounter = 1

    for (stop in tour.stops) {
        addAnnotation(stop.tourLocation.startLocation.lat, stop.tourLocation.startLocation.lng, markerCounter)
        markerCounter++
        Log.e("tourStops", tour.stops.size.toString())
    }
}

So I learned that I am 99% certain that my updateAnnotations is where the issue lies, because without it, the pin is actually deleted, however the numbers (of course) don’t update appropriately. After logging, the system is telling me there should be only 3 pins in both showDeletePopup and updateAnnotations, so I’m stuck on where the loop is occurring. I provided the what the user would see before and after deleting.
Before Deleting
After Deleting

New contributor

user25549555 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật