Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the reason for map camera movement #154

Merged
merged 5 commits into from Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.google.android.gms.maps.GoogleMap.OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import org.junit.Assert.*
Expand Down Expand Up @@ -75,11 +76,13 @@ class GoogleMapViewTests {
@Test
fun testCameraReportsMoving() {
initMap()
assertEquals(-1, cameraPositionState.cameraMoveStartedReason)
zoom(shouldAnimate = true, zoomIn = true) {
composeTestRule.waitUntil(1000) {
cameraPositionState.isMoving
}
assertTrue(cameraPositionState.isMoving)
assertEquals(REASON_DEVELOPER_ANIMATION, cameraPositionState.cameraMoveStartedReason)
}
}

Expand Down
Expand Up @@ -67,7 +67,7 @@ class LocationTrackingActivity : AppCompatActivity() {
setContent {
var isMapLoaded by remember { mutableStateOf(false) }

// To control the map camera
// To control and observe the map camera
val cameraPositionState = rememberCameraPositionState {
position = defaultCameraPosition
}
Expand All @@ -88,6 +88,21 @@ class LocationTrackingActivity : AppCompatActivity() {
cameraPositionState.animate(CameraUpdateFactory.newCameraPosition(cameraPosition), 1_000)
}

// Detect when the map starts moving and print the reason
LaunchedEffect(cameraPositionState.isMoving) {
if (cameraPositionState.isMoving) {
val reason = when(cameraPositionState.cameraMoveStartedReason) {
// See https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnCameraMoveStartedListener#constants
-1 -> "DEFAULT_NO_MOVEMENT_YET"
1 -> "REASON_GESTURE"
2 -> "REASON_API_ANIMATION"
3 -> "REASON_DEVELOPER_ANIMATION"
else -> "UNKNOWN"
}
Log.d(TAG, "Map camera started moving due to $reason")
}
}

Box(Modifier.fillMaxSize()) {
GoogleMap(
modifier = Modifier.matchParentSize(),
Expand Down
Expand Up @@ -27,12 +27,7 @@ import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.Projection
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Use single name imports here. I have my preferences configured to the following:

image

This should probably be enforced in the linter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch - I fixed the imports to not use wildcards and updated my AS settings.

import java.lang.Integer.MAX_VALUE
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
Expand Down Expand Up @@ -67,6 +62,13 @@ public class CameraPositionState(
public var isMoving: Boolean by mutableStateOf(false)
internal set

/**
* The reason for the start of the most recent camera moment, or -1 if the camera hasn't moved
* yet. See constant definitions at https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnCameraMoveStartedListener#constants.
*/
public var cameraMoveStartedReason: Int by mutableStateOf(-1)
internal set

/**
* Returns the current [Projection] to be used for converting between screen
* coordinates and lat/lng.
Expand Down
Expand Up @@ -69,6 +69,7 @@ internal class MapPropertiesNode(
cameraPositionState.isMoving = false
}
map.setOnCameraMoveStartedListener {
cameraPositionState.cameraMoveStartedReason = it
cameraPositionState.isMoving = true
}
map.setOnCameraMoveListener {
Expand Down