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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added StreetViewUtil sample #435

Merged
merged 7 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ StreetView(
)
```

In order to test whether that Street View location is valid, we recommend using the
`fetchStreetViewData` utility from the Android Map Utils library. We showcase an example of this
in `StreetViewActivity`:

```kotlin
streetViewResult =
fetchStreetViewData(singapore, BuildConfig.MAPS_API_KEY)
```

Please make sure that this API Key has Street View Static API activated.

</details>

<details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,31 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.google.android.gms.maps.StreetViewPanoramaOptions
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.Status
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
import com.google.maps.android.StreetViewUtils.Companion.fetchStreetViewData
wangela marked this conversation as resolved.
Show resolved Hide resolved

class StreetViewActivity : ComponentActivity() {

private val TAG = StreetViewActivity::class.java.simpleName

// This is an invalid location. If you use it instead of Singapore, the StreetViewUtils
// will return NOT_FOUND.
val invalidLocation = LatLng(32.429634, -96.828891)

@OptIn(MapsExperimentalFeature::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
var isPanningEnabled by remember { mutableStateOf(false) }
var isZoomEnabled by remember { mutableStateOf(false) }
var streetViewResult by remember { mutableStateOf(Status.NOT_FOUND) }

val camera = rememberStreetViewCameraPositionState()
LaunchedEffect(camera) {
launch {
Expand All @@ -69,41 +79,51 @@ class StreetViewActivity : ComponentActivity() {
Log.d(TAG, "Location at: $it")
}
}
launch {
// Make sure that this API Key has Street View Static API activated.
streetViewResult =
fetchStreetViewData(singapore, BuildConfig.MAPS_API_KEY)
}
}
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
if (streetViewResult == Status.OK) {
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
}
}
} else {
Text("Location not available.")
}
}
}
}
}


@Composable
fun StreetViewSwitch(title: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit) {
Row(Modifier.padding(4.dp)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.maps.android.compose.streetview

import android.content.ComponentCallbacks
Expand All @@ -23,14 +37,17 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.google.android.gms.maps.StreetViewPanoramaOptions
import com.google.android.gms.maps.StreetViewPanoramaView
import com.google.android.gms.maps.model.StreetViewPanoramaCamera
import com.google.android.gms.maps.model.StreetViewPanoramaOrientation
import com.google.maps.android.compose.disposingComposition
import com.google.maps.android.ktx.MapsExperimentalFeature
import com.google.maps.android.ktx.awaitStreetViewPanorama

/**
* A composable for displaying a Street View for a given location.
* A composable for displaying a Street View for a given location. A location might not be available for a given
* set of coordinates. We recommend you to check our sample on [StreetViewActivity] using our utility function
* in [StreetViewUtils] to manage non-existing locations.
wangela marked this conversation as resolved.
Show resolved Hide resolved
*
*
*
* @param modifier Modifier to be applied to the StreetView
* @param cameraPositionState the [StreetViewCameraPositionState] to be used to control or observe
Expand Down
Loading