Integrate Google Maps Into the Jetpack Compose App | Add a map to your app
The Maps Compose library for the Maps SDK for Android is a set of open-source composable functions and data types that you can use with Jetpack Compose to build your app.
In this article we will learn Integrate Google Maps Into the Jetpack Compose App | Maps Compose Library
..
Overview
Some commonly used composable functions and data types include:
- Circle : Composable function to add a circle to a map.
- GoogleMap : Composable function to add a map.
- GroundOverlay : Composable function to add a ground overlay to a map.
- MapProperties : Data type for properties that can be modified on a map.
- MapUISettings : Data type for UI-related settings on a map.
- Marker : Composable function to add a marker to a map.
- Polygon : Composable function to add a polygon to a map.
- Polyline : Composable function to add a polyline to a map.
- TileOverlay : Composable function to add a tile overlay to a map.
..
Step 1: Library:
- To install the Maps Compose library in your Google Maps project:
- Add the following dependencies to your module-level build.gradle file: (app level gradle file)
dependencies {
implementation 'com.google.maps.android:maps-compose:2.7.2'
implementation 'com.google.android.gms:play-services-maps:18.0.2'
}
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="Enter your API key" />
- cameraPositionState: CameraPositionState — used to control or observe the map’s camera state.
- googleMapOptionsFactory: () -> GoogleMapOptions — the block for creating the GoogleMapOptions provided when the map is created.
- properties: MapProperties — properties of the map like isBuildingEnabled, isIndoorEnabled, isMyLocationEnabled, and so on. If isMyLocationEnabled is set to true, then we need to request permissions for fine and coarse locations. To learn how to request location permissions in Jetpack compose take a look at one of my previous articles.
- uiSettings: MapUiSettings— UI-specific settings on the map like compassEnabled, scrollGesturesEnabled, rotationGesturesEnabled, and so on.
- various lambdas like onMapClicked, onMapLoaded, onMyLocationButtonClick.
val singapore = LatLng(51.52061810406676, -0.12635325270312533)
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(singapore, 10f)
}
//https://www.google.com/maps/place/London,+UK/@51.5287352,-0.3817854,10z/data=!3m1!4b1!4m5!3m4!1s0x47d8a00baf21de75:0x52963a5addd52a99!8m2!3d51.5072178!4d-0.1275862
@Composable
fun ComposeDemoApp() {
val singapore = LatLng(51.52061810406676, -0.12635325270312533)
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(singapore, 10f)
}
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState
) {
Marker(
state = MarkerState(position = singapore),
title = "London",
snippet = "Marker in Big Ben"
)
}
}
Comments
Post a Comment