Un popover mostra i contenuti (di solito testo o immagini) in una finestra a forma di fumetto sopra la mappa, in una determinata posizione. Il popup ha un'area di contenuti e un gambo affusolato. La punta dello stelo è collegata a una posizione specificata sulla mappa.
In genere, un popup viene associato a un
indicatore, ma puoi anche
associarlo a coordinate LatLng specifiche.
Aggiungere un popover
Per aggiungere un popover, crea un oggetto Popover e imposta le relative opzioni, tra cui
la posizione e la modalità di altitudine. La posizione è un oggetto LatLng che include
latitudine, longitudine e altitudine, che determinano dove viene
visualizzato il popup. Se l'ancoraggio è a un indicatore, viene utilizzata la posizione dell'indicatore.
Puoi anche controllare l'interpretazione dell'altitudine impostando la modalità altitudine.
Il contenuto di un popover deve essere contenuto all'interno di un AndroidView. I popup sono scorrevoli per impostazione predefinita e hanno un'altezza massima predefinita.
Ancorare un popover a un indicatore
Puoi ancorare i popup ai marcatori. Quando aggiungi un popover ancorato a un indicatore,
devi associarlo a un oggetto Marker.
class MapManager(private val map: MapView) {
/**
* Adds a popover anchored to a marker.
* @param context The Context required to instantiate UI views.
*/
fun addPopoverToMarker(context: Context) {
// 1. Create a marker
val markerOptions = markerOptions {
position = latLngAltitude {
latitude = 37.422
longitude = -122.084
altitude = 0.0
}
}
val marker = map.addMarker(markerOptions) ?: return
// 2. Create the custom view using the passed-in context
val textView = TextView(context).apply {
text = context.getString(R.string.popover_hello)
setPadding(16, 16, 16, 16)
setBackgroundColor(Color.WHITE)
setTextColor(Color.BLACK)
}
// 3. Configure and display the popover
val options = popoverOptions {
content = textView
positionAnchor = marker
altitudeMode = AltitudeMode.RELATIVE_TO_GROUND
}
val popover = map.addPopover(options)
popover.show()
}
}
Aggiungere un popover configurato
Il seguente esempio aggiunge un popover che si chiude automaticamente quando l'utente tocca al di fuori del popover e non esegue automaticamente il panning sui popover appena aperti:
/**
* Adds a configured popover (auto-close enabled, auto-pan disabled).
* @param context The Context used to inflate the UI and retrieve string resources.
*/
fun addConfiguredPopover(context: Context) {
// 1. Initialize the view with the explicit context
val textView = TextView(context).apply {
text = context.getString(com.example.snippets.common.R.string.popover_info)
setPadding(12, 12, 12, 12)
setBackgroundColor(Color.WHITE)
setTextColor(Color.BLACK)
}
// 2. Configure popover behavior
val options = popoverOptions {
content = textView
// Setting a fixed coordinate anchor
positionAnchor = latLngAltitude {
latitude = 0.0
longitude = 0.0
altitude = 0.0
}
autoCloseEnabled = true // Closes automatically when the map is tapped elsewhere
autoPanEnabled = false // Map camera remains stationary when popover appears
}
// 3. Add to the map instance
map.addPopover(options)
}