Skip to content

Commit d5f727f

Browse files
authored
chore: Add StreetViewPanoramaOptionsDemoActivity in Kotlin (googlemaps-samples#208)
Also apply following changes to Java version: * Refactor variables to remove Hungarian notation * Remove redundant cast for TextView * Normalize instance variable names * Normalize capitalization of activity title in UI list * Use lambda (requires Java 8 support) * Support Java 8 features (for all Java demos) via build.gradle
1 parent 62c06a8 commit d5f727f

File tree

8 files changed

+248
-40
lines changed

8 files changed

+248
-40
lines changed

ApiDemos/java/app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ android {
4646
versionNameSuffix "-v3"
4747
}
4848
}
49+
compileOptions {
50+
sourceCompatibility JavaVersion.VERSION_1_8
51+
targetCompatibility JavaVersion.VERSION_1_8
52+
}
4953
}
5054

5155
dependencies {

ApiDemos/java/app/src/gms/java/com/example/mapdemo/StreetViewPanoramaOptionsDemoActivity.java

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515

1616
package com.example.mapdemo;
1717

18-
import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
19-
import com.google.android.gms.maps.StreetViewPanorama;
20-
import com.google.android.gms.maps.SupportStreetViewPanoramaFragment;
21-
import com.google.android.gms.maps.model.LatLng;
22-
import com.google.android.gms.maps.model.StreetViewSource;
23-
2418
import android.os.Bundle;
2519
import android.view.View;
2620
import android.widget.CheckBox;
2721
import android.widget.Toast;
2822

2923
import androidx.appcompat.app.AppCompatActivity;
3024

25+
import com.google.android.gms.maps.StreetViewPanorama;
26+
import com.google.android.gms.maps.SupportStreetViewPanoramaFragment;
27+
import com.google.android.gms.maps.model.LatLng;
28+
import com.google.android.gms.maps.model.StreetViewSource;
29+
3130
/**
3231
* This shows how to create an activity with static streetview (all options have been switched off)
3332
*/
@@ -38,62 +37,58 @@ public class StreetViewPanoramaOptionsDemoActivity extends AppCompatActivity {
3837

3938
private static int RADIUS = 20;
4039

41-
private StreetViewPanorama mStreetViewPanorama;
40+
private StreetViewPanorama streetViewPanorama;
4241

43-
private CheckBox mStreetNameCheckbox;
42+
private CheckBox streetNameCheckbox;
4443

45-
private CheckBox mNavigationCheckbox;
44+
private CheckBox navigationCheckbox;
4645

47-
private CheckBox mZoomCheckbox;
46+
private CheckBox zoomCheckbox;
4847

49-
private CheckBox mPanningCheckbox;
48+
private CheckBox panningCheckbox;
5049

51-
private CheckBox mOutdoor;
50+
private CheckBox outdoorCheckbox;
5251

5352
@Override
5453
protected void onCreate(final Bundle savedInstanceState) {
5554
super.onCreate(savedInstanceState);
5655
setContentView(R.layout.street_view_panorama_options_demo);
5756

58-
mStreetNameCheckbox = (CheckBox) findViewById(R.id.streetnames);
59-
mNavigationCheckbox = (CheckBox) findViewById(R.id.navigation);
60-
mZoomCheckbox = (CheckBox) findViewById(R.id.zoom);
61-
mPanningCheckbox = (CheckBox) findViewById(R.id.panning);
62-
mOutdoor = (CheckBox) findViewById(R.id.outdoor);
57+
streetNameCheckbox = findViewById(R.id.streetnames);
58+
navigationCheckbox = findViewById(R.id.navigation);
59+
zoomCheckbox = findViewById(R.id.zoom);
60+
panningCheckbox = findViewById(R.id.panning);
61+
outdoorCheckbox = findViewById(R.id.outdoor);
6362

6463
SupportStreetViewPanoramaFragment streetViewPanoramaFragment =
6564
(SupportStreetViewPanoramaFragment)
6665
getSupportFragmentManager().findFragmentById(R.id.streetviewpanorama);
6766
streetViewPanoramaFragment.getStreetViewPanoramaAsync(
68-
new OnStreetViewPanoramaReadyCallback() {
69-
@Override
70-
public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
71-
mStreetViewPanorama = panorama;
72-
mStreetViewPanorama.setStreetNamesEnabled(mStreetNameCheckbox.isChecked());
73-
mStreetViewPanorama
74-
.setUserNavigationEnabled(mNavigationCheckbox.isChecked());
75-
mStreetViewPanorama.setZoomGesturesEnabled(mZoomCheckbox.isChecked());
76-
mStreetViewPanorama.setPanningGesturesEnabled(mPanningCheckbox.isChecked());
77-
78-
// Only set the panorama to SAN_FRAN on startup (when no panoramas have been
79-
// loaded which is when the savedInstanceState is null).
80-
if (savedInstanceState == null) {
81-
setPosition();
82-
}
67+
panorama -> {
68+
streetViewPanorama = panorama;
69+
panorama.setStreetNamesEnabled(streetNameCheckbox.isChecked());
70+
panorama.setUserNavigationEnabled(navigationCheckbox.isChecked());
71+
panorama.setZoomGesturesEnabled(zoomCheckbox.isChecked());
72+
panorama.setPanningGesturesEnabled(panningCheckbox.isChecked());
73+
74+
// Only set the panorama to SAN_FRAN on startup (when no panoramas have been
75+
// loaded which is when the savedInstanceState is null).
76+
if (savedInstanceState == null) {
77+
setPosition();
8378
}
8479
});
8580
}
8681

8782
private void setPosition() {
88-
mStreetViewPanorama.setPosition(
83+
streetViewPanorama.setPosition(
8984
SAN_FRAN,
9085
RADIUS,
91-
mOutdoor.isChecked() ? StreetViewSource.OUTDOOR : StreetViewSource.DEFAULT
86+
outdoorCheckbox.isChecked() ? StreetViewSource.OUTDOOR : StreetViewSource.DEFAULT
9287
);
9388
}
9489

9590
private boolean checkReady() {
96-
if (mStreetViewPanorama == null) {
91+
if (streetViewPanorama == null) {
9792
Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show();
9893
return false;
9994
}
@@ -104,28 +99,28 @@ public void onStreetNamesToggled(View view) {
10499
if (!checkReady()) {
105100
return;
106101
}
107-
mStreetViewPanorama.setStreetNamesEnabled(mStreetNameCheckbox.isChecked());
102+
streetViewPanorama.setStreetNamesEnabled(streetNameCheckbox.isChecked());
108103
}
109104

110105
public void onNavigationToggled(View view) {
111106
if (!checkReady()) {
112107
return;
113108
}
114-
mStreetViewPanorama.setUserNavigationEnabled(mNavigationCheckbox.isChecked());
109+
streetViewPanorama.setUserNavigationEnabled(navigationCheckbox.isChecked());
115110
}
116111

117112
public void onZoomToggled(View view) {
118113
if (!checkReady()) {
119114
return;
120115
}
121-
mStreetViewPanorama.setZoomGesturesEnabled(mZoomCheckbox.isChecked());
116+
streetViewPanorama.setZoomGesturesEnabled(zoomCheckbox.isChecked());
122117
}
123118

124119
public void onPanningToggled(View view) {
125120
if (!checkReady()) {
126121
return;
127122
}
128-
mStreetViewPanorama.setPanningGesturesEnabled(mPanningCheckbox.isChecked());
123+
streetViewPanorama.setPanningGesturesEnabled(panningCheckbox.isChecked());
129124
}
130125

131126
public void onOutdoorToggled(View view) {

ApiDemos/java/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
<string name="street_view_panorama_navigation_demo_description">Street View Panorama with programmatic navigation.</string>
216216
<string name="street_view_panorama_navigation_demo_label">Street View Panorama navigation</string>
217217
<string name="street_view_panorama_options_demo_description">Street View Panorama with toggles for options.</string>
218-
<string name="street_view_panorama_options_demo_label">Street View Panorama options</string>
218+
<string name="street_view_panorama_options_demo_label">Street View Panorama Options</string>
219219
<string name="street_view_panorama_view_demo_description">Standard Street View Panorama using a View.</string>
220220
<string name="street_view_panorama_view_demo_label">Street View Panorama View</string>
221221
<string name="streetnames">Streetnames</string>

ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/DemoDetailsList.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class DemoDetailsList {
5353
R.string.split_street_view_panorama_and_map_demo_label,
5454
R.string.split_street_view_panorama_and_map_demo_details,
5555
SplitStreetViewPanoramaAndMapDemoActivity::class.java),
56+
DemoDetails(
57+
R.string.street_view_panorama_options_demo_label,
58+
R.string.street_view_panorama_options_demo_details,
59+
StreetViewPanoramaOptionsDemoActivity::class.java),
5660
DemoDetails(R.string.tags_demo_label, R.string.tags_demo_details,
5761
TagsDemoActivity::class.java),
5862
DemoDetails(R.string.ui_settings_demo_label, R.string.ui_settings_demo_details,
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.example.kotlindemos
15+
16+
import android.os.Bundle
17+
import android.view.View
18+
import android.widget.CheckBox
19+
import android.widget.Toast
20+
import androidx.appcompat.app.AppCompatActivity
21+
import com.google.android.gms.maps.StreetViewPanorama
22+
import com.google.android.gms.maps.SupportStreetViewPanoramaFragment
23+
import com.google.android.gms.maps.model.LatLng
24+
import com.google.android.gms.maps.model.StreetViewSource
25+
26+
27+
/**
28+
* This shows how to create an activity with static streetview (all options have been switched off)
29+
*/
30+
class StreetViewPanoramaOptionsDemoActivity : AppCompatActivity() {
31+
private var streetViewPanorama: StreetViewPanorama? = null
32+
private lateinit var streetNameCheckbox: CheckBox
33+
private lateinit var navigationCheckbox: CheckBox
34+
private lateinit var zoomCheckbox: CheckBox
35+
private lateinit var panningCheckbox: CheckBox
36+
private lateinit var outdoorCheckbox: CheckBox
37+
38+
override fun onCreate(savedInstanceState: Bundle?) {
39+
super.onCreate(savedInstanceState)
40+
setContentView(R.layout.street_view_panorama_options_demo)
41+
streetNameCheckbox = findViewById(R.id.streetnames)
42+
navigationCheckbox = findViewById(R.id.navigation)
43+
zoomCheckbox = findViewById(R.id.zoom)
44+
panningCheckbox = findViewById(R.id.panning)
45+
outdoorCheckbox = findViewById(R.id.outdoor)
46+
val streetViewPanoramaFragment =
47+
supportFragmentManager.findFragmentById(R.id.streetviewpanorama) as SupportStreetViewPanoramaFragment?
48+
streetViewPanoramaFragment?.getStreetViewPanoramaAsync { panorama: StreetViewPanorama ->
49+
streetViewPanorama = panorama
50+
panorama.isStreetNamesEnabled = streetNameCheckbox.isChecked()
51+
panorama.isUserNavigationEnabled = navigationCheckbox.isChecked()
52+
panorama.isZoomGesturesEnabled = zoomCheckbox.isChecked()
53+
panorama.isPanningGesturesEnabled = panningCheckbox.isChecked()
54+
55+
// Only set the panorama to SAN_FRAN on startup (when no panoramas have been
56+
// loaded which is when the savedInstanceState is null).
57+
savedInstanceState ?: setPosition()
58+
}
59+
}
60+
61+
private fun setPosition() {
62+
streetViewPanorama?.setPosition(
63+
SAN_FRAN,
64+
RADIUS,
65+
if (outdoorCheckbox.isChecked) StreetViewSource.OUTDOOR else StreetViewSource.DEFAULT
66+
)
67+
}
68+
69+
private fun checkReady(): Boolean {
70+
if (streetViewPanorama == null) {
71+
Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show()
72+
return false
73+
}
74+
return true
75+
}
76+
77+
fun onStreetNamesToggled(view: View?) {
78+
if (!checkReady()) {
79+
return
80+
}
81+
streetViewPanorama?.isStreetNamesEnabled = streetNameCheckbox.isChecked
82+
}
83+
84+
fun onNavigationToggled(view: View?) {
85+
if (!checkReady()) {
86+
return
87+
}
88+
streetViewPanorama?.isUserNavigationEnabled = navigationCheckbox.isChecked
89+
}
90+
91+
fun onZoomToggled(view: View?) {
92+
if (!checkReady()) {
93+
return
94+
}
95+
streetViewPanorama?.isZoomGesturesEnabled = zoomCheckbox.isChecked
96+
}
97+
98+
fun onPanningToggled(view: View?) {
99+
if (!checkReady()) {
100+
return
101+
}
102+
streetViewPanorama?.isPanningGesturesEnabled = panningCheckbox.isChecked
103+
}
104+
105+
fun onOutdoorToggled(view: View?) {
106+
if (!checkReady()) {
107+
return
108+
}
109+
setPosition()
110+
}
111+
112+
companion object {
113+
// Cole St, San Fran
114+
private val SAN_FRAN = LatLng(37.765927, -122.449972)
115+
private const val RADIUS = 20
116+
}
117+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
Copyright (C) 2012 The Android Open Source Project
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
--><!-- This can go anywhere in your layout. -->
16+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent">
19+
20+
<fragment
21+
android:id="@+id/streetviewpanorama"
22+
android:layout_width="match_parent"
23+
android:layout_height="match_parent"
24+
class="com.google.android.gms.maps.SupportStreetViewPanoramaFragment" />
25+
26+
<!-- A set of test checkboxes. -->
27+
28+
<LinearLayout
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_alignParentLeft="true"
32+
android:layout_alignTop="@+id/streetviewpanorama"
33+
android:padding="6dp"
34+
android:background="@color/white"
35+
android:orientation="vertical">
36+
37+
<CheckBox
38+
android:id="@+id/streetnames"
39+
android:layout_width="wrap_content"
40+
android:layout_height="wrap_content"
41+
android:onClick="onStreetNamesToggled"
42+
android:checked="true"
43+
android:text="@string/streetnames" />
44+
45+
<CheckBox
46+
android:id="@+id/navigation"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:onClick="onNavigationToggled"
50+
android:checked="true"
51+
android:text="@string/navigation" />
52+
53+
<CheckBox
54+
android:id="@+id/zoom"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:onClick="onZoomToggled"
58+
android:checked="true"
59+
android:text="@string/zoom_gestures" />
60+
61+
<CheckBox
62+
android:id="@+id/panning"
63+
android:layout_width="wrap_content"
64+
android:layout_height="wrap_content"
65+
android:onClick="onPanningToggled"
66+
android:checked="true"
67+
android:text="@string/panning" />
68+
69+
<CheckBox
70+
android:id="@+id/outdoor"
71+
android:layout_width="wrap_content"
72+
android:layout_height="wrap_content"
73+
android:onClick="onOutdoorToggled"
74+
android:checked="true"
75+
android:text="@string/outdoor_only" />
76+
</LinearLayout>
77+
78+
</RelativeLayout>

ApiDemos/kotlin/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<activity android:name=".VisibleRegionDemoActivity"/>
6060
<activity android:name=".SplitStreetViewPanoramaAndMapDemoActivity" />
6161
<activity android:name=".StreetViewPanoramaBasicDemoActivity" />
62+
<activity android:name=".StreetViewPanoramaOptionsDemoActivity" />
6263
</application>
6364

6465
</manifest>

ApiDemos/kotlin/app/src/main/res/values/strings.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@
151151
<string name="street_view_panorama_basic_demo_label">Street View Panorama</string>
152152
<string name="street_view_panorama_basic_demo_details">Standard Street View Panorama using a Fragment.</string>
153153

154+
<!-- StreetView Panorama Options Demo -->
155+
<string name="street_view_panorama_options_demo_label">Street View Panorama Options</string>
156+
<string name="street_view_panorama_options_demo_details">Street View Panorama with toggles for options.</string>
157+
<string name="navigation">Navigation</string>
158+
<string name="outdoor_only">Outdoor Only</string>
159+
<string name="zoom_gestures">Zoom Gestures</string>
160+
<string name="panning">Panning Gestures</string>
161+
<string name="streetnames">Streetnames</string>
162+
154163
<!-- Tags Demo -->
155164
<string name="tags_demo_label">Tags</string>
156165
<string name="tags_demo_details">Demonstrates how to get and set tags on API objects.</string>

0 commit comments

Comments
 (0)