Last active
June 18, 2023 16:17
-
-
Save oussama-dz/0404612ce76f60294f58a6a07d6343f7 to your computer and use it in GitHub Desktop.
Mapbox composable using the AndroidView composable.
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 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