From 6a680b9f2c3cecdd68ee37b620441018b5675202 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Tue, 20 Feb 2018 14:12:41 +0100 Subject: [PATCH 1/3] Add style / tracking modes for camera integration --- .../locationlayer/LocationLayerTest.java | 20 +-- .../location/CompassListenerActivity.java | 4 +- .../LocationLayerMapChangeActivity.java | 4 +- .../location/LocationLayerModesActivity.java | 20 ++- .../ManualLocationUpdatesActivity.java | 4 +- .../locationlayer/LocationLayerPlugin.java | 145 +++++++++++------- ...LayerMode.java => LocationLayerStyle.java} | 21 +-- .../locationlayer/LocationLayerTracking.java | 68 ++++++++ .../plugins/locationlayer/Utils.java | 4 +- .../camera/LocationLayerCamera.java | 119 ++++++++++++++ 10 files changed, 314 insertions(+), 95 deletions(-) rename plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/{LocationLayerMode.java => LocationLayerStyle.java} (68%) 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..b99877cd3 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.setLocationLayerStyle(LocationLayerStyle.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.setLocationLayerStyle(LocationLayerStyle.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.setLocationLayerStyle(LocationLayerStyle.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.setLocationLayerStyle(LocationLayerStyle.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.setLocationLayerStyle(LocationLayerStyle.NORMAL); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NONE); + locationLayerPlugin.setLocationLayerEnabled(LocationLayerStyle.NONE); 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.setLocationLayerStyle(LocationLayerStyle.NORMAL); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); mapboxMap.setStyleUrl(Style.SATELLITE); uiController.loopMainThreadForAtLeast(500); - assertEquals(locationLayerPlugin.getLocationLayerMode(), LocationLayerMode.TRACKING); + assertEquals(locationLayerPlugin.getLocationLayerStyle(), LocationLayerStyle.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.setLocationLayerStyle(LocationLayerStyle.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.setLocationLayerStyle(LocationLayerStyle.NORMAL); locationLayerPlugin.applyStyle(LocationLayerOptions.builder(context).staleStateDelay(100).build()); locationLayerPlugin.forceLocationUpdate(location); uiController.loopMainThreadForAtLeast(200); 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..a78631153 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 @@ -9,7 +9,7 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mapbox.mapboxsdk.plugins.locationlayer.CompassListener; -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerMode; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerStyle; import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin; import com.mapbox.mapboxsdk.plugins.testapp.R; import com.mapbox.services.android.location.LostLocationEngine; @@ -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.setLocationLayerStyle(LocationLayerStyle.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..3f762841b 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 @@ -9,7 +9,7 @@ import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerMode; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerStyle; import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin; import com.mapbox.mapboxsdk.plugins.testapp.R; import com.mapbox.mapboxsdk.plugins.testapp.Utils; @@ -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.setLocationLayerStyle(LocationLayerStyle.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..f6731c0ed 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,5 +1,6 @@ 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; @@ -14,8 +15,9 @@ import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerMode; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerStyle; 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; @@ -54,36 +56,37 @@ public void locationModeNone(View view) { if (locationLayerPlugin == null) { return; } - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NONE); + locationLayerPlugin.setLocationLayerEnabled(false); } - @SuppressWarnings( {"MissingPermission"}) @OnClick(R.id.button_location_mode_compass) public void locationModeCompass(View view) { if (locationLayerPlugin == null) { return; } - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.COMPASS); + locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.COMPASS); + locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING_COMPASS); } - @SuppressWarnings( {"MissingPermission"}) @OnClick(R.id.button_location_mode_tracking) public void locationModeTracking(View view) { if (locationLayerPlugin == null) { return; } - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); + locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.NORMAL); + locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING); } - @SuppressWarnings( {"MissingPermission"}) @OnClick(R.id.button_location_mode_navigation) public void locationModeNavigation(View view) { if (locationLayerPlugin == null) { return; } - locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NAVIGATION); + locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.NAVIGATION); + locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING_GPS_NORTH); } + @SuppressLint("MissingPermission") @Override public void onMapReady(MapboxMap mapboxMap) { this.mapboxMap = mapboxMap; @@ -93,6 +96,7 @@ public void onMapReady(MapboxMap mapboxMap) { locationEngine.activate(); locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine); locationLayerPlugin.setOnLocationClickListener(this); + locationLayerPlugin.setLocationLayerEnabled(true); getLifecycle().addObserver(locationLayerPlugin); } 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..275365afb 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 @@ -10,7 +10,7 @@ import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerMode; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerStyle; import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin; import com.mapbox.mapboxsdk.plugins.testapp.R; import com.mapbox.mapboxsdk.plugins.testapp.Utils; @@ -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.setLocationLayerStyle(LocationLayerStyle.NORMAL); getLifecycle().addObserver(locationLayerPlugin); } 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 a0515f19b..4f3eab305 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 @@ -21,6 +21,7 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveListener; import com.mapbox.mapboxsdk.maps.MapboxMap.OnMapClickListener; +import com.mapbox.mapboxsdk.plugins.locationlayer.camera.LocationLayerCamera; import com.mapbox.services.android.telemetry.location.LocationEngine; import com.mapbox.services.android.telemetry.location.LocationEngineListener; import com.mapbox.services.commons.geojson.Point; @@ -40,11 +41,13 @@ * The Location layer plugin provides location awareness to your mobile application. Enabling this * plugin provides a contextual experience to your users by showing an icon representing the users * current location. A few different modes are offered to provide the right context to your users at - * the correct time. {@link LocationLayerMode#TRACKING} simply shows the users location on the map - * represented as a dot. {@link LocationLayerMode#COMPASS} mode allows you to display an arrow icon + * the correct time. {@link LocationLayerStyle#NORMAL} simply shows the users location on the map + * represented as a dot. {@link LocationLayerStyle#COMPASS} mode allows you to display an arrow icon * (by default) that points in the direction the device is pointing in. - * {@link LocationLayerMode#NAVIGATION} can be used in conjunction with our Navigation SDK to - * display a larger icon we call the user puck. Lastly, {@link LocationLayerMode#NONE} can be used + * {@link LocationLayerStyle#NAVIGATION} can be used in conjunction with our Navigation SDK to + * display a larger icon we call the user puck. + *

+ * Lastly, {@link LocationLayerPlugin#setLocationLayerEnabled(boolean)} can be used * to disable the Location Layer but keep the instance around till the activity is destroyed. *

* Using this plugin requires you to request permission beforehand manually or using @@ -61,12 +64,14 @@ public final class LocationLayerPlugin implements LocationEngineListener, Compas private LocationLayer locationLayer; private CompassManager compassManager; private LocationEngine locationEngine; + private LocationLayerCamera camera; private final MapboxMap mapboxMap; private final MapView mapView; // Enabled booleans - @LocationLayerMode.Mode - private int locationLayerMode; + @LocationLayerStyle.Style + private int locationLayerStyle; + private boolean isEnabled; // Previous compass and location values private float previousMagneticHeading; @@ -128,66 +133,79 @@ public LocationLayerPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMa private void initialize() { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); staleStateRunnable = new StaleStateRunnable(options.staleStateDelay()); - locationLayerMode = LocationLayerMode.NONE; + locationLayerStyle = LocationLayerStyle.NORMAL; locationLayer = new LocationLayer(mapView, mapboxMap, options, staleStateRunnable); compassManager = new CompassManager(mapView.getContext(), this); + camera = new LocationLayerCamera(mapboxMap); + } + + @RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION}) + public void setLocationLayerEnabled(boolean isEnabled) { + this.isEnabled = isEnabled; + if (isEnabled) { + enableLocationLayerPlugin(); + } else { + disableLocationLayerPlugin(); + } + } + + private void enableLocationLayerPlugin() { + // Set an initial location if one is available and the locationEngines not null + if (locationEngine != null) { + setLastLocation(); + locationEngine.addLocationEngineListener(this); + } + + toggleCameraListener(); + locationLayer.setLayersVisibility(true); } /** * After creating an instance of this plugin, you can use this API to enable the location mode of - * your choice. These modes can be found in the {@link LocationLayerMode} class and the parameter + * your choice. These modes can be found in the {@link LocationLayerStyle} class and the parameter * only accepts one of those modes. Note that before enabling the My Location layer, you will need * to ensure that you have the requested the required user location permissions. *

*

* - * @param locationLayerMode one of the modes found in {@link LocationLayerMode} + * @param locationLayerStyle one of the modes found in {@link LocationLayerStyle} * @since 0.1.0 */ - @RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION}) - public void setLocationLayerEnabled(@LocationLayerMode.Mode int locationLayerMode) { - this.locationLayerMode = locationLayerMode; - if (locationLayerMode != LocationLayerMode.NONE) { - locationLayer.setLayersVisibility(true); - - // Set an initial location if one is available and the locationEngines not null - if (locationEngine != null) { - setLastLocation(); - locationEngine.addLocationEngineListener(this); - } + public void setLocationLayerStyle(@LocationLayerStyle.Style int locationLayerStyle) { + this.locationLayerStyle = locationLayerStyle; + + if (locationLayerStyle == LocationLayerStyle.COMPASS) { + setLinearAnimation(false); + setNavigationEnabled(false); + setMyBearingEnabled(true); + } else if (locationLayerStyle == LocationLayerStyle.NAVIGATION) { + setMyBearingEnabled(false); + setNavigationEnabled(true); + } else if (locationLayerStyle == LocationLayerStyle.NORMAL) { + setLinearAnimation(false); + setMyBearingEnabled(false); + setNavigationEnabled(false); + } + } - toggleCameraListener(); - - if (locationLayerMode == LocationLayerMode.COMPASS) { - setLinearAnimation(false); - setNavigationEnabled(false); - setMyBearingEnabled(true); - } else if (locationLayerMode == LocationLayerMode.NAVIGATION) { - setMyBearingEnabled(false); - setNavigationEnabled(true); - } else if (locationLayerMode == LocationLayerMode.TRACKING) { - setLinearAnimation(false); - setMyBearingEnabled(false); - setNavigationEnabled(false); - } - } else { - disableLocationLayerPlugin(); + public void setLocationLayerTracking(@LocationLayerTracking.Mode int trackingMode) { + if (camera != null) { + camera.setTrackingMode(trackingMode); } } /** * Returns the current location mode being used with this plugin. * - * @return on of the {@link LocationLayerMode} values + * @return on of the {@link LocationLayerStyle} values * @since 0.1.0 */ - public int getLocationLayerMode() { - return locationLayerMode; + public int getLocationLayerStyle() { + return locationLayerStyle; } public LocationLayerOptions getLocationLayerOptions() { @@ -236,6 +254,13 @@ public void applyStyle(LocationLayerOptions options) { */ public void forceLocationUpdate(@Nullable Location location) { updateLocation(location); + updateCameraLocation(location); + } + + private void updateCameraLocation(Location location) { + if (camera != null) { + camera.moveToLocation(location); + } } /** @@ -250,7 +275,7 @@ public void forceLocationUpdate(@Nullable Location location) { public void setLocationEngine(@Nullable LocationEngine locationEngine) { if (locationEngine != null) { this.locationEngine = locationEngine; - setLocationLayerEnabled(locationLayerMode); + setLocationLayerStyle(locationLayerStyle); } else if (this.locationEngine != null) { this.locationEngine.removeLocationEngineListener(this); this.locationEngine = null; @@ -297,12 +322,12 @@ public void onStop() { @RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION}) @OnLifecycleEvent(Lifecycle.Event.ON_START) public void onStart() { - if (locationLayerMode != LocationLayerMode.NONE) { - setLocationLayerEnabled(locationLayerMode); + if (isEnabled) { + setLocationLayerStyle(locationLayerStyle); } if (!compassManager.getCompassListeners().isEmpty() - || (locationLayerMode == LocationLayerMode.COMPASS && compassManager.isSensorAvailable())) { + || (locationLayerStyle == LocationLayerStyle.COMPASS && compassManager.isSensorAvailable())) { compassManager.onStart(); } if (mapboxMap != null) { @@ -394,6 +419,7 @@ public void onConnected() { @Override public void onLocationChanged(Location location) { updateLocation(location); + updateCameraLocation(location); } @Override @@ -407,7 +433,7 @@ public void onCompassAccuracyChange(int compassStatus) { } private void toggleCameraListener() { - if (locationLayerMode == LocationLayerMode.NAVIGATION) { + if (locationLayerStyle == LocationLayerStyle.NAVIGATION) { mapboxMap.removeOnCameraMoveListener(this); return; } @@ -420,16 +446,16 @@ private void updateLocation(Location location) { locationUpdateTimestamp = SystemClock.elapsedRealtime(); return; } - if (locationLayerMode == LocationLayerMode.NAVIGATION && location.hasBearing()) { + if (locationLayerStyle == LocationLayerStyle.NAVIGATION && location.hasBearing()) { bearingChangeAnimate(location.getBearing()); - } else if (locationLayerMode != LocationLayerMode.NAVIGATION) { + } else if (locationLayerStyle != LocationLayerStyle.NAVIGATION) { locationLayer.updateAccuracyRadius(location); } setLocation(location); } /** - * disable the location layer plugin if the locationLayerMode is set to none. + * disable the location layer plugin if the locationLayerStyle is set to none. */ private void disableLocationLayerPlugin() { if (locationEngine != null) { @@ -447,7 +473,7 @@ private void setLastLocation() { Location lastLocation = locationEngine.getLastLocation(); if (lastLocation != null) { setLocation(lastLocation); - if (locationLayerMode != LocationLayerMode.NAVIGATION) { + if (locationLayerStyle != LocationLayerStyle.NAVIGATION) { locationLayer.updateAccuracyRadius(lastLocation); } } @@ -488,8 +514,9 @@ private void mapStyleFinishedLoading() { // recreate runtime style components locationLayer = new LocationLayer(mapView, mapboxMap, options, staleStateRunnable); // reset state - setLocationLayerEnabled(locationLayerMode); + setLocationLayerStyle(locationLayerStyle); setBearing(previousMagneticHeading); + updateCameraBearing(previousMagneticHeading); if (previousPoint != null) { locationLayer.setLocationPoint(previousPoint); } @@ -630,16 +657,24 @@ private void bearingChangeAnimate(float magneticHeading) { bearingChangeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { - setBearing((float) valueAnimator.getAnimatedValue()); + float bearing = (float) valueAnimator.getAnimatedValue(); + setBearing(bearing); + updateCameraBearing(bearing); } }); bearingChangeAnimator.start(); previousMagneticHeading = magneticHeading; } + private void updateCameraBearing(float bearing) { + if (camera != null) { + camera.updateBearing(bearing); + } + } + private void setBearing(float bearing) { locationLayer.setLayerBearing( - locationLayerMode == LocationLayerMode.NAVIGATION + locationLayerStyle == LocationLayerStyle.NAVIGATION ? NAVIGATION_LAYER : BEARING_LAYER, bearing ); } diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerMode.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerStyle.java similarity index 68% rename from plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerMode.java rename to plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerStyle.java index 3e6f94da3..c77203dd5 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerMode.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerStyle.java @@ -10,32 +10,25 @@ * * @since 0.1.0 */ -public final class LocationLayerMode { +public final class LocationLayerStyle { - private LocationLayerMode() { + private LocationLayerStyle() { // Class should not be initialized } /** * One of these constants should be used when - * {@link LocationLayerPlugin#setLocationLayerEnabled(int)}'s called. The - * mode can be switched at anytime by calling the {@code setLocationLayerEnabled} method passing + * {@link LocationLayerPlugin#setLocationLayerStyle(int)}'s called. The + * mode can be switched at anytime by calling the {@code setLocationLayerStyle} method passing * in the new mode you'd like the location layer to be in. * * @since 0.1.0 */ - @IntDef( {NONE, COMPASS, NAVIGATION, TRACKING}) + @IntDef( {COMPASS, NAVIGATION, NORMAL}) @Retention(RetentionPolicy.SOURCE) - @interface Mode { + @interface Style { } - /** - * All Tracking, bearing, and location are disabled. - * - * @since 0.1.0 - */ - public static final int NONE = 0x00000000; - /** * Tracking the bearing of the user based on sensor data. * @@ -55,5 +48,5 @@ private LocationLayerMode() { * * @since 0.1.0 */ - public static final int TRACKING = 0x00000012; + public static final int NORMAL = 0x00000012; } diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTracking.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTracking.java new file mode 100644 index 000000000..12902e1e7 --- /dev/null +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTracking.java @@ -0,0 +1,68 @@ +package com.mapbox.mapboxsdk.plugins.locationlayer; + +import android.location.Location; +import android.support.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Contains the variety of Location Layer modes which shape the behavior of the plugin. + * + * @since 0.1.0 + */ +public final class LocationLayerTracking { + + private LocationLayerTracking() { + // Class should not be initialized + } + + /** + * Determine the camera tracking behavior in the {@link LocationLayerPlugin}. + * + * @since 0.4.0 + */ + @IntDef( {NONE, TRACKING, TRACKING_COMPASS, TRACKING_GPS, TRACKING_GPS_NORTH}) + @Retention(RetentionPolicy.SOURCE) + public @interface Mode { + } + + /** + * No camera tracking. + * + * @since 0.4.0 + */ + public static final int NONE = 0x00000000; + + /** + * Tracks the user location. + * + * @since 0.4.0 + */ + public static final int TRACKING = 0x0000006; + + /** + * Tracks the user location, with camera bearing + * based on the bearing provided by the compass. + * + * @since 0.4.0 + */ + public static final int TRACKING_COMPASS = 0x00000010; + + /** + * Tracks the user location, with camera bearing + * based on the bearing provided by a normalized {@link Location#getBearing()}. + * + * @since 0.4.0 + */ + public static final int TRACKING_GPS = 0x00000014; + + + /** + * Tracks the user location, with camera bearing + * always set to north. + * + * @since 0.4.0 + */ + public static final int TRACKING_GPS_NORTH = 0x00000018; +} diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/Utils.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/Utils.java index c20d88ab2..851de6375 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/Utils.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/Utils.java @@ -15,7 +15,7 @@ import com.mapbox.services.commons.geojson.Point; -final class Utils { +public final class Utils { private Utils() { // Class should not be initialized @@ -29,7 +29,7 @@ private Utils() { * @return the shortest degree of rotation possible * @since 0.4.0 */ - static float shortestRotation(float magneticHeading, float previousMagneticHeading) { + public static float shortestRotation(float magneticHeading, float previousMagneticHeading) { double diff = previousMagneticHeading - magneticHeading; if (diff > 180.0f) { magneticHeading += 360.0f; diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LocationLayerCamera.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LocationLayerCamera.java new file mode 100644 index 000000000..87dbf9e7b --- /dev/null +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LocationLayerCamera.java @@ -0,0 +1,119 @@ +package com.mapbox.mapboxsdk.plugins.locationlayer.camera; + +import android.location.Location; +import android.view.animation.LinearInterpolator; + +import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerTracking; +import com.mapbox.mapboxsdk.plugins.locationlayer.Utils; + +public class LocationLayerCamera { + + private MapboxMap mapboxMap; + private int trackingMode = LocationLayerTracking.NONE; + private float bearing = -1; + + public LocationLayerCamera(MapboxMap mapboxMap) { + this.mapboxMap = mapboxMap; + } + + public void setTrackingMode(@LocationLayerTracking.Mode int trackingMode) { + this.trackingMode = trackingMode; + } + + public void moveToLocation(Location location) { + if (trackingMode == LocationLayerTracking.NONE) { + return; + } + buildCameraUpdateFromLocation(location); + } + + public void updateBearing(float bearing) { + if (trackingMode == LocationLayerTracking.TRACKING_COMPASS) { + mapboxMap.setBearing(bearing); + } + } + + private void buildCameraUpdateFromLocation(Location location) { + switch (trackingMode) { + case LocationLayerTracking.TRACKING: + case LocationLayerTracking.TRACKING_COMPASS: + buildTrackingAnimation(location); + break; + case LocationLayerTracking.TRACKING_GPS: + buildTrackingGPSAnimation(location); + break; + case LocationLayerTracking.TRACKING_GPS_NORTH: + buildTrackingGPSNorthAnimation(location); + break; + default: + break; + } + } + + private void buildTrackingAnimation(Location location) { + LatLng target = new LatLng(location); + + LatLngAnimator latLngAnimator = new LatLngAnimator(target, 1000); + latLngAnimator.setInterpolator(new LinearInterpolator()); + + MapAnimator.builder(mapboxMap) + .addLatLngAnimator(latLngAnimator) + .build() + .playTogether(); + } + + private void buildTrackingGPSAnimation(Location location) { + MapAnimator.Builder mapAnimation = MapAnimator.builder(mapboxMap); + addLatLngAnimator(location, mapAnimation); + + if (!location.hasBearing()) { + mapAnimation.build().playTogether(); + return; + } + + float targetBearing = calculateBearing(location); + createBearingAnimator(mapAnimation, targetBearing); + mapAnimation.build().playTogether(); + } + + private void buildTrackingGPSNorthAnimation(Location location) { + MapAnimator.Builder mapAnimation = MapAnimator.builder(mapboxMap); + addLatLngAnimator(location, mapAnimation); + + double bearing = mapboxMap.getCameraPosition().bearing; + if (bearing == 0) { + mapAnimation.build().playTogether(); + return; + } + + float targetBearing = Utils.shortestRotation(0, (float) bearing); + createBearingAnimator(mapAnimation, targetBearing); + mapAnimation.build().playTogether(); + } + + private void addLatLngAnimator(Location location, MapAnimator.Builder mapAnimation) { + LatLng target = new LatLng(location); + LatLngAnimator latLngAnimator = new LatLngAnimator(target, 1000); + latLngAnimator.setInterpolator(new LinearInterpolator()); + mapAnimation.addLatLngAnimator(latLngAnimator); + } + + private void createBearingAnimator(MapAnimator.Builder mapAnimation, float targetBearing) { + BearingAnimator bearingAnimator = new BearingAnimator(targetBearing, 1000); + bearingAnimator.setInterpolator(new LinearInterpolator()); + mapAnimation.addBearingAnimator(bearingAnimator); + } + + private float calculateBearing(Location location) { + if (location.hasBearing() && bearing > 0) { + float bearing = Utils.shortestRotation(location.getBearing(), this.bearing); + this.bearing = bearing; + return bearing; + } else { + this.bearing = location.getBearing(); + return this.bearing; + } + } +} From 68db48f20cc0c4a0fef1ca8b76c8cec9204fbffc Mon Sep 17 00:00:00 2001 From: danesfeder Date: Tue, 20 Feb 2018 14:16:20 +0100 Subject: [PATCH 2/3] Fix broken tests --- .../mapboxsdk/plugins/locationlayer/LocationLayerTest.java | 2 +- .../mapboxsdk/plugins/places/autocomplete/data/TestData.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 b99877cd3..63f20b92f 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 @@ -140,7 +140,7 @@ public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, Mapbo UiController uiController, Context context) { locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.NORMAL); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); - locationLayerPlugin.setLocationLayerEnabled(LocationLayerStyle.NONE); + locationLayerPlugin.setLocationLayerEnabled(false); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER).getVisibility().getValue() .equals(Property.NONE)); } 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 { From 352fca2e3ed18f41df7a6a0cb58890a31a12f976 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Tue, 20 Feb 2018 20:11:51 +0100 Subject: [PATCH 3/3] API tweaks and example update --- .../locationlayer/LocationLayerTest.java | 18 +-- .../location/CompassListenerActivity.java | 4 +- .../LocationLayerMapChangeActivity.java | 4 +- .../location/LocationLayerModesActivity.java | 103 +++++++++++++----- .../ManualLocationUpdatesActivity.java | 4 +- .../layout/activity_location_layer_mode.xml | 46 ++++---- app/src/main/res/values/strings.xml | 2 + ...LayerStyle.java => LocationLayerMode.java} | 10 +- .../locationlayer/LocationLayerPlugin.java | 96 ++++++++-------- .../locationlayer/LocationLayerTracking.java | 2 +- .../camera/LocationLayerCamera.java | 2 +- 11 files changed, 176 insertions(+), 115 deletions(-) rename plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/{LocationLayerStyle.java => LocationLayerMode.java} (83%) 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 63f20b92f..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.setLocationLayerStyle(LocationLayerStyle.NORMAL); + 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.setLocationLayerStyle(LocationLayerStyle.NORMAL); + 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.setLocationLayerStyle(LocationLayerStyle.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.setLocationLayerStyle(LocationLayerStyle.COMPASS); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.COMPASS); assertTrue(mapboxMap.getLayer(NAVIGATION_LAYER) != null); } }); @@ -138,7 +138,7 @@ public void locationLayerModeCorrectlySetToNone() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.NORMAL); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); locationLayerPlugin.setLocationLayerEnabled(false); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER).getVisibility().getValue() @@ -153,11 +153,11 @@ public void onMapChangeLocationLayerRedrawn() throws Exception { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController, Context context) { - locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.NORMAL); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); mapboxMap.setStyleUrl(Style.SATELLITE); uiController.loopMainThreadForAtLeast(500); - assertEquals(locationLayerPlugin.getLocationLayerStyle(), LocationLayerStyle.NORMAL); + 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.setLocationLayerStyle(LocationLayerStyle.NORMAL); + 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.setLocationLayerStyle(LocationLayerStyle.NORMAL); + locationLayerPlugin.setLocationLayerMode(LocationLayerMode.NORMAL); locationLayerPlugin.applyStyle(LocationLayerOptions.builder(context).staleStateDelay(100).build()); locationLayerPlugin.forceLocationUpdate(location); uiController.loopMainThreadForAtLeast(200); 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 a78631153..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 @@ -9,7 +9,7 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mapbox.mapboxsdk.plugins.locationlayer.CompassListener; -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerStyle; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerMode; import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin; import com.mapbox.mapboxsdk.plugins.testapp.R; import com.mapbox.services.android.location.LostLocationEngine; @@ -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.setLocationLayerStyle(LocationLayerStyle.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 3f762841b..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 @@ -9,7 +9,7 @@ import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerStyle; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerMode; import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin; import com.mapbox.mapboxsdk.plugins.testapp.R; import com.mapbox.mapboxsdk.plugins.testapp.Utils; @@ -52,7 +52,7 @@ public void onMapReady(MapboxMap mapboxMap) { locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); locationEngine.activate(); locationPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine); - locationPlugin.setLocationLayerStyle(LocationLayerStyle.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 f6731c0ed..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 @@ -5,9 +5,13 @@ 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; @@ -15,7 +19,7 @@ import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerStyle; +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; @@ -25,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; @@ -34,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; @@ -51,39 +66,20 @@ 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(false); + showModeListDialog(); } - @OnClick(R.id.button_location_mode_compass) + @OnClick(R.id.button_location_tracking) public void locationModeCompass(View view) { if (locationLayerPlugin == null) { return; } - locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.COMPASS); - locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING_COMPASS); - } - - @OnClick(R.id.button_location_mode_tracking) - public void locationModeTracking(View view) { - if (locationLayerPlugin == null) { - return; - } - locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.NORMAL); - locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING); - } - - @OnClick(R.id.button_location_mode_navigation) - public void locationModeNavigation(View view) { - if (locationLayerPlugin == null) { - return; - } - locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.NAVIGATION); - locationLayerPlugin.setLocationLayerTracking(LocationLayerTracking.TRACKING_GPS_NORTH); + showTrackingListDialog(); } @SuppressLint("MissingPermission") @@ -195,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 275365afb..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 @@ -10,7 +10,7 @@ import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerStyle; +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerMode; import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin; import com.mapbox.mapboxsdk.plugins.testapp.R; import com.mapbox.mapboxsdk.plugins.testapp.Utils; @@ -71,7 +71,7 @@ public void onMapReady(MapboxMap mapboxMap) { locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); locationEngine.activate(); locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, null); - locationLayerPlugin.setLocationLayerStyle(LocationLayerStyle.NORMAL); + 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"> -