From 6f79da7a82dde7dc529f4cd2217152781f792900 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Mon, 19 Feb 2018 17:38:57 +0100 Subject: [PATCH 01/24] Add initial animators --- .../locationlayer/camera/BearingAnimator.java | 19 +++ .../locationlayer/camera/LatLngAnimator.java | 41 +++++ .../locationlayer/camera/MapAnimator.java | 160 ++++++++++++++++++ .../locationlayer/camera/TiltAnimator.java | 19 +++ .../locationlayer/camera/ZoomAnimator.java | 19 +++ 5 files changed, 258 insertions(+) create mode 100644 plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/BearingAnimator.java create mode 100644 plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LatLngAnimator.java create mode 100644 plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/MapAnimator.java create mode 100644 plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/TiltAnimator.java create mode 100644 plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/ZoomAnimator.java diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/BearingAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/BearingAnimator.java new file mode 100644 index 000000000..3077d66fd --- /dev/null +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/BearingAnimator.java @@ -0,0 +1,19 @@ +package com.mapbox.mapboxsdk.plugins.locationlayer.camera; + +import android.animation.FloatEvaluator; +import android.animation.ValueAnimator; + +public class BearingAnimator extends ValueAnimator { + + private float targetBearing; + + public BearingAnimator(double targetBearing, long duration) { + setEvaluator(new FloatEvaluator()); + setDuration(duration); + this.targetBearing = (float) targetBearing; + } + + public float getTargetBearing() { + return targetBearing; + } +} diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LatLngAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LatLngAnimator.java new file mode 100644 index 000000000..a0838ca21 --- /dev/null +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LatLngAnimator.java @@ -0,0 +1,41 @@ +package com.mapbox.mapboxsdk.plugins.locationlayer.camera; + +import android.animation.TypeEvaluator; +import android.animation.ValueAnimator; +import android.support.annotation.NonNull; + +import com.mapbox.mapboxsdk.geometry.LatLng; + +public class LatLngAnimator extends ValueAnimator { + + private LatLng target; + + public LatLngAnimator(@NonNull LatLng target, long duration) { + setDuration(duration); + this.target = target; + } + + @Override + public void setObjectValues(Object... values) { + super.setObjectValues(values); + setEvaluator(new LatLngEvaluator()); + } + + public LatLng getTarget() { + return target; + } + + private static class LatLngEvaluator implements TypeEvaluator { + + private final LatLng latLng = new LatLng(); + + @Override + public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) { + latLng.setLatitude(startValue.getLatitude() + + ((endValue.getLatitude() - startValue.getLatitude()) * fraction)); + latLng.setLongitude(startValue.getLongitude() + + ((endValue.getLongitude() - startValue.getLongitude()) * fraction)); + return latLng; + } + } +} diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/MapAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/MapAnimator.java new file mode 100644 index 000000000..8ebc21292 --- /dev/null +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/MapAnimator.java @@ -0,0 +1,160 @@ +package com.mapbox.mapboxsdk.plugins.locationlayer.camera; + +import android.animation.Animator; +import android.animation.AnimatorSet; +import android.animation.ValueAnimator; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.mapbox.mapboxsdk.camera.CameraPosition; +import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.maps.MapboxMap; + +import java.util.ArrayList; +import java.util.List; + +public class MapAnimator { + + private List animators; + private AnimatorSet animatorSet; + + private MapAnimator(List animators) { + this.animators = animators; + animatorSet = new AnimatorSet(); + } + + public void playTogether() { + animatorSet.playTogether(animators); + animatorSet.start(); + } + + public void playTogether(@Nullable Animator.AnimatorListener listener) { + animatorSet.addListener(listener); + playTogether(); + } + + public void playSequentially() { + animatorSet.playSequentially(animators); + animatorSet.start(); + } + + public void playSequentially(@Nullable Animator.AnimatorListener listener) { + animatorSet.addListener(listener); + playSequentially(); + } + + public void cancel() { + animatorSet.cancel(); + } + + public static Builder builder(MapboxMap mapboxMap) { + return new Builder(mapboxMap); + } + + public static final class Builder { + + private final MapboxMap mapboxMap; + private List animators; + private CameraPosition currentPosition; + + private Builder(MapboxMap mapboxMap) { + this.mapboxMap = mapboxMap; + this.animators = new ArrayList<>(); + // Get the current target from the current map camera position + currentPosition = mapboxMap.getCameraPosition(); + } + + public Builder addLatLngAnimator(@NonNull LatLngAnimator latLngAnimator) { + + LatLng currentTarget = currentPosition.target; + if (currentTarget == latLngAnimator.getTarget()) { + return this; + } + + latLngAnimator.setObjectValues(currentTarget, latLngAnimator.getTarget()); + latLngAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mapboxMap.setLatLng((LatLng) animation.getAnimatedValue()); + } + }); + + animators.add(latLngAnimator); + return this; + } + + public Builder addZoomAnimator(@NonNull ZoomAnimator zoomAnimator) { + + float currentZoom = (float) currentPosition.zoom; + if (currentZoom == zoomAnimator.getTargetZoom()) { + return this; + } + + zoomAnimator.setFloatValues(currentZoom, zoomAnimator.getTargetZoom()); + zoomAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mapboxMap.setZoom((Float) animation.getAnimatedValue()); + } + }); + + animators.add(zoomAnimator); + return this; + } + + public Builder addBearingAnimator(@NonNull BearingAnimator bearingAnimator) { + + float currentBearing = (float) currentPosition.bearing; + float normalizedTargetBearing = normalizeBearing(currentBearing, bearingAnimator.getTargetBearing()); + if (currentBearing == normalizedTargetBearing) { + return this; + } + + bearingAnimator.setFloatValues(currentBearing, normalizedTargetBearing); + bearingAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mapboxMap.setBearing((Float) animation.getAnimatedValue()); + } + }); + + animators.add(bearingAnimator); + return this; + } + + public Builder addTiltAnimator(@NonNull TiltAnimator tiltAnimator) { + + float currentTilt = (float) currentPosition.tilt; + if (currentTilt == tiltAnimator.getTargetTilt()) { + return this; + } + + tiltAnimator.setFloatValues(currentTilt, tiltAnimator.getTargetTilt()); + tiltAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mapboxMap.setTilt((Float) animation.getAnimatedValue()); + } + }); + + animators.add(tiltAnimator); + return this; + } + + + + public MapAnimator build() { + return new MapAnimator(animators); + } + } + + private static float normalizeBearing(float currentBearing, float targetBearing) { + double diff = currentBearing - targetBearing; + if (diff > 180.0f) { + targetBearing += 360.0f; + } else if (diff < -180.0f) { + targetBearing -= 360.f; + } + return targetBearing; + } +} diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/TiltAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/TiltAnimator.java new file mode 100644 index 000000000..216afb148 --- /dev/null +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/TiltAnimator.java @@ -0,0 +1,19 @@ +package com.mapbox.mapboxsdk.plugins.locationlayer.camera; + +import android.animation.FloatEvaluator; +import android.animation.ValueAnimator; + +public class TiltAnimator extends ValueAnimator { + + private float targetTilt; + + public TiltAnimator(double targetTilt, long duration) { + setEvaluator(new FloatEvaluator()); + setDuration(duration); + this.targetTilt = (float) targetTilt; + } + + public float getTargetTilt() { + return targetTilt; + } +} diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/ZoomAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/ZoomAnimator.java new file mode 100644 index 000000000..34327760e --- /dev/null +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/ZoomAnimator.java @@ -0,0 +1,19 @@ +package com.mapbox.mapboxsdk.plugins.locationlayer.camera; + +import android.animation.FloatEvaluator; +import android.animation.ValueAnimator; + +public class ZoomAnimator extends ValueAnimator { + + private float targetZoom; + + public ZoomAnimator(double targetZoom, long duration) { + setEvaluator(new FloatEvaluator()); + setDuration(duration); + this.targetZoom = (float) targetZoom; + } + + public float getTargetZoom() { + return targetZoom; + } +} From d25dafa144f0e4eef8ab152b4874fb437b80d766 Mon Sep 17 00:00:00 2001 From: Dan Nesfeder Date: Wed, 21 Feb 2018 15:40:26 +0100 Subject: [PATCH 02/24] Add Camera and Tracking Modes (#294) * Add style / tracking modes for camera integration * Fix broken tests * API tweaks and example update --- .../locationlayer/LocationLayerTest.java | 20 +-- .../places/autocomplete/data/TestData.java | 2 +- .../location/CompassListenerActivity.java | 2 +- .../LocationLayerMapChangeActivity.java | 2 +- .../location/LocationLayerModesActivity.java | 105 ++++++++++++---- .../ManualLocationUpdatesActivity.java | 2 +- .../layout/activity_location_layer_mode.xml | 46 ++++--- app/src/main/res/values/strings.xml | 2 + .../locationlayer/LocationLayerMode.java | 15 +-- .../locationlayer/LocationLayerPlugin.java | 107 ++++++++++------ .../locationlayer/LocationLayerTracking.java | 68 ++++++++++ .../plugins/locationlayer/Utils.java | 4 +- .../camera/LocationLayerCamera.java | 119 ++++++++++++++++++ 13 files changed, 387 insertions(+), 107 deletions(-) create mode 100644 plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTracking.java create mode 100644 plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LocationLayerCamera.java diff --git a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTest.java b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTest.java index 4c6e55e45..7698a6b5e 100644 --- a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTest.java +++ b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTest.java @@ -85,7 +85,7 @@ public void locationSourceAdded() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); assertTrue(mapboxMap.getSource(LOCATION_SOURCE) != null); } }); @@ -97,7 +97,7 @@ public void locationTrackingLayersAdded() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); assertTrue(mapboxMap.getLayer(ACCURACY_LAYER) != null); assertTrue(mapboxMap.getLayer(BACKGROUND_LAYER) != null); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); @@ -111,7 +111,7 @@ public void locationBearingLayersAdded() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.COMPASS); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.COMPASS); assertTrue(mapboxMap.getLayer(ACCURACY_LAYER) != null); assertTrue(mapboxMap.getLayer(BACKGROUND_LAYER) != null); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); @@ -126,7 +126,7 @@ public void locationNavigationLayersAdded() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.COMPASS); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.COMPASS); assertTrue(mapboxMap.getLayer(NAVIGATION_LAYER) != null); } }); @@ -138,9 +138,9 @@ public void locationLayerModeCorrectlySetToNone() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NONE); + locationLayerPlugin.setLocationLayerEnabled(false); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER).getVisibility().getValue() .equals(Property.NONE)); } @@ -153,11 +153,11 @@ public void onMapChangeLocationLayerRedrawn() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); mapboxMap.setStyleUrl(Style.SATELLITE); uiController.loopMainThreadForAtLeast(500); - assertEquals(locationLayerPlugin.getLocationLayerMode(), LocationLayerMode.TRACKING); + assertEquals(locationLayerPlugin.getLocationLayerMode(), LocationLayerMode.NORMAL); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER).getVisibility().getValue() .equals(Property.VISIBLE)); @@ -175,7 +175,7 @@ public void whenStaleTimeSet_iconsDoChangeAtAppropriateTime() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); SymbolLayer symbolLayer = mapboxMap.getLayerAs(FOREGROUND_LAYER); assert symbolLayer != null; assertThat(symbolLayer.getIconImage().getValue(), equalTo(FOREGROUND_ICON)); @@ -193,7 +193,7 @@ public void whenDrawableChanged_continuesUsingStaleIcons() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); locationLayerPlugin.applyStyle(LocationLayerOptions.builder(context).staleStateDelay(100).build()); locationLayerPlugin.forceLocationUpdate(location); uiController.loopMainThreadForAtLeast(200); diff --git a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/data/TestData.java b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/data/TestData.java index ee97f639c..01e923c94 100644 --- a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/data/TestData.java +++ b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/data/TestData.java @@ -1,7 +1,7 @@ package com.mapbox.mapboxsdk.plugins.places.autocomplete.data; import com.google.gson.JsonObject; -import com.mapbox.geocoding.v5.models.CarmenFeature; +import com.mapbox.api.geocoding.v5.models.CarmenFeature; import com.mapbox.plugins.places.autocomplete.data.entity.SearchHistoryEntity; final class TestData { diff --git a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/CompassListenerActivity.java b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/CompassListenerActivity.java index 8e4b32c6e..5e94b7622 100644 --- a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/CompassListenerActivity.java +++ b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/CompassListenerActivity.java @@ -40,7 +40,7 @@ protected void onCreate(Bundle savedInstanceState) { public void onMapReady(final MapboxMap mapboxMap) { LocationEngine locationEngine = new LostLocationEngine(this); locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine); - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.COMPASS); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.COMPASS); locationLayerPlugin.addCompassListener(new CompassListener() { @Override public void onCompassChanged(float userHeading) { diff --git a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/LocationLayerMapChangeActivity.java b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/LocationLayerMapChangeActivity.java index f1a27f8a8..941a86eef 100644 --- a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/LocationLayerMapChangeActivity.java +++ b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/LocationLayerMapChangeActivity.java @@ -52,7 +52,7 @@ public void onMapReady(MapboxMap mapboxMap) { locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); locationEngine.activate(); locationPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine); - locationPlugin.setLocationLayerEnabled(LocationLayerMode.COMPASS); + locationPlugin.setLocationLayerMode(LocationLayerMode.COMPASS); } @OnClick(R.id.fabStyles) diff --git a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/LocationLayerModesActivity.java b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/LocationLayerModesActivity.java index bd2bb2ad9..598a4db0d 100644 --- a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/LocationLayerModesActivity.java +++ b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/LocationLayerModesActivity.java @@ -1,12 +1,17 @@ package com.mapbox.mapboxsdk.plugins.testapp.activity.location; +import android.annotation.SuppressLint; import android.location.Location; import android.os.Bundle; import android.support.annotation.VisibleForTesting; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.ListPopupWindow; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.TextView; import android.widget.Toast; import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; @@ -16,6 +21,7 @@ import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerMode; import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerTracking; import com.mapbox.mapboxsdk.plugins.locationlayer.OnLocationLayerClickListener; import com.mapbox.mapboxsdk.plugins.testapp.R; import com.mapbox.services.android.location.LostLocationEngine; @@ -23,6 +29,9 @@ import com.mapbox.services.android.telemetry.location.LocationEngineListener; import com.mapbox.services.android.telemetry.location.LocationEnginePriority; +import java.util.ArrayList; +import java.util.List; + import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; @@ -32,6 +41,14 @@ public class LocationLayerModesActivity extends AppCompatActivity implements OnM @BindView(R.id.map_view) MapView mapView; + @BindView(R.id.tv_mode) + TextView modeText; + @BindView(R.id.tv_tracking) + TextView trackingText; + @BindView(R.id.button_location_mode) + Button locationModeBtn; + @BindView(R.id.button_location_tracking) + Button locationTrackingBtn; private LocationLayerPlugin locationLayerPlugin; private LocationEngine locationEngine; @@ -49,41 +66,23 @@ protected void onCreate(Bundle savedInstanceState) { } @SuppressWarnings( {"MissingPermission"}) - @OnClick(R.id.button_location_mode_none) - public void locationModeNone(View view) { + @OnClick(R.id.button_location_mode) + public void locationMode(View view) { if (locationLayerPlugin == null) { return; } - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NONE); + showModeListDialog(); } - @SuppressWarnings( {"MissingPermission"}) - @OnClick(R.id.button_location_mode_compass) + @OnClick(R.id.button_location_tracking) public void locationModeCompass(View view) { if (locationLayerPlugin == null) { return; } - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.COMPASS); - } - - @SuppressWarnings( {"MissingPermission"}) - @OnClick(R.id.button_location_mode_tracking) - public void locationModeTracking(View view) { - if (locationLayerPlugin == null) { - return; - } - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); - } - - @SuppressWarnings( {"MissingPermission"}) - @OnClick(R.id.button_location_mode_navigation) - public void locationModeNavigation(View view) { - if (locationLayerPlugin == null) { - return; - } - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NAVIGATION); + showTrackingListDialog(); } + @SuppressLint("MissingPermission") @Override public void onMapReady(MapboxMap mapboxMap) { this.mapboxMap = mapboxMap; @@ -93,6 +92,7 @@ public void onMapReady(MapboxMap mapboxMap) { locationEngine.activate(); locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine); locationLayerPlugin.setOnLocationClickListener(this); + locationLayerPlugin.setLocationLayerEnabled(true); getLifecycle().addObserver(locationLayerPlugin); } @@ -191,10 +191,67 @@ public void onConnected() { public void onLocationChanged(Location location) { mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom( new LatLng(location.getLatitude(), location.getLongitude()), 16)); + locationEngine.removeLocationEngineListener(this); } @Override public void onLocationLayerClick() { Toast.makeText(this, "OnLocationLayerClick", Toast.LENGTH_LONG).show(); } + + private void showModeListDialog() { + List modes = new ArrayList<>(); + modes.add("Normal"); + modes.add("Compass"); + modes.add("Navigation"); + ArrayAdapter profileAdapter = new ArrayAdapter<>(this, + android.R.layout.simple_list_item_1, modes); + ListPopupWindow listPopup = new ListPopupWindow(this); + listPopup.setAdapter(profileAdapter); + listPopup.setAnchorView(locationModeBtn); + listPopup.setOnItemClickListener((parent, itemView, position, id) -> { + String selectedMode = modes.get(position); + locationModeBtn.setText(selectedMode); + if (selectedMode.contentEquals("Normal")) { + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); + } else if (selectedMode.contentEquals("Compass")) { + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.COMPASS); + } else if (selectedMode.contentEquals("Navigation")) { + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NAVIGATION); + } + listPopup.dismiss(); + }); + listPopup.show(); + } + + private void showTrackingListDialog() { + List trackingTypes = new ArrayList<>(); + trackingTypes.add("None"); + trackingTypes.add("Tracking"); + trackingTypes.add("Tracking Compass"); + trackingTypes.add("Tracking GPS"); + trackingTypes.add("Tracking GPS North"); + ArrayAdapter profileAdapter = new ArrayAdapter<>(this, + android.R.layout.simple_list_item_1, trackingTypes); + ListPopupWindow listPopup = new ListPopupWindow(this); + listPopup.setAdapter(profileAdapter); + listPopup.setAnchorView(locationTrackingBtn); + listPopup.setOnItemClickListener((parent, itemView, position, id) -> { + String selectedTrackingType = trackingTypes.get(position); + locationTrackingBtn.setText(selectedTrackingType); + if (selectedTrackingType.contentEquals("None")) { + locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.NONE); + } else if (selectedTrackingType.contentEquals("Tracking")) { + locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING); + } else if (selectedTrackingType.contentEquals("Tracking Compass")) { + locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING_COMPASS); + } else if (selectedTrackingType.contentEquals("Tracking GPS")) { + locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING_GPS); + } else if (selectedTrackingType.contentEquals("Tracking GPS North")) { + locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING_GPS_NORTH); + } + listPopup.dismiss(); + }); + listPopup.show(); + } } \ No newline at end of file diff --git a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/ManualLocationUpdatesActivity.java b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/ManualLocationUpdatesActivity.java index c50fd06b2..874e752f0 100644 --- a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/ManualLocationUpdatesActivity.java +++ b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/location/ManualLocationUpdatesActivity.java @@ -71,7 +71,7 @@ public void onMapReady(MapboxMap mapboxMap) { locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); locationEngine.activate(); locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, null); - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); getLifecycle().addObserver(locationLayerPlugin); } diff --git a/app/src/main/res/layout/activity_location_layer_mode.xml b/app/src/main/res/layout/activity_location_layer_mode.xml index 69fdc4ede..9217f2654 100644 --- a/app/src/main/res/layout/activity_location_layer_mode.xml +++ b/app/src/main/res/layout/activity_location_layer_mode.xml @@ -32,40 +32,46 @@ tools:layout_constraintLeft_creator="1" tools:layout_constraintRight_creator="1"> -