From 9ec01034316cd3f2184ca11fc4c4de5db93a031d Mon Sep 17 00:00:00 2001 From: Tobrun Date: Thu, 18 Feb 2016 16:25:19 +0100 Subject: [PATCH] [android] #4018 - migrated max/min zoom to UiSettings + added tests + update sample app --- .../com/mapbox/mapboxsdk/maps/MapView.java | 26 ++--- .../com/mapbox/mapboxsdk/maps/MapboxMap.java | 60 ----------- .../com/mapbox/mapboxsdk/maps/UiSettings.java | 99 ++++++++++++++++--- .../src/main/res/values/attrs.xml | 2 + .../mapboxsdk/testapp/MaxMinZoomActivity.java | 8 +- .../mapbox/mapboxsdk/maps/UiSettingsTest.java | 25 ++++- 6 files changed, 130 insertions(+), 90 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 1460b4b79..5bd0c5c87 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -235,13 +235,15 @@ public class MapView extends FrameLayout { mMapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); // Access token - if (typedArray.getString(R.styleable.MapView_access_token) != null) { + String accessToken = typedArray.getString(R.styleable.MapView_access_token); + if (accessToken != null) { setAccessToken(typedArray.getString(R.styleable.MapView_access_token)); } // Style url - if (typedArray.getString(R.styleable.MapView_style_url) != null) { - mMapboxMap.setStyleUrl(typedArray.getString(R.styleable.MapView_style_url)); + String styleUrl = typedArray.getString(R.styleable.MapView_style_url); + if (styleUrl != null) { + mMapboxMap.setStyleUrl(styleUrl); } // Enable gestures @@ -252,6 +254,10 @@ public class MapView extends FrameLayout { uiSettings.setTiltGesturesEnabled(typedArray.getBoolean(R.styleable.MapView_tilt_enabled, true)); uiSettings.setZoomControlsEnabled(typedArray.getBoolean(R.styleable.MapView_zoom_controls_enabled, false)); + // Zoom + uiSettings.setMaxZoom(typedArray.getFloat(R.styleable.MapView_zoom_max, (float) MapboxConstants.MAXIMUM_ZOOM)); + uiSettings.setMinZoom(typedArray.getFloat(R.styleable.MapView_zoom_min, (float) MapboxConstants.MINIMUM_ZOOM)); + // Compass uiSettings.setCompassEnabled(typedArray.getBoolean(R.styleable.MapView_compass_enabled, true)); uiSettings.setCompassGravity(typedArray.getInt(R.styleable.MapView_compass_gravity, Gravity.TOP | Gravity.END)); @@ -277,13 +283,7 @@ public class MapView extends FrameLayout { , (int) (typedArray.getDimension(R.styleable.MapView_attribution_margin_bottom, DIMENSION_SEVEN_DP) * mScreenDensity)); // User location - try { - //noinspection ResourceType - mMapboxMap.setMyLocationEnabled(typedArray.getBoolean(R.styleable.MapView_my_location_enabled, false)); - } catch (SecurityException ignore) { - // User did not accept location permissions - } - + mMapboxMap.setMyLocationEnabled(typedArray.getBoolean(R.styleable.MapView_my_location_enabled, false)); } finally { typedArray.recycle(); } @@ -732,7 +732,7 @@ public class MapView extends FrameLayout { * @return The minimum zoom level. */ @UiThread - public double getMinZoom() { + double getMinZoom() { return mNativeMapView.getMinZoom(); } @@ -744,7 +744,7 @@ public class MapView extends FrameLayout { * @param maxZoom The new maximum zoom level. */ @UiThread - public void setMaxZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double maxZoom) { + void setMaxZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double maxZoom) { mNativeMapView.setMaxZoom(maxZoom); } @@ -756,7 +756,7 @@ public class MapView extends FrameLayout { * @return The maximum zoom level. */ @UiThread - public double getMaxZoom() { + double getMaxZoom() { return mNativeMapView.getMaxZoom(); } 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 57a86814a..1c1f32fdc 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 @@ -302,66 +302,6 @@ public class MapboxMap { } } - // - // ZOOM - // - - /** - *

- * Sets the minimum zoom level the map can be displayed at. - *

