Skip to content

Commit

Permalink
Add/update samples for new camera listeners
Browse files Browse the repository at this point in the history
Change-Id: Ibef6f7f777144901f1985326791d950cd33a1138
  • Loading branch information
stephenmcd committed Jul 19, 2016
1 parent abc8d0a commit 6505cbd
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.CancelableCallback;
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveCanceledListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveListener;
import com.google.android.gms.maps.GoogleMap.OnCameraMoveStartedListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
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.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.SeekBar;
Expand All @@ -35,7 +42,14 @@
/**
* This shows how to change the camera position for the map.
*/
public class CameraDemoActivity extends AppCompatActivity implements OnMapReadyCallback {
public class CameraDemoActivity extends AppCompatActivity implements
OnCameraMoveStartedListener,
OnCameraMoveListener,
OnCameraMoveCanceledListener,
OnCameraIdleListener,
OnMapReadyCallback {

private static final String TAG = CameraDemoActivity.class.getName();

/**
* The amount by which to scroll the camera. Note that this amount is in raw pixels, not dp
Expand All @@ -60,10 +74,10 @@ public class CameraDemoActivity extends AppCompatActivity implements OnMapReadyC
private GoogleMap mMap;

private CompoundButton mAnimateToggle;

private CompoundButton mCustomDurationToggle;

private SeekBar mCustomDurationBar;
private PolylineOptions currPolylineOptions;
private boolean isCanceled = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -91,8 +105,14 @@ protected void onResume() {
public void onMapReady(GoogleMap map) {
mMap = map;

mMap.setOnCameraIdleListener(this);
mMap.setOnCameraMoveStartedListener(this);
mMap.setOnCameraMoveListener(this);
mMap.setOnCameraMoveCanceledListener(this);

// We will provide our own zoom controls.
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(true);

// Show Sydney
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.87365, 151.20689), 10));
Expand Down Expand Up @@ -306,4 +326,67 @@ private void changeCamera(CameraUpdate update, CancelableCallback callback) {
mMap.moveCamera(update);
}
}
}

@Override
public void onCameraMoveStarted(int reason) {
if (!isCanceled) {
mMap.clear();
}

String reasonText = "UNKNOWN_REASON";
currPolylineOptions = new PolylineOptions().width(5);
switch (reason) {
case OnCameraMoveStartedListener.REASON_GESTURE:
currPolylineOptions.color(Color.BLUE);
reasonText = "GESTURE";
break;
case OnCameraMoveStartedListener.REASON_API_ANIMATION:
currPolylineOptions.color(Color.RED);
reasonText = "API_ANIMATION";
break;
case OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION:
currPolylineOptions.color(Color.GREEN);
reasonText = "DEVELOPER_ANIMATION";
break;
}
Log.i(TAG, "onCameraMoveStarted(" + reasonText + ")");
addCameraTargetToPath();
}

@Override
public void onCameraMove() {
// 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");
}

@Override
public void onCameraMoveCanceled() {
// 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);
}
isCanceled = true; // Set to clear the map when dragging starts again.
currPolylineOptions = null;
Log.i(TAG, "onCameraMoveCancelled");
}

@Override
public void onCameraIdle() {
if (currPolylineOptions != null) {
addCameraTargetToPath();
mMap.addPolyline(currPolylineOptions);
}
currPolylineOptions = null;
isCanceled = false; // Set to *not* clear the map when dragging starts again.
Log.i(TAG, "onCameraIdle");
}

private void addCameraTargetToPath() {
LatLng target = mMap.getCameraPosition().target;
currPolylineOptions.add(target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ private DemoDetailsList() {
new DemoDetails(R.string.camera_demo_label,
R.string.camera_demo_description,
CameraDemoActivity.class),
new DemoDetails(R.string.camera_clamping_demo_label,
R.string.camera_clamping_demo_description,
CameraClampingDemoActivity.class),
new DemoDetails(R.string.circle_demo_label,
R.string.circle_demo_description,
CircleDemoActivity.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
package com.example.mapdemo;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

import android.os.Bundle;
Expand All @@ -33,12 +32,12 @@
* This shows how to listen to some {@link GoogleMap} events.
*/
public class EventsDemoActivity extends AppCompatActivity
implements OnMapClickListener, OnMapLongClickListener, OnCameraChangeListener,
implements OnMapClickListener, OnMapLongClickListener, OnCameraIdleListener,
OnMapReadyCallback {

private TextView mTapTextView;

private TextView mCameraTextView;
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -55,9 +54,10 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void onMapReady(GoogleMap map) {
map.setOnMapClickListener(this);
map.setOnMapLongClickListener(this);
map.setOnCameraChangeListener(this);
mMap = map;
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnCameraIdleListener(this);
}

@Override
Expand All @@ -71,7 +71,7 @@ public void onMapLongClick(LatLng point) {
}

@Override
public void onCameraChange(final CameraPosition position) {
mCameraTextView.setText(position.toString());
public void onCameraIdle() {
mCameraTextView.setText(mMap.getCameraPosition().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
Expand Down Expand Up @@ -85,11 +84,11 @@ public void onMapReady(GoogleMap map) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SFO, 18));
// Add a marker to the Opera House.
mMap.addMarker(new MarkerOptions().position(SOH).title("Sydney Opera House"));
// Add a camera change listener.
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
// Add a camera idle listener.
mMap.setOnCameraIdleListener(new OnCameraIdleListener() {
@Override
public void onCameraChange(CameraPosition pos) {
mMessageView.setText("CameraChangeListener: " + pos);
public void onCameraIdle() {
mMessageView.setText("CameraChangeListener: " + mMap.getCameraPosition());
}
});
}
Expand Down Expand Up @@ -138,8 +137,7 @@ public void setMorePadding(View view) {
if (!checkReady()) {
return;
}
View mapView = ((SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map)).getView();
View mapView = (getSupportFragmentManager().findFragmentById(R.id.map)).getView();
int left = 150;
int top = 0;
int right = mapView.getWidth() / 3;
Expand Down

0 comments on commit 6505cbd

Please sign in to comment.