summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java
diff options
context:
space:
mode:
authorTobrun <tobrun.van.nuland@gmail.com>2016-11-23 10:53:39 +0100
committerGitHub <noreply@github.com>2016-11-23 10:53:39 +0100
commitedb487b74d800d46f04b27dd7610a028720de79a (patch)
tree9c505790a26cc01ba3a8bc4c3565b824bcf8979e /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java
parent4577c56745d90cfa00837d00e5e5605260a54879 (diff)
downloadqtlocation-mapboxgl-edb487b74d800d46f04b27dd7610a028720de79a.tar.gz
[android] - move camera logic to dedicated transform class (#6919)
* [android] - move camera logic to dedicated transform class post camera updates to the message queue, this makes calling an new camera update in the on finish of another update possible. Simplify transform.java implementation. * fail the step if instrumentation tests fail, fixup typo
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java267
1 files changed, 139 insertions, 128 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java
index ab6476f6e6..8e42e794be 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java
@@ -5,7 +5,6 @@ import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.RectF;
import android.location.Location;
-import android.os.SystemClock;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -45,7 +44,6 @@ import com.mapbox.services.commons.geojson.Feature;
import java.lang.reflect.ParameterizedType;
import java.util.List;
-import java.util.concurrent.TimeUnit;
/**
* The general class to interact with in the Android Mapbox SDK. It exposes the entry point for all
@@ -64,9 +62,7 @@ public class MapboxMap {
private TrackingSettings trackingSettings;
private MyLocationViewSettings myLocationViewSettings;
private Projection projection;
- private CameraPosition cameraPosition;
- private boolean invalidCameraPosition;
-
+ private Transform transform;
private boolean myLocationEnabled;
private MapboxMap.OnMapClickListener onMapClickListener;
@@ -78,7 +74,6 @@ public class MapboxMap {
private MapboxMap.OnMyLocationTrackingModeChangeListener onMyLocationTrackingModeChangeListener;
private MapboxMap.OnMyBearingTrackingModeChangeListener onMyBearingTrackingModeChangeListener;
private MapboxMap.OnFpsChangedListener onFpsChangedListener;
- private MapboxMap.OnCameraChangeListener onCameraChangeListener;
private AnnotationManager annotationManager;
private InfoWindowManager infoWindowManager;
@@ -88,12 +83,18 @@ public class MapboxMap {
MapboxMap(@NonNull MapView mapView, IconManager iconManager) {
this.mapView = mapView;
- this.mapView.addOnMapChangedListener(new MapChangeCameraPositionListener());
uiSettings = new UiSettings(mapView);
trackingSettings = new TrackingSettings(this.mapView, uiSettings);
projection = new Projection(mapView);
infoWindowManager = new InfoWindowManager();
annotationManager = new AnnotationManager(mapView.getNativeMapView(), iconManager, infoWindowManager);
+
+ // TODO inject NativeMapView https://github.com/mapbox/mapbox-gl-native/issues/4100
+ NativeMapView nativeMapView = mapView.getNativeMapView();
+ if (nativeMapView != null) {
+ transform = new Transform(nativeMapView, this);
+ nativeMapView.addOnMapChangedListener(new CameraInvalidator());
+ }
}
// Style
@@ -379,6 +380,16 @@ public class MapboxMap {
//
/**
+ * Cancels ongoing animations.
+ * <p>
+ * This invokes the {@link CancelableCallback} for ongoing camera updates.
+ * </p>
+ */
+ public void cancelTransitions() {
+ transform.cancelTransitions();
+ }
+
+ /**
* Gets the current position of the camera.
* The CameraPosition returned is a snapshot of the current position, and will not automatically update when the
* camera moves.
@@ -386,10 +397,7 @@ public class MapboxMap {
* @return The current position of the Camera.
*/
public final CameraPosition getCameraPosition() {
- if (invalidCameraPosition) {
- invalidateCameraPosition();
- }
- return cameraPosition;
+ return transform.getCameraPosition();
}
/**
@@ -400,7 +408,7 @@ public class MapboxMap {
* @param cameraPosition the camera position to set
*/
public void setCameraPosition(@NonNull CameraPosition cameraPosition) {
- moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
+ moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), null);
}
/**
@@ -424,17 +432,13 @@ public class MapboxMap {
* @param callback the callback to be invoked when an animation finishes or is canceled
*/
@UiThread
- public final void moveCamera(CameraUpdate update, MapboxMap.CancelableCallback callback) {
- cameraPosition = update.getCameraPosition(this);
- mapView.resetTrackingModesIfRequired(cameraPosition);
- mapView.jumpTo(cameraPosition.bearing, cameraPosition.target, cameraPosition.tilt, cameraPosition.zoom);
- if (callback != null) {
- callback.onFinish();
- }
-
- if (onCameraChangeListener != null) {
- onCameraChangeListener.onCameraChange(this.cameraPosition);
- }
+ public final void moveCamera(final CameraUpdate update, final MapboxMap.CancelableCallback callback) {
+ mapView.post(new Runnable() {
+ @Override
+ public void run() {
+ transform.moveCamera(update, callback);
+ }
+ });
}
/**
@@ -489,11 +493,44 @@ public class MapboxMap {
easeCamera(update, durationMs, true, callback);
}
+ /**
+ * Gradually move the camera by a specified duration in milliseconds, zoom will not be affected
+ * unless specified within {@link CameraUpdate}. A callback can be used to be notified when
+ * easing the camera stops. If {@link #getCameraPosition()} is called during the animation, it
+ * will return the current location of the camera in flight.
+ * <p>
+ * Note that this will cancel location tracking mode if enabled.
+ * </p>
+ *
+ * @param update The change that should be applied to the camera.
+ * @param durationMs The duration of the animation in milliseconds. This must be strictly
+ * positive, otherwise an IllegalArgumentException will be thrown.
+ * @param easingInterpolator True for easing interpolator, false for linear.
+ */
@UiThread
public final void easeCamera(CameraUpdate update, int durationMs, boolean easingInterpolator) {
easeCamera(update, durationMs, easingInterpolator, null);
}
+ /**
+ * Gradually move the camera by a specified duration in milliseconds, zoom will not be affected
+ * unless specified within {@link CameraUpdate}. A callback can be used to be notified when
+ * easing the camera stops. If {@link #getCameraPosition()} is called during the animation, it
+ * will return the current location of the camera in flight.
+ * <p>
+ * Note that this will cancel location tracking mode if enabled.
+ * </p>
+ *
+ * @param update The change that should be applied to the camera.
+ * @param durationMs The duration of the animation in milliseconds. This must be strictly
+ * positive, otherwise an IllegalArgumentException will be thrown.
+ * @param easingInterpolator True for easing interpolator, false for linear.
+ * @param callback An optional callback to be notified from the main thread when the animation
+ * stops. If the animation stops due to its natural completion, the callback
+ * will be notified with onFinish(). If the animation stops due to interruption
+ * by a later camera movement or a user gesture, onCancel() will be called.
+ * Do not update or ease the camera from within onCancel().
+ */
@UiThread
public final void easeCamera(
CameraUpdate update, int durationMs, boolean easingInterpolator, final MapboxMap.CancelableCallback callback) {
@@ -501,33 +538,34 @@ public class MapboxMap {
easeCamera(update, durationMs, easingInterpolator, true, callback);
}
- @UiThread
- public final void easeCamera(
- CameraUpdate update, int durationMs, boolean easingInterpolator, boolean resetTrackingMode, final MapboxMap.CancelableCallback callback) {
- // dismiss tracking, moving camera is equal to a gesture
- cameraPosition = update.getCameraPosition(this);
- if (resetTrackingMode) {
- mapView.resetTrackingModesIfRequired(cameraPosition);
- }
-
- mapView.easeTo(cameraPosition.bearing, cameraPosition.target, getDurationNano(durationMs), cameraPosition.tilt,
- cameraPosition.zoom, easingInterpolator, new CancelableCallback() {
- @Override
- public void onCancel() {
- if (callback != null) {
- callback.onCancel();
- }
- invalidateCameraPosition();
- }
-
- @Override
- public void onFinish() {
- if (callback != null) {
- callback.onFinish();
- }
- invalidateCameraPosition();
- }
- });
+ /**
+ * Gradually move the camera by a specified duration in milliseconds, zoom will not be affected
+ * unless specified within {@link CameraUpdate}. A callback can be used to be notified when
+ * easing the camera stops. If {@link #getCameraPosition()} is called during the animation, it
+ * will return the current location of the camera in flight.
+ * <p>
+ * Note that this will cancel location tracking mode if enabled.
+ * </p>
+ *
+ * @param update The change that should be applied to the camera.
+ * @param durationMs The duration of the animation in milliseconds. This must be strictly
+ * positive, otherwise an IllegalArgumentException will be thrown.
+ * @param resetTrackingMode True to reset tracking modes if required, false to ignore
+ * @param easingInterpolator True for easing interpolator, false for linear.
+ * @param callback An optional callback to be notified from the main thread when the animation
+ * stops. If the animation stops due to its natural completion, the callback
+ * will be notified with onFinish(). If the animation stops due to interruption
+ * by a later camera movement or a user gesture, onCancel() will be called.
+ * Do not update or ease the camera from within onCancel().
+ */
+ @UiThread
+ public final void easeCamera(final CameraUpdate update, final int durationMs, final boolean easingInterpolator, final boolean resetTrackingMode, final MapboxMap.CancelableCallback callback) {
+ mapView.post(new Runnable() {
+ @Override
+ public void run() {
+ transform.easeCamera(update, durationMs, easingInterpolator, resetTrackingMode, callback);
+ }
+ });
}
/**
@@ -596,58 +634,22 @@ public class MapboxMap {
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
@UiThread
- public final void animateCamera(CameraUpdate update, int durationMs, final MapboxMap.CancelableCallback callback) {
- cameraPosition = update.getCameraPosition(this);
- mapView.resetTrackingModesIfRequired(cameraPosition);
- mapView.flyTo(cameraPosition.bearing, cameraPosition.target, getDurationNano(durationMs), cameraPosition.tilt,
- cameraPosition.zoom, new CancelableCallback() {
- @Override
- public void onCancel() {
- if (callback != null) {
- callback.onCancel();
- }
- invalidateCameraPosition();
- }
-
- @Override
- public void onFinish() {
- if (onCameraChangeListener != null) {
- onCameraChangeListener.onCameraChange(cameraPosition);
- }
-
- if (callback != null) {
- callback.onFinish();
- }
- invalidateCameraPosition();
- }
- });
- }
-
- /**
- * Converts milliseconds to nanoseconds
- *
- * @param durationMs The time in milliseconds
- * @return time in nanoseconds
- */
- private long getDurationNano(long durationMs) {
- return durationMs > 0 ? TimeUnit.NANOSECONDS.convert(durationMs, TimeUnit.MILLISECONDS) : 0;
+ public final void animateCamera(final CameraUpdate update, final int durationMs, final MapboxMap.CancelableCallback callback) {
+ mapView.post(new Runnable() {
+ @Override
+ public void run() {
+ transform.animateCamera(update, durationMs, callback);
+ }
+ });
}
/**
* Invalidates the current camera position by reconstructing it from mbgl
*/
- private void invalidateCameraPosition() {
- if (invalidCameraPosition) {
- invalidCameraPosition = false;
-
- CameraPosition cameraPosition = mapView.invalidateCameraPosition();
- if (cameraPosition != null) {
- this.cameraPosition = cameraPosition;
- }
-
- if (onCameraChangeListener != null) {
- onCameraChangeListener.onCameraChange(this.cameraPosition);
- }
+ void invalidateCameraPosition() {
+ CameraPosition cameraPosition = transform.invalidateCameraPosition();
+ if (cameraPosition != null) {
+ mapView.updateCameraPosition(cameraPosition);
}
}
@@ -659,7 +661,7 @@ public class MapboxMap {
* Resets the map view to face north.
*/
public void resetNorth() {
- mapView.resetNorth();
+ transform.resetNorth();
}
//
@@ -875,7 +877,8 @@ public class MapboxMap {
@UiThread
@NonNull
- public List<MarkerView> addMarkerViews(@NonNull List<? extends BaseMarkerViewOptions> markerViewOptions) {
+ public List<MarkerView> addMarkerViews(@NonNull List<? extends
+ BaseMarkerViewOptions> markerViewOptions) {
return annotationManager.addMarkerViews(markerViewOptions, this);
}
@@ -897,7 +900,8 @@ public class MapboxMap {
*/
@UiThread
@NonNull
- public List<Marker> addMarkers(@NonNull List<? extends BaseMarkerOptions> markerOptionsList) {
+ public List<Marker> addMarkers(@NonNull List<? extends
+ BaseMarkerOptions> markerOptionsList) {
return annotationManager.addMarkers(markerOptionsList, this);
}
@@ -1239,7 +1243,7 @@ public class MapboxMap {
return infoWindowManager.isAllowConcurrentMultipleOpenInfoWindows();
}
- // used by MapView
+ // Internal API
List<InfoWindow> getInfoWindows() {
return infoWindowManager.getInfoWindows();
}
@@ -1248,6 +1252,11 @@ public class MapboxMap {
return annotationManager;
}
+ Transform getTransform() {
+ return transform;
+ }
+
+
//
// Padding
//
@@ -1299,7 +1308,7 @@ public class MapboxMap {
*/
@UiThread
public void setOnCameraChangeListener(@Nullable OnCameraChangeListener listener) {
- onCameraChangeListener = listener;
+ transform.setOnCameraChangeListener(listener);
}
/**
@@ -1410,7 +1419,8 @@ public class MapboxMap {
* use null.
*/
@UiThread
- public void setOnInfoWindowLongClickListener(@Nullable OnInfoWindowLongClickListener listener) {
+ public void setOnInfoWindowLongClickListener(@Nullable OnInfoWindowLongClickListener
+ listener) {
infoWindowManager.setOnInfoWindowLongClickListener(listener);
}
@@ -1493,7 +1503,8 @@ public class MapboxMap {
* To unset the callback, use null.
*/
@UiThread
- public void setOnMyLocationChangeListener(@Nullable MapboxMap.OnMyLocationChangeListener listener) {
+ public void setOnMyLocationChangeListener(@Nullable MapboxMap.OnMyLocationChangeListener
+ listener) {
mapView.setOnMyLocationChangeListener(listener);
}
@@ -1504,12 +1515,14 @@ public class MapboxMap {
* To unset the callback, use null.
*/
@UiThread
- public void setOnMyLocationTrackingModeChangeListener(@Nullable MapboxMap.OnMyLocationTrackingModeChangeListener listener) {
+ public void setOnMyLocationTrackingModeChangeListener
+ (@Nullable MapboxMap.OnMyLocationTrackingModeChangeListener listener) {
onMyLocationTrackingModeChangeListener = listener;
}
// used by MapView
- MapboxMap.OnMyLocationTrackingModeChangeListener getOnMyLocationTrackingModeChangeListener() {
+ MapboxMap.OnMyLocationTrackingModeChangeListener getOnMyLocationTrackingModeChangeListener
+ () {
return onMyLocationTrackingModeChangeListener;
}
@@ -1520,7 +1533,8 @@ public class MapboxMap {
* To unset the callback, use null.
*/
@UiThread
- public void setOnMyBearingTrackingModeChangeListener(@Nullable OnMyBearingTrackingModeChangeListener listener) {
+ public void setOnMyBearingTrackingModeChangeListener
+ (@Nullable OnMyBearingTrackingModeChangeListener listener) {
onMyBearingTrackingModeChangeListener = listener;
}
@@ -1582,7 +1596,8 @@ public class MapboxMap {
*/
@UiThread
@NonNull
- public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates, @Nullable String... layerIds) {
+ public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates, @Nullable String...
+ layerIds) {
return mapView.getNativeMapView().queryRenderedFeatures(coordinates, layerIds);
}
@@ -1595,11 +1610,26 @@ public class MapboxMap {
*/
@UiThread
@NonNull
- public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates, @Nullable String... layerIds) {
+ public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates, @Nullable String...
+ layerIds) {
return mapView.getNativeMapView().queryRenderedFeatures(coordinates, layerIds);
}
//
+ // Innner classes
+ //
+
+ private class CameraInvalidator implements MapView.OnMapChangedListener {
+
+ @Override
+ public void onMapChanged(@MapView.MapChange int change) {
+ if (change == MapView.REGION_DID_CHANGE_ANIMATED) {
+ invalidateCameraPosition();
+ }
+ }
+ }
+
+ //
// Interfaces
//
@@ -1963,23 +1993,4 @@ public class MapboxMap {
*/
void onSnapshotReady(Bitmap snapshot);
}
-
- private class MapChangeCameraPositionListener implements MapView.OnMapChangedListener {
-
- private static final long UPDATE_RATE_MS = 400;
- private long previousUpdateTimestamp = 0;
-
- @Override
- public void onMapChanged(@MapView.MapChange int change) {
- if (change >= MapView.REGION_WILL_CHANGE && change <= MapView.REGION_DID_CHANGE_ANIMATED) {
- invalidCameraPosition = true;
- long currentTime = SystemClock.elapsedRealtime();
- if (currentTime < previousUpdateTimestamp) {
- return;
- }
- invalidateCameraPosition();
- previousUpdateTimestamp = currentTime + UPDATE_RATE_MS;
- }
- }
- }
}