Last active
June 18, 2023 16:16
-
-
Save oussama-dz/a83fa5e3ab124c10c06239e7e60034f0 to your computer and use it in GitHub Desktop.
Handle user interaction with the mapbox map.
This file contains hidden or 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 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