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
user25549555 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.