From 4790195a904f02200cd0360f094b5ae12ae2899a Mon Sep 17 00:00:00 2001 From: danesfeder Date: Tue, 27 Feb 2018 17:07:33 -0500 Subject: [PATCH] Start work on animation for bearing --- .../plugins/locationlayer/LocationLayer.java | 3 +- .../locationlayer/LocationLayerAnimator.java | 38 ++++++++----------- .../locationlayer/LocationLayerCamera.java | 12 +----- .../locationlayer/LocationLayerPlugin.java | 13 ++++++- 4 files changed, 30 insertions(+), 36 deletions(-) diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java index 860ae1ef2..9ae9e4738 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java @@ -76,8 +76,9 @@ final class LocationLayer implements LocationLayerAnimator.OnAnimationsValuesCha private final Map sourceMap = new HashMap<>(); LocationLayer(MapView mapView, MapboxMap mapboxMap, LocationLayerOptions options) { - this.mapboxMap = mapboxMap; this.context = mapView.getContext(); + this.mapboxMap = mapboxMap; + this.options = options; initializeComponents(); setRenderMode(RenderMode.NORMAL); } diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerAnimator.java index 5c7859d00..174685ee7 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerAnimator.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerAnimator.java @@ -1,7 +1,6 @@ package com.mapbox.mapboxsdk.plugins.locationlayer; import android.animation.ValueAnimator; -import android.location.Location; import android.support.annotation.NonNull; import com.mapbox.mapboxsdk.geometry.LatLng; @@ -26,31 +25,22 @@ void removeListener(OnAnimationsValuesChangeListener listener) { listeners.remove(listener); } - void feedNewLocation(@NonNull Location previousLocation, @NonNull Location newLocation) { - LatLng previousLatLng; - if (latLngAnimator != null) { - previousLatLng = (LatLng) latLngAnimator.getAnimatedValue(); - } else { - previousLatLng = new LatLng(previousLocation); - } - LatLng newLatLng = new LatLng(newLocation); + void feedNewLatLng(@NonNull LatLng previousTargetLatLng, @NonNull LatLng targetLatLng) { + cancelLatLngAnimation(); + latLngAnimator = new LatLngAnimator(previousTargetLatLng, targetLatLng, 1000); + // FIXME: 22/02/2018 evaluate duration of animation better - float previousBearing; - if (gpsBearingAnimator != null) { - previousBearing = (float) gpsBearingAnimator.getAnimatedValue(); - } else { - previousBearing = previousLocation.getBearing(); - } + latLngAnimator.addUpdateListener(latLngUpdateListener); + latLngAnimator.start(); + } - cancelLocationAnimations(); - latLngAnimator = new LatLngAnimator(previousLatLng, newLatLng, 1000); - gpsBearingAnimator = new BearingAnimator(previousBearing, newLocation.getBearing(), 1000); + void feedNewGpsBearing(float previousGpsBearing, float targetGpsBearing) { + cancelBearingAnimation(); + float normalizedTargetGpsBearing = Utils.shortestRotation(previousGpsBearing, targetGpsBearing); + gpsBearingAnimator = new BearingAnimator(previousGpsBearing, normalizedTargetGpsBearing, 1000); // FIXME: 22/02/2018 evaluate duration of animation better - latLngAnimator.addUpdateListener(latLngUpdateListener); gpsBearingAnimator.addUpdateListener(gpsBearingUpdateListener); - - latLngAnimator.start(); gpsBearingAnimator.start(); } @@ -102,16 +92,18 @@ interface OnAnimationsValuesChangeListener { } void cancelAllAnimations() { - cancelLocationAnimations(); + cancelLatLngAnimation(); cancelCompassAnimations(); } - private void cancelLocationAnimations() { + private void cancelLatLngAnimation() { if (latLngAnimator != null) { latLngAnimator.cancel(); latLngAnimator.removeAllUpdateListeners(); } + } + private void cancelBearingAnimation() { if (gpsBearingAnimator != null) { gpsBearingAnimator.cancel(); gpsBearingAnimator.removeAllUpdateListeners(); diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerCamera.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerCamera.java index 2b9f6ea22..4c08a7918 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerCamera.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerCamera.java @@ -45,13 +45,10 @@ public void onNewLatLngValue(LatLng latLng) { @Override public void onNewGpsBearingValue(float gpsBearing) { if (cameraMode == CameraMode.TRACKING_GPS - || cameraMode == CameraMode.NONE_GPS) { + || cameraMode == CameraMode.NONE_GPS + || cameraMode == CameraMode.TRACKING_GPS_NORTH) { setBearing(gpsBearing); } - - if (cameraMode == CameraMode.TRACKING_GPS_NORTH) { - setBearing(0); - } } @Override @@ -62,8 +59,3 @@ public void onNewCompassBearingValue(float compassBearing) { } } } - - -/* - - float targetBearing = Utils.shortestRotation(0, (float) bearing);*/ diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index 15ca6c848..621ae2754 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -525,10 +525,19 @@ private void updateLocation(final Location location) { } staleStateManager.updateLatestLocationTime(); + if (lastLocation != null) { - locationLayerAnimator.feedNewLocation(lastLocation, location); + LatLng previousLatLngTarget = new LatLng(lastLocation); + LatLng latLngTarget = new LatLng(location); + locationLayerAnimator.feedNewLatLng(previousLatLngTarget, latLngTarget); + + float previousGpsBearing = lastLocation.getBearing(); + if (getCameraMode() == CameraMode.TRACKING_GPS_NORTH && mapboxMap.getCameraPosition().bearing != 0) { + locationLayerAnimator.feedNewGpsBearing(previousGpsBearing, 0); + } else { + locationLayerAnimator.feedNewGpsBearing(previousGpsBearing, location.getBearing()); + } } - lastLocation = location; }