-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: added StreetViewUtil * docs: added documentation for StreetViewUtil
- Loading branch information
Showing
12 changed files
with
200 additions
and
20 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
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
15 changes: 0 additions & 15 deletions
15
demo/src/main/java/com/google/maps/android/utils/demo/SampleManager.java
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
demo/src/main/java/com/google/maps/android/utils/demo/StreetViewDemoActivity.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,30 @@ | ||
package com.google.maps.android.utils.demo | ||
|
||
import android.app.Activity | ||
import android.os.Bundle | ||
import android.widget.TextView | ||
import com.google.android.gms.maps.model.LatLng | ||
import com.google.maps.android.StreetViewUtils | ||
import kotlinx.coroutines.DelicateCoroutinesApi | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
|
||
class StreetViewDemoActivity : Activity() { | ||
|
||
@OptIn(DelicateCoroutinesApi::class) | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.street_view_demo) | ||
|
||
GlobalScope.launch(Dispatchers.Main) { | ||
val response1 = | ||
StreetViewUtils.fetchStreetViewData(LatLng(48.1425918, 11.5386121), BuildConfig.MAPS_API_KEY) | ||
val response2 = StreetViewUtils.fetchStreetViewData(LatLng(8.1425918, 11.5386121), BuildConfig.MAPS_API_KEY) | ||
|
||
findViewById<TextView>(R.id.textViewFirstLocation).text = "Location 1 is supported in StreetView: $response1" | ||
findViewById<TextView>(R.id.textViewSecondLocation).text = "Location 2 is supported in StreetView: $response2" | ||
} | ||
} | ||
} | ||
|
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,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<TextView | ||
android:id="@+id/textViewFirstLocation" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" /> | ||
<TextView | ||
android:id="@+id/textViewSecondLocation" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" /> | ||
</LinearLayout> |
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
86 changes: 86 additions & 0 deletions
86
library/src/main/java/com/google/maps/android/StreetViewUtil.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,86 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* 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 | ||
|
||
import kotlinx.coroutines.withContext | ||
import java.io.IOException | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
import com.google.android.gms.maps.model.LatLng | ||
import kotlinx.coroutines.Dispatchers | ||
import org.json.JSONObject | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
|
||
|
||
/** | ||
* Utility functions for StreetView | ||
*/ | ||
class StreetViewUtils { | ||
companion object { | ||
|
||
/** | ||
* This function will check whether a location is available on StreetView or not. | ||
* | ||
* @param latLng Location to check | ||
* @param apiKey Maps API Key | ||
* @return A boolean value specifying if the location is available on Street View or not. | ||
*/ | ||
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" | ||
|
||
return withContext(Dispatchers.IO) { | ||
try { | ||
val url = URL(urlString) | ||
val connection = url.openConnection() as HttpURLConnection | ||
connection.requestMethod = "GET" | ||
|
||
val responseCode = connection.responseCode | ||
if (responseCode == HttpURLConnection.HTTP_OK) { | ||
val inputStream = connection.inputStream | ||
val bufferedReader = BufferedReader(InputStreamReader(inputStream)) | ||
val responseString = bufferedReader.use { it.readText() } | ||
bufferedReader.close() | ||
inputStream.close() | ||
deserializeResponse(responseString).status | ||
} else { | ||
throw IOException("HTTP Error: $responseCode") | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
throw IOException("Network error: ${e.message}") | ||
} | ||
} | ||
} | ||
|
||
private fun deserializeResponse(responseString: String): ResponseStreetView { | ||
val jsonObject = JSONObject(responseString) | ||
val statusString = jsonObject.optString("status") | ||
val status = Status.valueOf(statusString) | ||
|
||
return ResponseStreetView(status) | ||
} | ||
} | ||
} | ||
|
||
data class ResponseStreetView(val status: Status) | ||
|
||
enum class Status { | ||
OK, | ||
ZERO_RESULTS, | ||
NOT_FOUND | ||
} |
39 changes: 39 additions & 0 deletions
39
library/src/test/java/com/google/maps/android/StreetViewUtilTest.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,39 @@ | ||
package com.google.maps.android | ||
|
||
import com.google.android.gms.maps.model.LatLng | ||
import io.mockk.coEvery | ||
import io.mockk.mockkObject | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class StreetViewUtilsTest { | ||
|
||
lateinit var latLng : LatLng | ||
|
||
val apiKey = "AN_API_KEY" | ||
|
||
@Before | ||
fun setUp() { | ||
latLng = LatLng(37.7749, -122.4194) // San Francisco coordinates | ||
|
||
// Mock the network behavior using MockK | ||
mockkObject(StreetViewUtils) | ||
coEvery { StreetViewUtils.fetchStreetViewData(any(), any()) } returns Status.NOT_FOUND | ||
coEvery { StreetViewUtils.fetchStreetViewData(latLng, apiKey) } returns Status.OK | ||
} | ||
|
||
@Test | ||
fun testLocationFoundOnStreetView() = runBlocking { | ||
val status = StreetViewUtils.fetchStreetViewData(latLng, apiKey) | ||
assertEquals(Status.OK, status) | ||
} | ||
|
||
@Test | ||
fun testLocationNotFoundOnStreetView() = runBlocking { | ||
val status = StreetViewUtils.fetchStreetViewData(LatLng(10.0, 20.0), apiKey) | ||
assertEquals(Status.NOT_FOUND, status) | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
MAPS_API_KEY="YOUR_API_KEY" | ||
MAPS_API_KEY="AIzaSyA4SCF8hbjd5M3pq-FncAtSu1uTe_yaQBg" |