Skip to content

Instantly share code, notes, and snippets.

@PiotrPrus
Created August 24, 2021 22:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PiotrPrus/64c18ae4f74db2792a59dfca6ba14460 to your computer and use it in GitHub Desktop.
Save PiotrPrus/64c18ae4f74db2792a59dfca6ba14460 to your computer and use it in GitHub Desktop.
@Composable
fun MapSnapshot(map: MapView, location: LatLng) {
val mapBitmap: MutableState<Bitmap?> = remember { mutableStateOf(null) }
val coroutineScope = rememberCoroutineScope()
if (mapBitmap.value != null) {
Image(
bitmap = mapBitmap.value!!.asImageBitmap(),
contentDescription = "Map snapshot",
)
} else {
AndroidView({ map }) { mapView ->
coroutineScope.launch {
val googleMap = mapView.awaitMap()
val zoom = calculateZoom()
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, zoom))
googleMap.addMarker { position(location) }
googleMap.snapshot {
mapBitmap.value = it
}
}
}
}
DisposableEffect(location) {
mapBitmap.value = null
onDispose { mapBitmap.value = null }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment