Skip to content

Commit d4b2a1e

Browse files
committed
Add camera clamping demo
Change-Id: I8dfc0dbaad250f67d05c0c173ff79c525f9338e4
1 parent 7b9c03e commit d4b2a1e

File tree

4 files changed

+298
-2
lines changed

4 files changed

+298
-2
lines changed

ApiDemos/app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<!--
2121
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
2222
Google Maps Android API v2, but you must specify either coarse or fine
23-
location permissions for the 'MyLocation' functionality.
23+
location permissions for the 'MyLocation' functionality.
2424
-->
2525
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2626

@@ -45,7 +45,7 @@
4545
Note that the API key is linked to the encryption key used to sign the APK.
4646
You need a different API key for each encryption key, including the release key that is used to
4747
sign the APK for publishing.
48-
You can define the keys for the debug and release targets in src/debug/ and src/release/.
48+
You can define the keys for the debug and release targets in src/debug/ and src/release/.
4949
-->
5050
<meta-data
5151
android:name="com.google.android.geo.API_KEY"
@@ -66,6 +66,9 @@
6666
<activity
6767
android:name=".CameraDemoActivity"
6868
android:label="@string/camera_demo_label"/>
69+
<activity
70+
android:name=".CameraClampingDemoActivity"
71+
android:label="@string/camera_clamping_demo_label"/>
6972
<activity
7073
android:name=".CircleDemoActivity"
7174
android:label="@string/circle_demo_label"/>
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright (C) 2016 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+
*/
16+
17+
package com.example.mapdemo;
18+
19+
import com.google.android.gms.maps.CameraUpdateFactory;
20+
import com.google.android.gms.maps.GoogleMap;
21+
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener;
22+
import com.google.android.gms.maps.OnMapReadyCallback;
23+
import com.google.android.gms.maps.SupportMapFragment;
24+
import com.google.android.gms.maps.model.CameraPosition;
25+
import com.google.android.gms.maps.model.LatLng;
26+
import com.google.android.gms.maps.model.LatLngBounds;
27+
28+
import android.os.Bundle;
29+
import android.support.v7.app.AppCompatActivity;
30+
import android.view.View;
31+
import android.widget.TextView;
32+
import android.widget.Toast;
33+
34+
/**
35+
* This shows how to constrain the camera to specific boundaries and zoom levels.
36+
*/
37+
public class CameraClampingDemoActivity extends AppCompatActivity
38+
implements OnMapReadyCallback, OnCameraIdleListener {
39+
40+
private static final String TAG = CameraClampingDemoActivity.class.getSimpleName();
41+
42+
private static final float ZOOM_DELTA = 2.0f;
43+
private static final float DEFAULT_MIN_ZOOM = 2.0f;
44+
private static final float DEFAULT_MAX_ZOOM = 22.0f;
45+
46+
private static final LatLngBounds ADELAIDE = new LatLngBounds(
47+
new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
48+
private static final CameraPosition ADELAIDE_CAMERA = new CameraPosition.Builder()
49+
.target(new LatLng(-34.92873, 138.59995)).zoom(20.0f).bearing(0).tilt(0).build();
50+
51+
private static final LatLngBounds PACIFIC = new LatLngBounds(
52+
new LatLng(-15.0, 165.0), new LatLng(15.0, -165.0));
53+
private static final CameraPosition PACIFIC_CAMERA = new CameraPosition.Builder()
54+
.target(new LatLng(0, -180)).zoom(4.0f).bearing(0).tilt(0).build();
55+
56+
private GoogleMap mMap;
57+
58+
/**
59+
* Internal min zoom level that can be toggled via the demo.
60+
*/
61+
private float mMinZoom;
62+
63+
/**
64+
* Internal max zoom level that can be toggled via the demo.
65+
*/
66+
private float mMaxZoom;
67+
68+
private TextView mCameraTextView;
69+
70+
@Override
71+
protected void onCreate(Bundle savedInstanceState) {
72+
super.onCreate(savedInstanceState);
73+
74+
setContentView(R.layout.camera_clamping_demo);
75+
76+
mMap = null;
77+
resetMinMaxZoom();
78+
79+
mCameraTextView = (TextView) findViewById(R.id.camera_text);
80+
81+
SupportMapFragment mapFragment =
82+
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
83+
mapFragment.getMapAsync(this);
84+
}
85+
86+
@Override
87+
protected void onResume() {
88+
super.onResume();
89+
}
90+
91+
@Override
92+
public void onMapReady(GoogleMap map) {
93+
mMap = map;
94+
map.setOnCameraIdleListener(this);
95+
}
96+
97+
@Override
98+
public void onCameraIdle() {
99+
mCameraTextView.setText(mMap.getCameraPosition().toString());
100+
}
101+
102+
/**
103+
* Before the map is ready many calls will fail.
104+
* This should be called on all entry points that call methods on the Google Maps API.
105+
*/
106+
private boolean checkReady() {
107+
if (mMap == null) {
108+
Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show();
109+
return false;
110+
}
111+
return true;
112+
}
113+
114+
private void toast(String msg) {
115+
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
116+
}
117+
118+
private void resetMinMaxZoom() {
119+
mMinZoom = DEFAULT_MIN_ZOOM;
120+
mMaxZoom = DEFAULT_MAX_ZOOM;
121+
}
122+
123+
/**
124+
* Click handler for clamping to Adelaide button.
125+
* @param view
126+
*/
127+
public void onClampToAdelaide(View view) {
128+
if (!checkReady()) {
129+
return;
130+
}
131+
mMap.setLatLngBoundsForCameraTarget(ADELAIDE);
132+
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(ADELAIDE_CAMERA));
133+
}
134+
135+
/**
136+
* Click handler for clamping to Pacific button.
137+
* @param view
138+
*/
139+
public void onClampToPacific(View view) {
140+
if (!checkReady()) {
141+
return;
142+
}
143+
mMap.setLatLngBoundsForCameraTarget(PACIFIC);
144+
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(PACIFIC_CAMERA));
145+
}
146+
147+
public void onLatLngClampReset(View view) {
148+
if (!checkReady()) {
149+
return;
150+
}
151+
// Setting bounds to null removes any previously set bounds.
152+
mMap.setLatLngBoundsForCameraTarget(null);
153+
toast("LatLngBounds clamp reset.");
154+
}
155+
156+
public void onSetMinZoomClamp(View view) {
157+
if (!checkReady()) {
158+
return;
159+
}
160+
mMinZoom += ZOOM_DELTA;
161+
// Constrains the minimum zoom level.
162+
mMap.setMinZoomPreference(mMinZoom);
163+
toast("Min zoom preference set to: " + mMinZoom);
164+
}
165+
166+
public void onSetMaxZoomClamp(View view) {
167+
if (!checkReady()) {
168+
return;
169+
}
170+
mMaxZoom -= ZOOM_DELTA;
171+
// Constrains the maximum zoom level.
172+
mMap.setMaxZoomPreference(mMaxZoom);
173+
toast("Max zoom preference set to: " + mMaxZoom);
174+
}
175+
176+
public void onMinMaxZoomClampReset(View view) {
177+
if (!checkReady()) {
178+
return;
179+
}
180+
resetMinMaxZoom();
181+
mMap.resetMinMaxZoomPreference();
182+
toast("Min/Max zoom preferences reset.");
183+
}
184+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2016 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:map="http://schemas.android.com/apk/res-auto"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:orientation="vertical">
22+
<LinearLayout
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:orientation="vertical">
26+
<LinearLayout
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:orientation="horizontal">
30+
<Button
31+
android:id="@+id/clamp_min_zoom"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:onClick="onSetMinZoomClamp"
35+
android:text="@string/clamp_min_zoom"/>
36+
<Button
37+
android:id="@+id/clamp_max_zoom"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:onClick="onSetMaxZoomClamp"
41+
android:text="@string/clamp_max_zoom"/>
42+
<Button
43+
android:id="@+id/clamp_zoom_reset"
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"
46+
android:onClick="onMinMaxZoomClampReset"
47+
android:text="@string/clamp_zoom_reset"/>
48+
</LinearLayout>
49+
<LinearLayout
50+
android:layout_width="wrap_content"
51+
android:layout_height="wrap_content"
52+
android:orientation="horizontal">
53+
<Button
54+
android:id="@+id/clamp_latlng_adelaide"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:onClick="onClampToAdelaide"
58+
android:layout_weight="0.5"
59+
android:text="@string/clamp_latlng_adelaide"/>
60+
<Button
61+
android:id="@+id/clamp_latlng_pacific"
62+
android:layout_width="wrap_content"
63+
android:layout_height="wrap_content"
64+
android:onClick="onClampToPacific"
65+
android:layout_weight="0.5"
66+
android:text="@string/clamp_latlng_pacific"/>
67+
<Button
68+
android:id="@+id/clamp_latlng_reset"
69+
android:layout_width="wrap_content"
70+
android:layout_height="wrap_content"
71+
android:onClick="onLatLngClampReset"
72+
android:text="@string/clamp_latlng_reset"/>
73+
</LinearLayout>
74+
<LinearLayout
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content"
77+
android:orientation="horizontal">
78+
<TextView
79+
android:id="@+id/camera_text"
80+
android:text="@string/move_the_camera"
81+
android:layout_width="match_parent"
82+
android:layout_height="wrap_content"/>
83+
</LinearLayout>
84+
</LinearLayout>
85+
<fragment
86+
android:id="@+id/map"
87+
android:layout_width="fill_parent"
88+
android:layout_height="fill_parent"
89+
class="com.google.android.gms.maps.SupportMapFragment"
90+
map:cameraMinZoomPreference="10.0"
91+
map:cameraMaxZoomPreference="14.0"
92+
map:latLngBoundsSouthWestLatitude="37.4"
93+
map:latLngBoundsSouthWestLongitude="-122.1"
94+
map:latLngBoundsNorthEastLatitude="37.45"
95+
map:latLngBoundsNorthEastLongitude="-122.05"
96+
map:cameraTargetLat="37.421976"
97+
map:cameraTargetLng="-122.084065"
98+
map:cameraZoom="12"/>
99+
</LinearLayout>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
<string name="camera_demo_description">Demonstrates camera functions.</string>
2929
<string name="circle_demo_label">Circles</string>
3030
<string name="circle_demo_description">Demonstrates how to add Circles to a map.</string>
31+
32+
<string name="camera_clamping_demo_label">Camera Clamping</string>
33+
<string name="camera_clamping_demo_description">Demonstrates how to constrain the camera to specific boundaries and zoom levels.</string>
34+
<string name="clamp_latlng_adelaide">Clamp to Adelaide</string>
35+
<string name="clamp_latlng_pacific">Clamp to Pacific</string>
36+
<string name="clamp_latlng_reset">Reset LatLng Bounds</string>
37+
<string name="clamp_min_zoom">MinZoom++</string>
38+
<string name="clamp_max_zoom">MaxZoom--</string>
39+
<string name="clamp_zoom_reset">Reset Zoom Bounds</string>
40+
3141
<string name="clear_map">Clear</string>
3242
<string name="clickable">Clickable</string>
3343
<string name="compass">Compass</string>

0 commit comments

Comments
 (0)