- * - * @param minZoom The new minimum zoom level. - */ - @UiThread - public void setMinZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double minZoom) { - if ((minZoom < MapboxConstants.MINIMUM_ZOOM) || (minZoom > MapboxConstants.MAXIMUM_ZOOM)) { - Log.e(MapboxConstants.TAG, "Not setting minZoom, value is in unsupported range: " + minZoom); - return; - } - mMapView.setMinZoom(minZoom); - } - - /** - *

- * Gets the maximum zoom level the map can be displayed at. - *

- * - * @return The minimum zoom level. - */ - @UiThread - public double getMinZoom() { - return mMapView.getMinZoom(); - } - - /** - *

- * Sets the maximum zoom level the map can be displayed at. - *

- * - * @param maxZoom The new maximum zoom level. - */ - @UiThread - public void setMaxZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double maxZoom) { - if ((maxZoom < MapboxConstants.MINIMUM_ZOOM) || (maxZoom > MapboxConstants.MAXIMUM_ZOOM)) { - Log.e(MapboxConstants.TAG, "Not setting maxZoom, value is in unsupported range: " + maxZoom); - return; - } - mMapView.setMaxZoom(maxZoom); - } - - /** - *

- * Gets the maximum zoom level the map can be displayed at. - *

- * - * @return The maximum zoom level. - */ - @UiThread - public double getMaxZoom() { - return mMapView.getMaxZoom(); - } - // // Manual zoom controls // diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java index ede6217a4..aac31cd0f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java @@ -1,11 +1,15 @@ package com.mapbox.mapboxsdk.maps; +import android.support.annotation.FloatRange; import android.support.annotation.NonNull; import android.support.annotation.UiThread; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.VideoView; +import com.mapbox.mapboxsdk.constants.MapboxConstants; + /** * Settings for the user interface of a MapboxMap. To obtain this interface, call getUiSettings(). */ @@ -23,6 +27,9 @@ public class UiSettings { private boolean zoomControlsEnabled; private boolean scrollGesturesEnabled; + private double maxZoomLevel = -1; + private double minZoomLevel = -1; + UiSettings(@NonNull MapView mapView) { this.mapView = mapView; this.compassSettings = new ViewSettings(); @@ -30,6 +37,70 @@ public class UiSettings { this.attributionSettings = new ViewSettings(); } + /** + *

+ * Sets the minimum zoom level the map can be displayed at. + *

