I am developing a feature using Android's MapView where a callout should appear when a user taps on a polygon graphic. Currently, I am facing an issue where, although the polygon graphic is selected (graphic.isSelected = true), the callout does not display. The logcat outputs "show," indicating that the code to display the callout has been executed.
Here is the relevant part of my code in the fragment:
lifecycleScope.launch {
mapView = binding.mapView.apply {
setMap(requireContext())
}
mapView.graphicsOverlays.add(viewModel.graphicsOverlays)
calloutView =
LayoutInflater.from(requireContext()).inflate(R.layout.layout_callout, null).apply {
setOnClickListener {
showDetail()
}
}
mapView.singleTapConfirm(viewModel.graphicsOverlays, calloutView)
}
The singleTapConfirm and identifyGraphic functions are defined as follows:
suspend fun MapView.singleTapConfirm(graphicsOverlay: GraphicsOverlay, contentView: View? = null) {
onSingleTapConfirmed.collect { event ->
Log.e("singleTapConfirm","onSingleTapConfirmed")
graphicsOverlay.clearSelection()
// get screen point
val screenCoordinate = event.screenCoordinate
identifyGraphic(screenCoordinate, graphicsOverlay)?.let { selectedGraphic->
if (contentView != null) {
callout.show(contentView,selectedGraphic)
Log.e("singleTapConfirm","show")
selectedGraphic.isSelected = true
} else {
Log.e("contentView", "contentView is null")
}
} ?: run {
Log.e("contentView", "no graphics identified")
// callout.isAnimationEnabled = false
callout.dismiss()
}
}
}
suspend fun MapView.identifyGraphic(
screenCoordinate: ScreenCoordinate,
graphicsOverlay: GraphicsOverlay
): Graphic? {
val graphics = mutableListOf<Graphic>()
val identifyGraphicsOverlayResult =
identifyGraphicsOverlay(
graphicsOverlay,
screenCoordinate,
10.0,
false,
1
)
identifyGraphicsOverlayResult.getOrNull()?.let {
it.graphics.toMutableList().forEach { graphic ->
if (graphic.geometry is Polygon) {
graphics.add(graphic)
}
}
if (graphics.isNotEmpty()) return graphics[0]
} ?: run { graphicsOverlay.clearSelection() }
return null
}
I am not sure why the callout is not displaying even though the polygon is correctly identified and selected. Any suggestions or insights would be greatly appreciated.