Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.Button
Expand All @@ -49,7 +51,6 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
Expand All @@ -60,6 +61,7 @@ private const val TAG = "MapSampleActivity"

val singapore = LatLng(1.35, 103.87)
val singapore2 = LatLng(1.40, 103.77)
val defaultCameraPosition = CameraPosition.fromLatLngZoom(singapore, 11f)

class MapSampleActivity : ComponentActivity() {

Expand All @@ -69,7 +71,7 @@ class MapSampleActivity : ComponentActivity() {
var isMapLoaded by remember { mutableStateOf(false) }
// Observing and controlling the camera's state can be done with a CameraPositionState
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(singapore, 11f)
position = defaultCameraPosition
}

Box(Modifier.fillMaxSize()) {
Expand Down Expand Up @@ -106,6 +108,13 @@ fun GoogleMapView(
cameraPositionState: CameraPositionState,
onMapLoaded: () -> Unit,
) {
val singaporePositionState = rememberMarkerPositionState(position = singapore)
val singapore2PositionState = rememberMarkerPositionState(position = singapore2)
var circlePositionState by remember { mutableStateOf(singapore) }
if (singaporePositionState.dragState == DragState.END) {
circlePositionState = singaporePositionState.position
}

var uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) }
var shouldAnimateZoom by remember { mutableStateOf(true) }
var ticker by remember { mutableStateOf(0) }
Expand All @@ -129,22 +138,23 @@ fun GoogleMapView(
false
}
MarkerInfoWindowContent(
position = singapore,
positionState = singaporePositionState,
title = "Zoom in has been tapped $ticker times.",
onClick = markerClick,
draggable = true,
) {
Text(it.title ?: "Title", color = Color.Red)
}
MarkerInfoWindowContent(
position = singapore2,
positionState = singapore2PositionState,
title = "Marker with custom info window.\nZoom in has been tapped $ticker times.",
icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE),
onClick = markerClick,
) {
Text(it.title ?: "Title", color = Color.Blue)
}
Circle(
center = singapore,
center = circlePositionState,
fillColor = MaterialTheme.colors.secondary,
strokeColor = MaterialTheme.colors.secondaryVariant,
radius = 1000.0,
Expand All @@ -156,6 +166,20 @@ fun GoogleMapView(
Log.d("GoogleMap", "Selected map type $it")
mapProperties = mapProperties.copy(mapType = it)
})
Button(
modifier = Modifier.padding(4.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = MaterialTheme.colors.onPrimary,
contentColor = MaterialTheme.colors.primary
),
onClick = {
mapProperties = mapProperties.copy(mapType = MapType.NORMAL)
cameraPositionState.position = defaultCameraPosition
singaporePositionState.position = singapore
}
) {
Text(text = "RESET MAP", style = MaterialTheme.typography.body1)
}
val coroutineScope = rememberCoroutineScope()
ZoomControls(
shouldAnimateZoom,
Expand Down Expand Up @@ -186,7 +210,7 @@ fun GoogleMapView(
uiSettings = uiSettings.copy(zoomControlsEnabled = it)
}
)
DebugView(cameraPositionState)
DebugView(cameraPositionState, singaporePositionState)
}
}

Expand Down Expand Up @@ -263,7 +287,10 @@ private fun MapButton(text: String, onClick: () -> Unit) {
}

@Composable
private fun DebugView(cameraPositionState: CameraPositionState) {
private fun DebugView(
cameraPositionState: CameraPositionState,
markerPositionState: MarkerPositionState
) {
Column(
Modifier
.fillMaxWidth(),
Expand All @@ -273,5 +300,10 @@ private fun DebugView(cameraPositionState: CameraPositionState) {
if (cameraPositionState.isMoving) "moving" else "not moving"
Text(text = "Camera is $moving")
Text(text = "Camera position is ${cameraPositionState.position}")
Spacer(modifier = Modifier.height(4.dp))
val dragging =
if (markerPositionState.dragState == DragState.DRAG) "dragging" else "not dragging"
Text(text = "Marker is $dragging")
Text(text = "Marker position is ${markerPositionState.position}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.google.maps.android.compose

import androidx.compose.runtime.AbstractApplier
import androidx.compose.ui.platform.ComposeView
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.Circle
Expand Down Expand Up @@ -112,21 +111,24 @@ internal class MapApplier(
}
map.setOnMarkerDragListener(object : GoogleMap.OnMarkerDragListener {
override fun onMarkerDrag(marker: Marker) {
val markerDragState =
decorations.nodeForMarker(marker)?.markerDragState
markerDragState?.dragState = DragState.DRAG
with(decorations.nodeForMarker(marker)) {
this?.markerPositionState?.position = marker.position
this?.markerPositionState?.dragState = DragState.DRAG
}
}

override fun onMarkerDragEnd(marker: Marker) {
val markerDragState =
decorations.nodeForMarker(marker)?.markerDragState
markerDragState?.dragState = DragState.END
with(decorations.nodeForMarker(marker)) {
this?.markerPositionState?.position = marker.position
this?.markerPositionState?.dragState = DragState.END
}
}

override fun onMarkerDragStart(marker: Marker) {
val markerDragState =
decorations.nodeForMarker(marker)?.markerDragState
markerDragState?.dragState = DragState.START
with(decorations.nodeForMarker(marker)) {
this?.markerPositionState?.position = marker.position
this?.markerPositionState?.dragState = DragState.START
}
}
})
map.setInfoWindowAdapter(
Expand Down
Loading