Skip to content

Instantly share code, notes, and snippets.

@oussama-dz
Last active June 18, 2023 16:17
Show Gist options
  • Save oussama-dz/0404612ce76f60294f58a6a07d6343f7 to your computer and use it in GitHub Desktop.
Save oussama-dz/0404612ce76f60294f58a6a07d6343f7 to your computer and use it in GitHub Desktop.
Mapbox composable using the AndroidView composable.
@Composable
fun MapBoxMap(
modifier: Modifier = Modifier,
point: Point?,
) {
val context = LocalContext.current
val marker = remember(context) {
context.getDrawable(R.drawable.marker)!!.toBitmap()
}
var pointAnnotationManager: PointAnnotationManager? by remember {
mutableStateOf(null)
}
AndroidView(
factory = {
MapView(it).also { mapView ->
mapView.getMapboxMap().loadStyleUri(Style.TRAFFIC_DAY)
val annotationApi = mapView.annotations
pointAnnotationManager = annotationApi.createPointAnnotationManager()
}
},
update = { mapView ->
if (point != null) {
pointAnnotationManager?.let {
it.deleteAll()
val pointAnnotationOptions = PointAnnotationOptions()
.withPoint(point)
.withIconImage(marker)
it.create(pointAnnotationOptions)
mapView.getMapboxMap()
.flyTo(CameraOptions.Builder().zoom(16.0).center(point).build())
}
}
NoOpUpdate
},
modifier = modifier
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment