chore: Add region tags for CameraDemoActivity (#233)

Also made following changes to Java version:
* Remove Hungarian notation from variables
* Remove redundant view casts
This commit is contained in:
Sean Barbeau 2020-06-02 11:21:22 -04:00 committed by GitHub
parent 09dcb13876
commit a72d0bde1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 67 deletions

View File

@ -15,6 +15,16 @@
package com.example.mapdemo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
@ -29,26 +39,17 @@ import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
/**
* This shows how to change the camera position for the map.
*/
// [START maps_camera_events]
public class CameraDemoActivity extends AppCompatActivity implements
OnCameraMoveStartedListener,
OnCameraMoveListener,
OnCameraMoveCanceledListener,
OnCameraIdleListener,
OnMapReadyCallback {
// [START_EXCLUDE silent]
private static final String TAG = CameraDemoActivity.class.getName();
/**
@ -70,60 +71,67 @@ public class CameraDemoActivity extends AppCompatActivity implements
.bearing(0)
.tilt(25)
.build();
// [END_EXCLUDE]
private GoogleMap mMap;
private CompoundButton mAnimateToggle;
private CompoundButton mCustomDurationToggle;
private SeekBar mCustomDurationBar;
private GoogleMap map;
// [START_EXCLUDE silent]
private CompoundButton animateToggle;
private CompoundButton customDurationToggle;
private SeekBar customDurationBar;
private PolylineOptions currPolylineOptions;
private boolean isCanceled = false;
// [END_EXCLUDE]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_demo);
mAnimateToggle = (CompoundButton) findViewById(R.id.animate);
mCustomDurationToggle = (CompoundButton) findViewById(R.id.duration_toggle);
mCustomDurationBar = (SeekBar) findViewById(R.id.duration_bar);
// [START_EXCLUDE silent]
animateToggle = findViewById(R.id.animate);
customDurationToggle = findViewById(R.id.duration_toggle);
customDurationBar = findViewById(R.id.duration_bar);
updateEnabledState();
// [END_EXCLUDE]
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
// [START_EXCLUDE silent]
@Override
protected void onResume() {
super.onResume();
updateEnabledState();
}
// [END_EXCLUDE]
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.setOnCameraIdleListener(this);
mMap.setOnCameraMoveStartedListener(this);
mMap.setOnCameraMoveListener(this);
mMap.setOnCameraMoveCanceledListener(this);
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setOnCameraIdleListener(this);
map.setOnCameraMoveStartedListener(this);
map.setOnCameraMoveListener(this);
map.setOnCameraMoveCanceledListener(this);
// [START_EXCLUDE silent]
// We will provide our own zoom controls.
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(true);
// [END_EXCLUDE]
// Show Sydney
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
}
// [START_EXCLUDE silent]
/**
* When the map is not ready the CameraUpdateFactory cannot be used. This should be called on
* all entry points that call methods on the Google Maps API.
*/
private boolean checkReady() {
if (mMap == null) {
if (map == null) {
Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show();
return false;
}
@ -172,7 +180,7 @@ public class CameraDemoActivity extends AppCompatActivity implements
return;
}
mMap.stopAnimation();
map.stopAnimation();
}
/**
@ -205,7 +213,7 @@ public class CameraDemoActivity extends AppCompatActivity implements
return;
}
CameraPosition currentCameraPosition = mMap.getCameraPosition();
CameraPosition currentCameraPosition = map.getCameraPosition();
float currentTilt = currentCameraPosition.tilt;
float newTilt = currentTilt + 10;
@ -225,7 +233,7 @@ public class CameraDemoActivity extends AppCompatActivity implements
return;
}
CameraPosition currentCameraPosition = mMap.getCameraPosition();
CameraPosition currentCameraPosition = map.getCameraPosition();
float currentTilt = currentCameraPosition.tilt;
@ -300,9 +308,9 @@ public class CameraDemoActivity extends AppCompatActivity implements
* Update the enabled state of the custom duration controls.
*/
private void updateEnabledState() {
mCustomDurationToggle.setEnabled(mAnimateToggle.isChecked());
mCustomDurationBar
.setEnabled(mAnimateToggle.isChecked() && mCustomDurationToggle.isChecked());
customDurationToggle.setEnabled(animateToggle.isChecked());
customDurationBar
.setEnabled(animateToggle.isChecked() && customDurationToggle.isChecked());
}
private void changeCamera(CameraUpdate update) {
@ -314,79 +322,101 @@ public class CameraDemoActivity extends AppCompatActivity implements
* animate toggle button.
*/
private void changeCamera(CameraUpdate update, CancelableCallback callback) {
if (mAnimateToggle.isChecked()) {
if (mCustomDurationToggle.isChecked()) {
int duration = mCustomDurationBar.getProgress();
if (animateToggle.isChecked()) {
if (customDurationToggle.isChecked()) {
int duration = customDurationBar.getProgress();
// The duration must be strictly positive so we make it at least 1.
mMap.animateCamera(update, Math.max(duration, 1), callback);
map.animateCamera(update, Math.max(duration, 1), callback);
} else {
mMap.animateCamera(update, callback);
map.animateCamera(update, callback);
}
} else {
mMap.moveCamera(update);
map.moveCamera(update);
}
}
// [END_EXCLUDE]
@Override
public void onCameraMoveStarted(int reason) {
// [START_EXCLUDE silent]
if (!isCanceled) {
mMap.clear();
map.clear();
}
// [END_EXCLUDE]
String reasonText = "UNKNOWN_REASON";
// [START_EXCLUDE silent]
currPolylineOptions = new PolylineOptions().width(5);
// [END_EXCLUDE]
switch (reason) {
case OnCameraMoveStartedListener.REASON_GESTURE:
// [START_EXCLUDE silent]
currPolylineOptions.color(Color.BLUE);
// [END_EXCLUDE]
reasonText = "GESTURE";
break;
case OnCameraMoveStartedListener.REASON_API_ANIMATION:
// [START_EXCLUDE silent]
currPolylineOptions.color(Color.RED);
// [END_EXCLUDE]
reasonText = "API_ANIMATION";
break;
case OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION:
// [START_EXCLUDE silent]
currPolylineOptions.color(Color.GREEN);
// [END_EXCLUDE]
reasonText = "DEVELOPER_ANIMATION";
break;
}
Log.i(TAG, "onCameraMoveStarted(" + reasonText + ")");
Log.d(TAG, "onCameraMoveStarted(" + reasonText + ")");
// [START_EXCLUDE silent]
addCameraTargetToPath();
// [END_EXCLUDE]
}
@Override
public void onCameraMove() {
// [START_EXCLUDE silent]
// When the camera is moving, add its target to the current path we'll draw on the map.
if (currPolylineOptions != null) {
addCameraTargetToPath();
}
Log.i(TAG, "onCameraMove");
// [END_EXCLUDE]
Log.d(TAG, "onCameraMove");
}
@Override
public void onCameraMoveCanceled() {
// [START_EXCLUDE silent]
// When the camera stops moving, add its target to the current path, and draw it on the map.
if (currPolylineOptions != null) {
addCameraTargetToPath();
mMap.addPolyline(currPolylineOptions);
map.addPolyline(currPolylineOptions);
}
isCanceled = true; // Set to clear the map when dragging starts again.
currPolylineOptions = null;
Log.i(TAG, "onCameraMoveCancelled");
// [END_EXCLUDE]
Log.d(TAG, "onCameraMoveCancelled");
}
@Override
public void onCameraIdle() {
// [START_EXCLUDE silent]
if (currPolylineOptions != null) {
addCameraTargetToPath();
mMap.addPolyline(currPolylineOptions);
map.addPolyline(currPolylineOptions);
}
currPolylineOptions = null;
isCanceled = false; // Set to *not* clear the map when dragging starts again.
Log.i(TAG, "onCameraIdle");
// [END_EXCLUDE]
Log.d(TAG, "onCameraIdle");
}
// [START_EXCLUDE silent]
private void addCameraTargetToPath() {
LatLng target = mMap.getCameraPosition().target;
LatLng target = map.getCameraPosition().target;
currPolylineOptions.add(target);
}
}
// [END_EXCLUDE]
}
// [END maps_camera_events]

View File

@ -41,6 +41,7 @@ import com.google.android.gms.maps.model.PolylineOptions
/**
* This shows how to change the camera position for the map.
*/
// [START maps_camera_events]
class CameraDemoActivity :
AppCompatActivity(),
OnCameraMoveStartedListener,
@ -48,7 +49,7 @@ class CameraDemoActivity :
OnCameraMoveCanceledListener,
OnCameraIdleListener,
OnMapReadyCallback {
// [START_EXCLUDE silent]
/**
* The amount by which to scroll the camera. Note that this amount is in raw pixels, not dp
* (density-independent pixels).
@ -69,38 +70,40 @@ class CameraDemoActivity :
.bearing(0f)
.tilt(25f)
.build()
// [END_EXCLUDE]
private lateinit var map: GoogleMap
// [START_EXCLUDE silent]
private lateinit var animateToggle: CompoundButton
private lateinit var customDurationToggle: CompoundButton
private lateinit var customDurationBar: SeekBar
private var currPolylineOptions: PolylineOptions? = null
private var isCanceled = false
// [END_EXCLUDE]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.camera_demo)
// [START_EXCLUDE silent]
animateToggle = findViewById(R.id.animate)
customDurationToggle = findViewById(R.id.duration_toggle)
customDurationBar = findViewById(R.id.duration_bar)
updateEnabledState()
// [END_EXCLUDE]
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
// [START_EXCLUDE silent]
override fun onResume() {
super.onResume()
updateEnabledState()
}
// [END_EXCLUDE]
override fun onMapReady(googleMap: GoogleMap?) {
// return early if the map was not initialised properly
map = googleMap ?: return
@ -109,16 +112,18 @@ class CameraDemoActivity :
setOnCameraMoveStartedListener(this@CameraDemoActivity)
setOnCameraMoveListener(this@CameraDemoActivity)
setOnCameraMoveCanceledListener(this@CameraDemoActivity)
// [START_EXCLUDE silent]
// We will provide our own zoom controls.
uiSettings.isZoomControlsEnabled = false
uiSettings.isMyLocationButtonEnabled = true
// [END_EXCLUDE]
// Show Sydney
moveCamera(CameraUpdateFactory.newLatLngZoom(sydneyLatLng, 10f))
}
}
// [START_EXCLUDE silent]
/**
* When the map is not ready the CameraUpdateFactory cannot be used. This should be used to wrap
* all entry points that call methods on the Google Maps API.
@ -286,31 +291,44 @@ class CameraDemoActivity :
map.moveCamera(update)
}
}
// [END_EXCLUDE]
override fun onCameraMoveStarted(reason: Int) {
// [START_EXCLUDE silent]
if (!isCanceled) map.clear()
// [END_EXCLUDE]
var reasonText = "UNKNOWN_REASON"
// [START_EXCLUDE silent]
currPolylineOptions = PolylineOptions().width(5f)
// [END_EXCLUDE]
when (reason) {
OnCameraMoveStartedListener.REASON_GESTURE -> {
// [START_EXCLUDE silent]
currPolylineOptions?.color(Color.BLUE)
// [END_EXCLUDE]
reasonText = "GESTURE"
}
OnCameraMoveStartedListener.REASON_API_ANIMATION -> {
// [START_EXCLUDE silent]
currPolylineOptions?.color(Color.RED)
// [END_EXCLUDE]
reasonText = "API_ANIMATION"
}
OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION -> {
// [START_EXCLUDE silent]
currPolylineOptions?.color(Color.GREEN)
// [END_EXCLUDE]
reasonText = "DEVELOPER_ANIMATION"
}
}
Log.i(TAG, "onCameraMoveStarted($reasonText)")
Log.d(TAG, "onCameraMoveStarted($reasonText)")
// [START_EXCLUDE silent]
addCameraTargetToPath()
// [END_EXCLUDE]
}
// [START_EXCLUDE silent]
/**
* Ensures that currPolyLine options is not null before accessing it
*
@ -319,15 +337,18 @@ class CameraDemoActivity :
private fun checkPolylineThen(stuffToDo: () -> Unit) {
if (currPolylineOptions != null) stuffToDo()
}
// [END_EXCLUDE]
override fun onCameraMove() {
Log.i(TAG, "onCameraMove")
Log.d(TAG, "onCameraMove")
// [START_EXCLUDE silent]
// When the camera is moving, add its target to the current path we'll draw on the map.
checkPolylineThen { addCameraTargetToPath() }
// [END_EXCLUDE]
}
override fun onCameraMoveCanceled() {
// [START_EXCLUDE silent]
// When the camera stops moving, add its target to the current path, and draw it on the map.
checkPolylineThen {
addCameraTargetToPath()
@ -336,10 +357,12 @@ class CameraDemoActivity :
isCanceled = true // Set to clear the map when dragging starts again.
currPolylineOptions = null
Log.i(TAG, "onCameraMoveCancelled")
// [END_EXCLUDE]
Log.d(TAG, "onCameraMoveCancelled")
}
override fun onCameraIdle() {
// [START_EXCLUDE silent]
checkPolylineThen {
addCameraTargetToPath()
map.addPolyline(currPolylineOptions)
@ -347,10 +370,13 @@ class CameraDemoActivity :
currPolylineOptions = null
isCanceled = false // Set to *not* clear the map when dragging starts again.
Log.i(TAG, "onCameraIdle")
// [END_EXCLUDE]
Log.d(TAG, "onCameraIdle")
}
// [START_EXCLUDE silent]
private fun addCameraTargetToPath() {
currPolylineOptions?.add(map.cameraPosition.target)
}
}
// [END_EXCLUDE]
}
// [END maps_camera_events]