Add Marker to Google Maps in Android using Jetpack Compose
In this article we will learn Integrate Google Map 's markers Into the Jetpack Compose App | Maps Compose Library
What if we want to mark places,
for example, nearest gas stations, hotels, or whatever you need? Library got us covered. We can use Markers for that.
Marker composable has a couple of parameters. We will go through the most commonly used, but feel free to check them all.
- state: MarkerState — used to control or observe the marker state such as its position and info window.
- draggable: Boolean — sets the draggability for the marker.
- flat: Boolean — sets if the marker should be flat against the map.
- icon: BitmapDescriptor — sets the icon for the marker.
- various lambdas like onClick, onInfoWindowClick, onInfoWindowClose and onInfoWindowLongClick.
@Composable
fun ComposeMapDemoMarkers() {
val singapore = LatLng(1.3554117053046808, 103.86454252780209)
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(singapore, 30f)
}
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState
) {
Marker(
state = rememberMarkerState(position = singapore),
title = "Marker1",
snippet = "Marker in Singapore",
icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)
)
}
}
@Composable
fun GoogleMarkers() {
Marker(
state = rememberMarkerState(position = LatLng(44.811058, 20.4617586)),
title = "Marker1",
icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)
)
Marker(
state = rememberMarkerState(position = LatLng(44.811058, 20.4627586)),
title = "Marker2",
icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)
)
Marker(
state = rememberMarkerState(position = LatLng(44.810058, 20.4627586)),
title = "Marker3",
icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)
)
Marker(
state = rememberMarkerState(position = LatLng(44.809058, 20.4627586)),
title = "Marker4",
icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)
)
Marker(
state = rememberMarkerState(position = LatLng(44.809058, 20.4617586)),
title = "Marker5",
icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)
)
}
Comments
Post a Comment