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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ composable elements to the content of the `GoogleMap`.
GoogleMap(
//...
) {
Marker(position = LatLng(-34, 151), title = "Marker in Sydney")
Marker(position = LatLng(35.66, 139.6), title = "Marker in Tokyo")
Marker(
state = MarkerState(position = LatLng(-34, 151)),
title = "Marker in Sydney"
)
Marker(
state = MarkerState(position = LatLng(35.66, 139.6)),
title = "Marker in Tokyo"
)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class GoogleMapViewTests {
composeTestRule.waitUntil(1000) {
cameraPositionState.isMoving
}
composeTestRule.waitUntil(1000) {
composeTestRule.waitUntil(3000) {
!cameraPositionState.isMoving
}
assertEquals(
Expand All @@ -118,7 +118,7 @@ class GoogleMapViewTests {
composeTestRule.waitUntil(1000) {
cameraPositionState.isMoving
}
composeTestRule.waitUntil(1000) {
composeTestRule.waitUntil(3000) {
!cameraPositionState.isMoving
}
assertEquals(
Expand All @@ -135,7 +135,7 @@ class GoogleMapViewTests {
composeTestRule.waitUntil(1000) {
cameraPositionState.isMoving
}
composeTestRule.waitUntil(1000) {
composeTestRule.waitUntil(3000) {
!cameraPositionState.isMoving
}
assertEquals(
Expand Down Expand Up @@ -184,6 +184,30 @@ class GoogleMapViewTests {
)
}

@Test(expected = IllegalStateException::class)
fun testMarkerStateCannotBeReused() {
val countDownLatch = CountDownLatch(1)
composeTestRule.setContent {
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState,
onMapLoaded = {
countDownLatch.countDown()
}
) {
val markerState = rememberMarkerState()
Marker(
state = markerState
)
Marker(
state = markerState
)
}
}
val mapLoaded = countDownLatch.await(30, TimeUnit.SECONDS)
assertTrue(mapLoaded)
}

private fun zoom(
shouldAnimate: Boolean,
zoomIn: Boolean,
Expand Down
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 Down Expand Up @@ -60,6 +62,7 @@ private const val TAG = "MapSampleActivity"
val singapore = LatLng(1.35, 103.87)
val singapore2 = LatLng(1.40, 103.77)
val singapore3 = LatLng(1.45, 103.77)
val defaultCameraPosition = CameraPosition.fromLatLngZoom(singapore, 11f)

class MapSampleActivity : ComponentActivity() {

Expand All @@ -69,7 +72,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 +109,14 @@ fun GoogleMapView(
cameraPositionState: CameraPositionState,
onMapLoaded: () -> Unit,
) {
val singaporeState = rememberMarkerState(position = singapore)
val singapore2State = rememberMarkerState(position = singapore2)
val singapore3State = rememberMarkerState(position = singapore3)
var circleCenter by remember { mutableStateOf(singapore) }
if (singaporeState.dragState == DragState.END) {
circleCenter = singaporeState.position
}

var uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) }
var shouldAnimateZoom by remember { mutableStateOf(true) }
var ticker by remember { mutableStateOf(0) }
Expand All @@ -132,27 +143,28 @@ fun GoogleMapView(
false
}
MarkerInfoWindowContent(
position = singapore,
state = singaporeState,
title = "Zoom in has been tapped $ticker times.",
onClick = markerClick,
draggable = true,
) {
Text(it.title ?: "Title", color = Color.Red)
}
MarkerInfoWindowContent(
position = singapore2,
state = singapore2State,
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)
}
Marker(
position = singapore3,
state = singapore3State,
title = "Marker in Singapore",
onClick = markerClick
)
Circle(
center = singapore,
center = circleCenter,
fillColor = MaterialTheme.colors.secondary,
strokeColor = MaterialTheme.colors.secondaryVariant,
radius = 1000.0,
Expand All @@ -164,6 +176,21 @@ 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
singaporeState.position = singapore
singaporeState.hideInfoWindow()
}
) {
Text(text = "RESET MAP", style = MaterialTheme.typography.body1)
}
val coroutineScope = rememberCoroutineScope()
ZoomControls(
shouldAnimateZoom,
Expand Down Expand Up @@ -194,7 +221,7 @@ fun GoogleMapView(
uiSettings = uiSettings.copy(zoomControlsEnabled = it)
}
)
DebugView(cameraPositionState)
DebugView(cameraPositionState, singaporeState)
}
}

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

@Composable
private fun DebugView(cameraPositionState: CameraPositionState) {
private fun DebugView(
cameraPositionState: CameraPositionState,
markerState: MarkerState
) {
Column(
Modifier
.fillMaxWidth(),
Expand All @@ -281,5 +311,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 (markerState.dragState == DragState.DRAG) "dragging" else "not dragging"
Text(text = "Marker is $dragging")
Text(text = "Marker position is ${markerState.position}")
}
}
}
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ext.projectArtifactId = { project ->

allprojects {
group = 'com.google.maps.android'
version = '1.3.1'
version = '2.0.0-SNAPSHOT'
project.ext.artifactId = rootProject.ext.projectArtifactId(project)
}

Expand Down Expand Up @@ -125,8 +125,10 @@ subprojects { project ->

repositories {
maven {
def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
name = "mavencentral"
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username sonatypeUsername
password sonatypePassword
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,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?.markerState?.position = marker.position
this?.markerState?.dragState = DragState.DRAG
}
}

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

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