Created
August 24, 2021 22:09
-
-
Save PiotrPrus/64c18ae4f74db2792a59dfca6ba14460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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