22
22
23
23
package com .example .mapdemo ;
24
24
25
+ import android .graphics .Color ;
26
+ import android .os .Bundle ;
27
+ import android .util .Log ;
28
+ import android .view .View ;
29
+ import android .widget .CompoundButton ;
30
+ import android .widget .SeekBar ;
31
+ import android .widget .Toast ;
32
+
33
+ import androidx .appcompat .app .AppCompatActivity ;
34
+
25
35
import com .google .android .libraries .maps .CameraUpdate ;
26
36
import com .google .android .libraries .maps .CameraUpdateFactory ;
27
37
import com .google .android .libraries .maps .GoogleMap ;
36
46
import com .google .android .libraries .maps .model .LatLng ;
37
47
import com .google .android .libraries .maps .model .PolylineOptions ;
38
48
39
- import android .graphics .Color ;
40
- import android .os .Bundle ;
41
- import android .util .Log ;
42
- import android .view .View ;
43
- import android .widget .CompoundButton ;
44
- import android .widget .SeekBar ;
45
- import android .widget .Toast ;
46
-
47
- import androidx .appcompat .app .AppCompatActivity ;
48
-
49
49
/**
50
50
* This shows how to change the camera position for the map.
51
51
*/
52
+ // [START maps_camera_events]
52
53
public class CameraDemoActivity extends AppCompatActivity implements
53
54
OnCameraMoveStartedListener ,
54
55
OnCameraMoveListener ,
55
56
OnCameraMoveCanceledListener ,
56
57
OnCameraIdleListener ,
57
58
OnMapReadyCallback {
58
-
59
+ // [START_EXCLUDE silent]
59
60
private static final String TAG = CameraDemoActivity .class .getName ();
60
61
61
62
/**
@@ -77,60 +78,67 @@ public class CameraDemoActivity extends AppCompatActivity implements
77
78
.bearing (0 )
78
79
.tilt (25 )
79
80
.build ();
81
+ // [END_EXCLUDE]
80
82
81
- private GoogleMap mMap ;
82
-
83
- private CompoundButton mAnimateToggle ;
84
- private CompoundButton mCustomDurationToggle ;
85
- private SeekBar mCustomDurationBar ;
83
+ private GoogleMap map ;
84
+ // [START_EXCLUDE silent]
85
+ private CompoundButton animateToggle ;
86
+ private CompoundButton customDurationToggle ;
87
+ private SeekBar customDurationBar ;
86
88
private PolylineOptions currPolylineOptions ;
87
89
private boolean isCanceled = false ;
90
+ // [END_EXCLUDE]
88
91
89
92
@ Override
90
93
protected void onCreate (Bundle savedInstanceState ) {
91
94
super .onCreate (savedInstanceState );
92
95
setContentView (R .layout .camera_demo );
93
-
94
- mAnimateToggle = ( CompoundButton ) findViewById (R .id .animate );
95
- mCustomDurationToggle = ( CompoundButton ) findViewById (R .id .duration_toggle );
96
- mCustomDurationBar = ( SeekBar ) findViewById (R .id .duration_bar );
96
+ // [START_EXCLUDE silent]
97
+ animateToggle = findViewById (R .id .animate );
98
+ customDurationToggle = findViewById (R .id .duration_toggle );
99
+ customDurationBar = findViewById (R .id .duration_bar );
97
100
98
101
updateEnabledState ();
102
+ // [END_EXCLUDE]
99
103
100
104
SupportMapFragment mapFragment =
101
105
(SupportMapFragment ) getSupportFragmentManager ().findFragmentById (R .id .map );
102
106
mapFragment .getMapAsync (this );
103
107
}
104
108
109
+ // [START_EXCLUDE silent]
105
110
@ Override
106
111
protected void onResume () {
107
112
super .onResume ();
108
113
updateEnabledState ();
109
114
}
115
+ // [END_EXCLUDE]
110
116
111
117
@ Override
112
- public void onMapReady (GoogleMap map ) {
113
- mMap = map ;
114
-
115
- mMap .setOnCameraIdleListener (this );
116
- mMap .setOnCameraMoveStartedListener (this );
117
- mMap .setOnCameraMoveListener (this );
118
- mMap .setOnCameraMoveCanceledListener (this );
119
-
118
+ public void onMapReady (GoogleMap googleMap ) {
119
+ map = googleMap ;
120
+
121
+ map .setOnCameraIdleListener (this );
122
+ map .setOnCameraMoveStartedListener (this );
123
+ map .setOnCameraMoveListener (this );
124
+ map .setOnCameraMoveCanceledListener (this );
125
+ // [START_EXCLUDE silent]
120
126
// We will provide our own zoom controls.
121
- mMap .getUiSettings ().setZoomControlsEnabled (false );
122
- mMap .getUiSettings ().setMyLocationButtonEnabled (true );
127
+ map .getUiSettings ().setZoomControlsEnabled (false );
128
+ map .getUiSettings ().setMyLocationButtonEnabled (true );
129
+ // [END_EXCLUDE]
123
130
124
131
// Show Sydney
125
- mMap .moveCamera (CameraUpdateFactory .newLatLngZoom (new LatLng (-33.87365 , 151.20689 ), 10 ));
132
+ map .moveCamera (CameraUpdateFactory .newLatLngZoom (new LatLng (-33.87365 , 151.20689 ), 10 ));
126
133
}
127
134
135
+ // [START_EXCLUDE silent]
128
136
/**
129
137
* When the map is not ready the CameraUpdateFactory cannot be used. This should be called on
130
138
* all entry points that call methods on the Google Maps API.
131
139
*/
132
140
private boolean checkReady () {
133
- if (mMap == null ) {
141
+ if (map == null ) {
134
142
Toast .makeText (this , R .string .map_not_ready , Toast .LENGTH_SHORT ).show ();
135
143
return false ;
136
144
}
@@ -179,7 +187,7 @@ public void onStopAnimation(View view) {
179
187
return ;
180
188
}
181
189
182
- mMap .stopAnimation ();
190
+ map .stopAnimation ();
183
191
}
184
192
185
193
/**
@@ -212,7 +220,7 @@ public void onTiltMore(View view) {
212
220
return ;
213
221
}
214
222
215
- CameraPosition currentCameraPosition = mMap .getCameraPosition ();
223
+ CameraPosition currentCameraPosition = map .getCameraPosition ();
216
224
float currentTilt = currentCameraPosition .tilt ;
217
225
float newTilt = currentTilt + 10 ;
218
226
@@ -232,7 +240,7 @@ public void onTiltLess(View view) {
232
240
return ;
233
241
}
234
242
235
- CameraPosition currentCameraPosition = mMap .getCameraPosition ();
243
+ CameraPosition currentCameraPosition = map .getCameraPosition ();
236
244
237
245
float currentTilt = currentCameraPosition .tilt ;
238
246
@@ -307,9 +315,9 @@ public void onToggleCustomDuration(View view) {
307
315
* Update the enabled state of the custom duration controls.
308
316
*/
309
317
private void updateEnabledState () {
310
- mCustomDurationToggle .setEnabled (mAnimateToggle .isChecked ());
311
- mCustomDurationBar
312
- .setEnabled (mAnimateToggle .isChecked () && mCustomDurationToggle .isChecked ());
318
+ customDurationToggle .setEnabled (animateToggle .isChecked ());
319
+ customDurationBar
320
+ .setEnabled (animateToggle .isChecked () && customDurationToggle .isChecked ());
313
321
}
314
322
315
323
private void changeCamera (CameraUpdate update ) {
@@ -321,79 +329,101 @@ private void changeCamera(CameraUpdate update) {
321
329
* animate toggle button.
322
330
*/
323
331
private void changeCamera (CameraUpdate update , CancelableCallback callback ) {
324
- if (mAnimateToggle .isChecked ()) {
325
- if (mCustomDurationToggle .isChecked ()) {
326
- int duration = mCustomDurationBar .getProgress ();
332
+ if (animateToggle .isChecked ()) {
333
+ if (customDurationToggle .isChecked ()) {
334
+ int duration = customDurationBar .getProgress ();
327
335
// The duration must be strictly positive so we make it at least 1.
328
- mMap .animateCamera (update , Math .max (duration , 1 ), callback );
336
+ map .animateCamera (update , Math .max (duration , 1 ), callback );
329
337
} else {
330
- mMap .animateCamera (update , callback );
338
+ map .animateCamera (update , callback );
331
339
}
332
340
} else {
333
- mMap .moveCamera (update );
341
+ map .moveCamera (update );
334
342
}
335
343
}
344
+ // [END_EXCLUDE]
336
345
337
346
@ Override
338
347
public void onCameraMoveStarted (int reason ) {
348
+ // [START_EXCLUDE silent]
339
349
if (!isCanceled ) {
340
- mMap .clear ();
350
+ map .clear ();
341
351
}
352
+ // [END_EXCLUDE]
342
353
343
354
String reasonText = "UNKNOWN_REASON" ;
355
+ // [START_EXCLUDE silent]
344
356
currPolylineOptions = new PolylineOptions ().width (5 );
357
+ // [END_EXCLUDE]
345
358
switch (reason ) {
346
359
case OnCameraMoveStartedListener .REASON_GESTURE :
360
+ // [START_EXCLUDE silent]
347
361
currPolylineOptions .color (Color .BLUE );
362
+ // [END_EXCLUDE]
348
363
reasonText = "GESTURE" ;
349
364
break ;
350
365
case OnCameraMoveStartedListener .REASON_API_ANIMATION :
366
+ // [START_EXCLUDE silent]
351
367
currPolylineOptions .color (Color .RED );
368
+ // [END_EXCLUDE]
352
369
reasonText = "API_ANIMATION" ;
353
370
break ;
354
371
case OnCameraMoveStartedListener .REASON_DEVELOPER_ANIMATION :
372
+ // [START_EXCLUDE silent]
355
373
currPolylineOptions .color (Color .GREEN );
374
+ // [END_EXCLUDE]
356
375
reasonText = "DEVELOPER_ANIMATION" ;
357
376
break ;
358
377
}
359
- Log .i (TAG , "onCameraMoveStarted(" + reasonText + ")" );
378
+ Log .d (TAG , "onCameraMoveStarted(" + reasonText + ")" );
379
+ // [START_EXCLUDE silent]
360
380
addCameraTargetToPath ();
381
+ // [END_EXCLUDE]
361
382
}
362
383
363
384
@ Override
364
385
public void onCameraMove () {
386
+ // [START_EXCLUDE silent]
365
387
// When the camera is moving, add its target to the current path we'll draw on the map.
366
388
if (currPolylineOptions != null ) {
367
389
addCameraTargetToPath ();
368
390
}
369
- Log .i (TAG , "onCameraMove" );
391
+ // [END_EXCLUDE]
392
+ Log .d (TAG , "onCameraMove" );
370
393
}
371
394
372
395
@ Override
373
396
public void onCameraMoveCanceled () {
397
+ // [START_EXCLUDE silent]
374
398
// When the camera stops moving, add its target to the current path, and draw it on the map.
375
399
if (currPolylineOptions != null ) {
376
400
addCameraTargetToPath ();
377
- mMap .addPolyline (currPolylineOptions );
401
+ map .addPolyline (currPolylineOptions );
378
402
}
379
403
isCanceled = true ; // Set to clear the map when dragging starts again.
380
404
currPolylineOptions = null ;
381
- Log .i (TAG , "onCameraMoveCancelled" );
405
+ // [END_EXCLUDE]
406
+ Log .d (TAG , "onCameraMoveCancelled" );
382
407
}
383
408
384
409
@ Override
385
410
public void onCameraIdle () {
411
+ // [START_EXCLUDE silent]
386
412
if (currPolylineOptions != null ) {
387
413
addCameraTargetToPath ();
388
- mMap .addPolyline (currPolylineOptions );
414
+ map .addPolyline (currPolylineOptions );
389
415
}
390
416
currPolylineOptions = null ;
391
417
isCanceled = false ; // Set to *not* clear the map when dragging starts again.
392
- Log .i (TAG , "onCameraIdle" );
418
+ // [END_EXCLUDE]
419
+ Log .d (TAG , "onCameraIdle" );
393
420
}
394
421
422
+ // [START_EXCLUDE silent]
395
423
private void addCameraTargetToPath () {
396
- LatLng target = mMap .getCameraPosition ().target ;
424
+ LatLng target = map .getCameraPosition ().target ;
397
425
currPolylineOptions .add (target );
398
426
}
399
- }
427
+ // [END_EXCLUDE]
428
+ }
429
+ // [END maps_camera_events]
0 commit comments