Skip to content

Commit

Permalink
feat: Add support for StreetView. (#209)
Browse files Browse the repository at this point in the history
* 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
arriolac committed Dec 16, 2022
1 parent e710beb commit aabe02c
Show file tree
Hide file tree
Showing 17 changed files with 554 additions and 16 deletions.
18 changes: 16 additions & 2 deletions README.md
Expand Up @@ -127,7 +127,7 @@ GoogleMap(
}
```

#### Customizing a marker's info window
### Customizing a marker's info window

You can customize a marker's info window contents by using the
`MarkerInfoWindowContent` element, or if you want to customize the entire info
Expand All @@ -153,7 +153,21 @@ MarkerInfoWindow(
}
```

#### Obtaining Access to the raw GoogleMap (Experimental)
### Street View

You can add a Street View given a location using the `StreetView` composable.
To use it, provide a `StreetViewPanoramaOptions` object as follows:

```kotlin
val singapore = LatLng(1.35, 103.87)
StreetView(
streetViewPanoramaOptionsFactory = {
StreetViewPanoramaOptions().position(singapore)
}
)
```

### Obtaining Access to the raw GoogleMap (Experimental)

Certain use cases require extending the `GoogleMap` object to decorate / augment
the map. For example, while marker clustering is not yet supported by Maps Compose
Expand Down
@@ -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)
}
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -54,6 +54,9 @@
<activity
android:name=".ScaleBarActivity"
android:exported="false"/>
<activity
android:name=".StreetViewActivity"
android:exported="false"/>

<!-- Used by createComponentActivity() for unit testing -->
<activity android:name="androidx.activity.ComponentActivity" />
Expand Down
Expand Up @@ -61,10 +61,10 @@ import kotlinx.coroutines.launch

private const val TAG = "BasicMapActivity"

private val singapore = LatLng(1.35, 103.87)
private val singapore2 = LatLng(1.40, 103.77)
private val singapore3 = LatLng(1.45, 103.77)
private val defaultCameraPosition = CameraPosition.fromLatLngZoom(singapore, 11f)
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 BasicMapActivity : ComponentActivity() {

Expand Down
Expand Up @@ -30,8 +30,6 @@ import kotlin.random.Random

private const val TAG = "LocationTrackActivity"
private const val zoom = 8f
private val singapore = LatLng(1.35, 103.87)
private val defaultCameraPosition = CameraPosition.fromLatLngZoom(singapore, zoom)

/**
* This shows how to use a custom location source to show a blue dot on the map based on your own
Expand Down
Expand Up @@ -105,6 +105,13 @@ class MainActivity : ComponentActivity() {
}) {
Text(getString(R.string.scale_bar_activity))
}
Spacer(modifier = Modifier.padding(5.dp))
Button(
onClick = {
context.startActivity(Intent(context, StreetViewActivity::class.java))
}) {
Text(getString(R.string.street_view))
}
}
}
}
Expand Down
Expand Up @@ -21,8 +21,6 @@ import com.google.maps.android.clustering.ClusterItem
import com.google.maps.android.clustering.ClusterManager
import kotlin.random.Random

private val singapore = LatLng(1.35, 103.87)
private val singapore2 = LatLng(2.50, 103.87)
private val TAG = MapClusteringActivity::class.simpleName

class MapClusteringActivity : ComponentActivity() {
Expand Down
Expand Up @@ -43,9 +43,6 @@ import com.google.android.gms.maps.model.Marker

private const val TAG = "ScrollingMapActivity"

private val singapore = LatLng(1.35, 103.87)
private val defaultCameraPosition = CameraPosition.fromLatLngZoom(singapore, 11f)

class MapInColumnActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Expand Up @@ -42,8 +42,6 @@ import com.google.maps.android.compose.widgets.ScaleBar
private const val TAG = "ScaleBarActivity"

private const val zoom = 8f
private val singapore = LatLng(1.35, 103.87)
private val defaultCameraPosition = CameraPosition.fromLatLngZoom(singapore, zoom)

class ScaleBarActivity : ComponentActivity() {

Expand Down
@@ -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)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -22,4 +22,5 @@
<string name="map_clustering_activity">Map Clustering</string>
<string name="location_tracking_activity">Location Tracking</string>
<string name="scale_bar_activity">Scale Bar</string>
<string name="street_view">Street View</string>
</resources>
Expand Up @@ -138,7 +138,7 @@ public fun GoogleMap(
}
}

private suspend inline fun disposingComposition(factory: () -> Composition) {
internal suspend inline fun disposingComposition(factory: () -> Composition) {
val composition = factory()
try {
awaitCancellation()
Expand Down

0 comments on commit aabe02c

Please sign in to comment.