ak
02/09/2021, 7:45 PMAndroidView
to display a MapView
in Compose. I am able to get the map to display fine, but there are interactions within my screen that require me to modify the map after a user clicks a button. i.e. displaying a route on the map after doing a network call from the Google Directions API.
The trouble I am having is my AndroidView
Composable update
block is not firing when my Directions API call is ready. e.g. when my mutableStateOf(directions)
changes.ak
02/09/2021, 7:45 PMAndroidView( MapView )
:ak
02/09/2021, 7:46 PM@SuppressLint("MissingPermission")
@Composable
fun StationDetailMap(
mapView: MapView,
stationViewModel: StationViewModel,
latitude: String?,
longitude: String?,
stationName: String?
) {
val coroutineScope = rememberCoroutineScope()
val userLocationState = stationViewModel.userLocation
val directions = stationViewModel.directions
AndroidView({
mapView
}) { map ->
coroutineScope.launch {
val googleMap = map.awaitMap()
googleMap.apply {
val latLng = LatLng(latitude!!.toDouble(), longitude!!.toDouble())
addMarker {
position(latLng)
title(stationName)
}
val boundsBuilder = LatLngBounds.builder()
boundsBuilder.include(latLng)
userLocationState.value?.let {
boundsBuilder.include(LatLng(it.latitude, it.longitude))
isMyLocationEnabled = true
}
val polylineOptions = PolylineOptions()
directions.value?.let {
it.routes.firstOrNull()?.overViewPolyline?.polylinePoints?.forEach {
polylineOptions.add(LatLng(it.lat, it.lng))
}
addPolyline(polylineOptions)
}
moveCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 100))
}
}
}
}
jim
02/09/2021, 7:51 PMAndroidView
should be creating the view, not referencing it.
The second lambda parameter is not composable, so it will not subscribe to updates. You will need to pull out / build up any relevant information above the invocation of AndroidView
and the only job of that lambda is to apply the changes to the underlying Android MapView.jim
02/09/2021, 7:54 PMak
02/09/2021, 7:56 PMjim
02/09/2021, 7:57 PMjim
02/09/2021, 7:57 PMjim
02/09/2021, 7:58 PMak
02/09/2021, 7:58 PMak
02/09/2021, 8:02 PM// The MapView lifecycle is handled by this composable. As the MapView also needs to be updated
// with input from Compose UI, those updates are encapsulated into the MapViewContainer
// composable. In this way, when an update to the MapView happens, this composable won't
// recompose and the MapView won't need to be recreated.
which sounds like the intended effect?Colton Idle
02/09/2021, 8:03 PMjim
02/09/2021, 8:06 PMak
02/09/2021, 8:07 PMak
02/09/2021, 8:07 PMval googleMap = map.awaitMap()
jim
02/09/2021, 8:07 PMak
02/09/2021, 8:08 PMjim
02/09/2021, 8:09 PMak
02/09/2021, 8:14 PMHalil Ozercan
02/09/2021, 8:16 PMComposeMapView
, map is re-drawn and re-initialized.Halil Ozercan
02/09/2021, 8:18 PMak
02/09/2021, 8:21 PM