+ * + * @param minZoom The new minimum zoom level. + */ + @UiThread + public void setMinZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double minZoom) { + if ((minZoom < MapboxConstants.MINIMUM_ZOOM) || (minZoom > MapboxConstants.MAXIMUM_ZOOM)) { + Log.e(MapboxConstants.TAG, "Not setting minZoom, value is in unsupported range: " + minZoom); + return; + } + minZoomLevel = minZoom; + mapView.setMinZoom(minZoom); + } + + /** + *

+ * Gets the maximum zoom level the map can be displayed at. + *

+ * + * @return The minimum zoom level. + */ + @UiThread + public double getMinZoom() { + if (minZoomLevel == -1) { + return minZoomLevel = mapView.getMinZoom(); + } + return minZoomLevel; + } + + /** + *

+ * Sets the maximum zoom level the map can be displayed at. + *

+ * + * @param maxZoom The new maximum zoom level. + */ + @UiThread + public void setMaxZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double maxZoom) { + if ((maxZoom < MapboxConstants.MINIMUM_ZOOM) || (maxZoom > MapboxConstants.MAXIMUM_ZOOM)) { + Log.e(MapboxConstants.TAG, "Not setting maxZoom, value is in unsupported range: " + maxZoom); + return; + } + maxZoomLevel = maxZoom; + mapView.setMaxZoom(maxZoom); + } + + /** + *

+ * Gets the maximum zoom level the map can be displayed at. + *

+ * + * @return The maximum zoom level. + */ + @UiThread + public double getMaxZoom() { + if (maxZoomLevel == -1) { + return maxZoomLevel = mapView.getMaxZoom(); + } + return maxZoomLevel; + } + /** *

* Enables or disables the compass. The compass is an icon on the map that indicates the @@ -110,7 +181,7 @@ public class UiSettings { * @return The top margin in pixels */ public int getCompassMarginTop() { - return compassSettings.getMargins()[1]; + return compassSettings.getMargins()[1]; } /** @@ -119,7 +190,7 @@ public class UiSettings { * @return The right margin in pixels */ public int getCompassMarginRight() { - return compassSettings.getMargins()[2]; + return compassSettings.getMargins()[2]; } /** @@ -128,7 +199,7 @@ public class UiSettings { * @return The bottom margin in pixels */ public int getCompassMarginBottom() { - return compassSettings.getMargins()[3]; + return compassSettings.getMargins()[3]; } /** @@ -141,7 +212,7 @@ public class UiSettings { */ public void setLogoEnabled(boolean enabled) { logoSettings.setEnabled(enabled); - mapView.setLogoVisibility(enabled ); + mapView.setLogoVisibility(enabled); } /** @@ -196,7 +267,7 @@ public class UiSettings { * * @return The left margin in pixels */ - public int getLogoMarginLeft(){ + public int getLogoMarginLeft() { return logoSettings.getMargins()[0]; } @@ -205,7 +276,7 @@ public class UiSettings { * * @return The top margin in pixels */ - public int getLogoMarginTop(){ + public int getLogoMarginTop() { return logoSettings.getMargins()[1]; } @@ -214,7 +285,7 @@ public class UiSettings { * * @return The right margin in pixels */ - public int getLogoMarginRight(){ + public int getLogoMarginRight() { return logoSettings.getMargins()[2]; } @@ -223,7 +294,7 @@ public class UiSettings { * * @return The bottom margin in pixels */ - public int getLogoMarginBottom(){ + public int getLogoMarginBottom() { return logoSettings.getMargins()[3]; } @@ -292,7 +363,7 @@ public class UiSettings { * * @return The left margin in pixels */ - public int getAttributionMarginLeft(){ + public int getAttributionMarginLeft() { return attributionSettings.getMargins()[0]; } @@ -301,7 +372,7 @@ public class UiSettings { * * @return The top margin in pixels */ - public int getAttributionMarginTop(){ + public int getAttributionMarginTop() { return attributionSettings.getMargins()[1]; } @@ -310,7 +381,7 @@ public class UiSettings { * * @return The right margin in pixels */ - public int getAttributionMarginRight(){ + public int getAttributionMarginRight() { return attributionSettings.getMargins()[2]; } @@ -319,7 +390,7 @@ public class UiSettings { * * @return The bottom margin in pixels */ - public int getAttributionMarginBottom(){ + public int getAttributionMarginBottom() { return attributionSettings.getMargins()[3]; } @@ -471,9 +542,9 @@ public class UiSettings { setZoomGesturesEnabled(enabled); } - public void invalidate(){ + public void invalidate() { mapView.setLogoMargins(getLogoMarginLeft(), getLogoMarginTop(), getLogoMarginRight(), getLogoMarginBottom()); - mapView.setCompassMargins(getCompassMarginLeft(),getCompassMarginTop(),getCompassMarginRight(),getCompassMarginBottom()); + mapView.setCompassMargins(getCompassMarginLeft(), getCompassMarginTop(), getCompassMarginRight(), getCompassMarginBottom()); mapView.setAttributionMargins(getAttributionMarginLeft(), getAttributionMarginTop(), getAttributionMarginRight(), getAttributionMarginBottom()); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml b/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml index 63547247f..f516e98dd 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml +++ b/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml @@ -4,6 +4,8 @@ + + diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MaxMinZoomActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MaxMinZoomActivity.java index 3ace776e0..997a434dc 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MaxMinZoomActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MaxMinZoomActivity.java @@ -15,6 +15,7 @@ import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.maps.UiSettings; import com.mapbox.mapboxsdk.utils.ApiAccess; public class MaxMinZoomActivity extends AppCompatActivity { @@ -44,8 +45,11 @@ public class MaxMinZoomActivity extends AppCompatActivity { public void onMapReady(@NonNull final MapboxMap mapboxMap) { mMapboxMap = mapboxMap; mMapboxMap.setStyle(Style.SATELLITE_STREETS); - mMapboxMap.setMinZoom(3); - mMapboxMap.setMaxZoom(5); + + UiSettings uiSettings = mapboxMap.getUiSettings(); + uiSettings.setMinZoom(3); + uiSettings.setMaxZoom(5); + mMapboxMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-1.063510, 32.895425))); } }); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/maps/UiSettingsTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/maps/UiSettingsTest.java index 79ed1926d..cb9031c66 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/maps/UiSettingsTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/maps/UiSettingsTest.java @@ -21,6 +21,30 @@ public class UiSettingsTest { assertNotNull("uiSettings should not be null", uiSettings); } + @Test + public void testMinZoom() { + double zoom = 10; + UiSettings uiSettings = new UiSettings(mMapView); + uiSettings.setMinZoom(zoom); + assertEquals("MinZoom should match", zoom, uiSettings.getMinZoom(), 0); + } + + @Test + public void testMaxZoom() { + double zoom = 10; + UiSettings uiSettings = new UiSettings(mMapView); + uiSettings.setMaxZoom(zoom); + assertEquals("MaxZoom should match", zoom, uiSettings.getMaxZoom(), 0); + } + + @Test + public void testInitialZoomLevels() { + //we are mocking MapView we expect a value of 0 to be returned + UiSettings uiSettings = new UiSettings(mMapView); + assertEquals("MaxZoom should match", 0, uiSettings.getMaxZoom(), 0); + assertEquals("MinZoom should match", 0, uiSettings.getMinZoom(), 0); + } + @Test public void testCompassEnabled() { UiSettings uiSettings = new UiSettings(mMapView); @@ -204,7 +228,6 @@ public class UiSettingsTest { assertEquals("Scroll gesture should be disabled", false, uiSettings.isScrollGesturesEnabled()); } - @Test public void testInvalidate() { UiSettings uiSettings = new UiSettings(mMapView);