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
+ }
0 commit comments