Skip to content

Commit

Permalink
feat: added street view source parameter to fetchStreetViewData (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufonderd committed Nov 6, 2023
1 parent 233089a commit 2fd3921
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,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,
* 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");
}

0 comments on commit 2fd3921

Please sign in to comment.