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

feat: add street view source parameter #1262

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ Full guides for using the utilities are published in
The StreetViewUtil class provides functionality to check whether a location is supported in StreetView. You can avoid errors when [adding a Street View panorama](https://developers.google.com/maps/documentation/android-sdk/streetview) to an Android app by calling this metadata utility and only adding a Street View panorama if the response is `OK`.

```kotlin
StreetViewUtils.fetchStreetViewData(LatLng(8.1425918, 11.5386121), BuildConfig.MAPS_API_KEY)
StreetViewUtils.fetchStreetViewData(LatLng(8.1425918, 11.5386121), BuildConfig.MAPS_API_KEY,Source.DEFAULT)
```

`fetchStreetViewData` will return `NOT_FOUND`, `OK`, `ZERO_RESULTS` or `REQUEST_DENIED`, depending on the response.

By default, the `Source` is set to `Source.DEFAULT`, but you can also specify `Source.OUTDOOR` to request outdoor Street View panoramas.

</details>

<details>
Expand Down
28 changes: 22 additions & 6 deletions library/src/main/java/com/google/maps/android/StreetViewUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,26 @@ class StreetViewUtils {
/**
* This function will check whether a location is available on StreetView or not.
*
* @param latLng Location to check
* @param apiKey Maps API Key
* @param latLng The `LatLng` object representing the location for which you want to fetch Street View data.
* @param apiKey The API key for Google Maps services.
* @param source The source of the Street View panorama. It is optional parameter and default value is `Source.DEFAULT`
* - `Source.DEFAULT`: Use the default Street View source.
* - `Source.OUTDOOR`: Use the outdoor Street View source.
* @return A Status value specifying if the location is available on Street View or not,
wangela marked this conversation as resolved.
Show resolved Hide resolved
* whether the used key is a right one, or any other error.
*/
suspend fun fetchStreetViewData(latLng: LatLng, apiKey: String): Status {
val urlString =
"https://maps.googleapis.com/maps/api/streetview/metadata?location=${latLng.latitude},${latLng.longitude}&key=$apiKey"
suspend fun fetchStreetViewData(
latLng: LatLng,
apiKey: String,
source: Source = Source.DEFAULT
): Status {

val urlString = buildString {
append("https://maps.googleapis.com/maps/api/streetview/metadata")
append("?location=${latLng.latitude},${latLng.longitude}")
append("&key=$apiKey")
append("&source=${source.value}")
}

return withContext(Dispatchers.IO) {
try {
Expand Down Expand Up @@ -72,7 +84,6 @@ class StreetViewUtils {
val jsonObject = JSONObject(responseString)
val statusString = jsonObject.optString("status")
val status = Status.valueOf(statusString)

return ResponseStreetView(status)
}
}
Expand All @@ -89,3 +100,8 @@ enum class Status {
INVALID_REQUEST,
UNKNOWN_ERROR
}

enum class Source(var value: String) {
DEFAULT("default"),
OUTDOOR("outdoor");
}