Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for StreetView. (#209)
* feat: Add support for StreetView. Change-Id: Id7f5b23a2458d77a08422886ef12799ffa0ebe7c * Add click support. Change-Id: Ie6ee4d41bbef9e9242fc3a33fd53dee8fca2d87a * Add sample. Change-Id: I8177feadc27d466f39e5d4e8d04b629afd241b4c * Adding properties to StreetView composable and updater. Change-Id: I416a77dbba34689d3c60d766bce2429fa3479641 * Use rememberUpdatedState. Change-Id: I945630a8e36edea272462fed72ccd1662efcb497 * Create StreetViewCameraPositionState. Change-Id: I7f7d0c165cf38738a624a8333c1d8bb35ecbf048 * Add test. Change-Id: Ib140373efa7fe265369ff3b81bfe2f9a4b202b24 * Clean tests Change-Id: I0c19a5ec5b716b22bffa05f0c916fb67fca745be * PR feedback. Change-Id: I4121f8774fedb592f0c9d9a86a153f0399bad666 * Add Street View example to README. * PR feedback * Add docs to StreetView composable.
- Loading branch information
Showing
17 changed files
with
554 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/src/androidTest/java/com/google/maps/android/compose/StreetViewTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.google.maps.android.compose | ||
|
||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.semantics.contentDescription | ||
import androidx.compose.ui.semantics.semantics | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import com.google.android.gms.maps.StreetViewPanoramaOptions | ||
import com.google.android.gms.maps.model.StreetViewPanoramaOrientation | ||
import com.google.maps.android.compose.streetview.StreetView | ||
import com.google.maps.android.compose.streetview.StreetViewCameraPositionState | ||
import com.google.maps.android.ktx.MapsExperimentalFeature | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class StreetViewTests { | ||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
private lateinit var cameraPositionState: StreetViewCameraPositionState | ||
private val initialLatLng = singapore | ||
|
||
@Before | ||
fun setUp() { | ||
cameraPositionState = StreetViewCameraPositionState() | ||
} | ||
|
||
@OptIn(MapsExperimentalFeature::class) | ||
private fun initStreetView(onClick: (StreetViewPanoramaOrientation) -> Unit = {}) { | ||
composeTestRule.setContent { | ||
StreetView( | ||
Modifier.semantics { contentDescription = "StreetView" }, | ||
cameraPositionState = cameraPositionState, | ||
streetViewPanoramaOptionsFactory = { | ||
StreetViewPanoramaOptions() | ||
.position(initialLatLng) | ||
}, | ||
onClick = onClick | ||
) | ||
} | ||
composeTestRule.waitUntil(8000) { | ||
cameraPositionState.location.position.latitude != 0.0 && | ||
cameraPositionState.location.position.longitude != 0.0 | ||
} | ||
} | ||
|
||
@Test | ||
fun testStartingStreetViewPosition() { | ||
initStreetView() | ||
initialLatLng.assertEquals(cameraPositionState.location.position) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app/src/main/java/com/google/maps/android/compose/StreetViewActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.google.maps.android.compose | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.background | ||
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.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.LazyRow | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Switch | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.runtime.snapshotFlow | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.style.LineHeightStyle | ||
import androidx.compose.ui.unit.dp | ||
import com.google.android.gms.maps.StreetViewPanoramaOptions | ||
import com.google.maps.android.compose.streetview.StreetView | ||
import com.google.maps.android.compose.streetview.rememberStreetViewCameraPositionState | ||
import com.google.maps.android.ktx.MapsExperimentalFeature | ||
import kotlinx.coroutines.launch | ||
|
||
class StreetViewActivity : ComponentActivity() { | ||
|
||
private val TAG = StreetViewActivity::class.java.simpleName | ||
|
||
@OptIn(MapsExperimentalFeature::class) | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
var isPanningEnabled by remember { mutableStateOf(false) } | ||
var isZoomEnabled by remember { mutableStateOf(false) } | ||
val camera = rememberStreetViewCameraPositionState() | ||
LaunchedEffect(camera) { | ||
launch { | ||
snapshotFlow { camera.panoramaCamera } | ||
.collect { | ||
Log.d(TAG, "Camera at: $it") | ||
} | ||
} | ||
launch { | ||
snapshotFlow { camera.location } | ||
.collect { | ||
Log.d(TAG, "Location at: $it") | ||
} | ||
} | ||
} | ||
Box(Modifier.fillMaxSize(), Alignment.BottomStart) { | ||
StreetView( | ||
Modifier.matchParentSize(), | ||
cameraPositionState = camera, | ||
streetViewPanoramaOptionsFactory = { | ||
StreetViewPanoramaOptions().position(singapore) | ||
}, | ||
isPanningGesturesEnabled = isPanningEnabled, | ||
isZoomGesturesEnabled = isZoomEnabled, | ||
onClick = { | ||
Log.d(TAG, "Street view clicked") | ||
}, | ||
onLongClick = { | ||
Log.d(TAG, "Street view long clicked") | ||
} | ||
) | ||
Column( | ||
Modifier | ||
.fillMaxWidth() | ||
.background(Color.White) | ||
.padding(8.dp) | ||
) { | ||
StreetViewSwitch(title = "Panning", checked = isPanningEnabled) { | ||
isPanningEnabled = it | ||
} | ||
StreetViewSwitch(title = "Zooming", checked = isZoomEnabled) { | ||
isZoomEnabled = it | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun StreetViewSwitch(title: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit) { | ||
Row(Modifier.padding(4.dp)) { | ||
Text(title) | ||
Spacer(Modifier.weight(1f)) | ||
Switch(checked = checked, onCheckedChange = onCheckedChange) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.