Skip to content

Instantly share code, notes, and snippets.

@oussama-dz
Last active June 18, 2023 16:16
Show Gist options
  • Select an option

  • Save oussama-dz/a83fa5e3ab124c10c06239e7e60034f0 to your computer and use it in GitHub Desktop.

Select an option

Save oussama-dz/a83fa5e3ab124c10c06239e7e60034f0 to your computer and use it in GitHub Desktop.
Handle user interaction with the mapbox map.
@Composable
fun MapBoxMap(
modifier: Modifier = Modifier,
onPointChange: (Point) -> Unit,
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()
mapView.getMapboxMap().addOnMapClickListener { p ->
onPointChange(p)
true
}
}
},
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