Skip to content

Commit

Permalink
feat: Add support to get projection. (#53)
Browse files Browse the repository at this point in the history
* feat: Add support to get projection.

Change-Id: I999c56e6a31e33dd4c8d20215d24f56509a3e967

* Add tests for projection.

Change-Id: Ia1a9b3dd95080e5df62e4aadf5fd46b03d82175b

* Update waitUntil duration.

Change-Id: Ic71bdd2fa8efe9a7a949d96b25010ee744c54a68
  • Loading branch information
arriolac committed Mar 10, 2022
1 parent d1df65e commit e085588
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.annotation.UiThreadTest
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -86,7 +88,7 @@ class GoogleMapViewTests {
composeTestRule.waitUntil(1000) {
cameraPositionState.isMoving
}
composeTestRule.waitUntil(1000) {
composeTestRule.waitUntil(3000) {
!cameraPositionState.isMoving
}
assertFalse(cameraPositionState.isMoving)
Expand Down Expand Up @@ -150,7 +152,7 @@ class GoogleMapViewTests {
composeTestRule.waitUntil(1000) {
cameraPositionState.isMoving
}
composeTestRule.waitUntil(1000) {
composeTestRule.waitUntil(3000) {
!cameraPositionState.isMoving
}
assertEquals(
Expand All @@ -161,7 +163,32 @@ class GoogleMapViewTests {
}
}

private fun zoom(shouldAnimate: Boolean, zoomIn: Boolean, assertionBlock: () -> Unit) {
@Test
@UiThreadTest
fun testLatLngInVisibleRegion() {
val projection = cameraPositionState.projection
assertNotNull(projection)
assertTrue(
projection!!.visibleRegion.latLngBounds.contains(startingPosition)
)
}

@Test
@UiThreadTest
fun testLatLngNotInVisibleRegion() {
val projection = cameraPositionState.projection
assertNotNull(projection)
val latLng = LatLng(23.4, 25.6)
assertFalse(
projection!!.visibleRegion.latLngBounds.contains(latLng)
)
}

private fun zoom(
shouldAnimate: Boolean,
zoomIn: Boolean,
assertionBlock: () -> Unit
) {
if (!shouldAnimate) {
composeTestRule.onNodeWithTag("cameraAnimations")
.assertIsDisplayed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 +59,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)

class MapSampleActivity : ComponentActivity() {

Expand Down Expand Up @@ -126,6 +126,9 @@ fun GoogleMapView(
// Drawing on the map is accomplished with a child-based API
val markerClick: (Marker) -> Boolean = {
Log.d(TAG, "${it.title} was clicked")
cameraPositionState.projection?.let { projection ->
Log.d(TAG, "The current projection is: $projection")
}
false
}
MarkerInfoWindowContent(
Expand All @@ -143,6 +146,11 @@ fun GoogleMapView(
) {
Text(it.title ?: "Title", color = Color.Blue)
}
Marker(
position = singapore3,
title = "Marker in Singapore",
onClick = markerClick
)
Circle(
center = singapore,
fillColor = MaterialTheme.colors.secondary,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.runtime.setValue
import com.google.android.gms.maps.CameraUpdate
import com.google.android.gms.maps.CameraUpdateFactory
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
Expand Down Expand Up @@ -65,6 +66,13 @@ class CameraPositionState(
var isMoving by mutableStateOf(false)
internal set

/**
* Returns the current [Projection] to be used for converting between screen
* coordinates and lat/lng.
*/
val projection: Projection?
get() = map?.projection

/**
* Local source of truth for the current camera position.
* While [map] is non-null this reflects the current position of [map] as it changes.
Expand Down

0 comments on commit e085588

Please sign in to comment.