Select to view content in your preferred language

Displaying Callout

350
2
04-22-2024 10:59 PM
ChanceJason
Emerging Contributor

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.

0 Kudos
2 Replies
ChanceJason
Emerging Contributor

sometimes it can show, but sometimes can not

0 Kudos
PuneetPrakash
Esri Contributor

Can you make sure that `selectedGraphic` always has a valid geometry associated with it when you call,

callout.show(contentView,selectedGraphic)


in the scenarios when the callout does show up vs when it does not show. Also check for the validity of the contentView. Try testing your code by showing the callout with a simple contentView which is just a text view. 

contentView = TextView(applicationContext).apply {
            text = "lorem ipsum"
        }
0 Kudos