From 9beb6629b68f2c07ff3f778d9bf4acfbcb3b6598 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Wed, 3 Feb 2016 14:51:22 +0100 Subject: [PATCH] [android] #3752 - added scroll by x,y feature, introduced new CameraUpdateType, introduced caching system for invalidating CameraPosition, general cleanup, readded getLatLng in MapView, .. [android] #3752 - correctly invalidating cameraposition --- .../mapboxsdk/camera/CameraPosition.java | 40 ++++- .../mapboxsdk/maps/CameraUpdateFactory.java | 68 ++++--- .../com/mapbox/mapboxsdk/maps/MapView.java | 15 +- .../com/mapbox/mapboxsdk/maps/MapboxMap.java | 36 +++- .../src/main/AndroidManifest.xml | 6 +- .../mapboxsdk/testapp/MainActivity.java | 4 + .../mapboxsdk/testapp/ScrollByActivity.java | 166 ++++++++++++++++++ .../src/main/res/drawable/ic_input_24dp.xml | 9 + .../main/res/layout/activity_scroll_by.xml | 99 +++++++++++ .../src/main/res/menu/menu_drawer.xml | 17 +- .../src/main/res/values/strings.xml | 4 + 11 files changed, 422 insertions(+), 42 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/ScrollByActivity.java create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable/ic_input_24dp.xml create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_scroll_by.xml diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java index 1b7bcba32..004e7f5f8 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java @@ -80,6 +80,11 @@ public final class CameraPosition implements Parcelable { out.writeFloat(zoom); } + @Override + public String toString() { + return "Target: " + target + ", Zoom:" + zoom + ", Bearing:" + bearing + ", Tilt:" + tilt; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -119,6 +124,7 @@ public final class CameraPosition implements Parcelable { private LatLng target = null; private float tilt = -1; private float zoom = -1; + private boolean isRadiant; /** * Creates an empty builder. @@ -127,6 +133,15 @@ public final class CameraPosition implements Parcelable { super(); } + /** + * Creates a builder for building CameraPosition objects using radiants. + * + * @param isRadiant + */ + public Builder(boolean isRadiant) { + this.isRadiant = isRadiant; + } + /** * Create Builder with an existing CameraPosition data. * @@ -147,13 +162,13 @@ public final class CameraPosition implements Parcelable { * * @param typedArray TypedArray containgin attribute values */ - public Builder(TypedArray typedArray){ + public Builder(TypedArray typedArray) { super(); - if(typedArray!=null) { + if (typedArray != null) { this.bearing = typedArray.getFloat(R.styleable.MapView_direction, 0.0f); double lat = typedArray.getFloat(R.styleable.MapView_center_latitude, 0.0f); double lng = typedArray.getFloat(R.styleable.MapView_center_longitude, 0.0f); - this.target= new LatLng(lat, lng); + this.target = new LatLng(lat, lng); this.tilt = typedArray.getFloat(R.styleable.MapView_tilt, 0.0f); this.zoom = typedArray.getFloat(R.styleable.MapView_zoom, 0.0f); } @@ -174,15 +189,14 @@ public final class CameraPosition implements Parcelable { } } - /** * Create Builder from an existing CameraPositionUpdate update. * * @param update Update containing camera options */ - public Builder(CameraUpdateFactory.ZoomUpdate update){ + public Builder(CameraUpdateFactory.ZoomUpdate update) { super(); - if(update!=null){ + if (update != null) { this.zoom = update.getZoom(); } } @@ -194,7 +208,12 @@ public final class CameraPosition implements Parcelable { * @return Builder */ public Builder bearing(float bearing) { - this.bearing = (float) (-bearing * MathConstants.DEG2RAD); + if(isRadiant){ + this.bearing = bearing; + }else{ + // converting degrees to radiant + this.bearing = (float) (-bearing * MathConstants.DEG2RAD); + } return this; } @@ -226,7 +245,12 @@ public final class CameraPosition implements Parcelable { */ @FloatRange(from = 0.0, to = 60.0) public Builder tilt(float tilt) { - this.tilt = (float) (MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT) * MathConstants.DEG2RAD); + if(isRadiant){ + this.tilt = tilt; + }else { + // converting degrees to radiant + this.tilt = (float) (MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT) * MathConstants.DEG2RAD); + } return this; } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/CameraUpdateFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/CameraUpdateFactory.java index 38b340908..81b455607 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/CameraUpdateFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/CameraUpdateFactory.java @@ -69,22 +69,6 @@ public class CameraUpdateFactory { return new CameraBoundsUpdate(bounds, paddingLeft, paddingTop, paddingRight, paddingBottom); } -// /** -// * Returns a CameraUpdate that transforms the camera such that the specified latitude/longitude -// * bounds are centered on screen within a bounding box of specified dimensions at the greatest -// * possible zoom level. You can specify additional padding, to further restrict the size of -// * the bounding box. The returned CameraUpdate has a bearing of 0 and a tilt of 0. -// * -// * @param bounds -// * @param width -// * @param height -// * @param padding -// * @return -// */ -// public static CameraUpdate newLatLngBounds(@NonNull LatLngBounds bounds, int width, int height, int padding) { -// throw new UnsupportedOperationException("Not implemented yet"); -// } - /** * Returns a CameraUpdate that moves the center of the screen to a latitude and longitude specified by a LatLng object, and moves to the given zoom level. * @@ -96,16 +80,16 @@ public class CameraUpdateFactory { return new CameraPositionUpdate(-1, latLng, -1, zoom); } -// /** -// * Returns a CameraUpdate that scrolls the camera over the map, shifting the center of view by the specified number of pixels in the x and y directions. -// * -// * @param xPixel -// * @param yPixel -// * @return -// */ -// public static CameraUpdate scrollBy(float xPixel, float yPixel) { -// throw new UnsupportedOperationException("Not implemented yet"); -// } + /** + * Returns a CameraUpdate that scrolls the camera over the map, shifting the center of view by the specified number of pixels in the x and y directions. + * + * @param xPixel + * @param yPixel + * @return + */ + public static CameraUpdate scrollBy(float xPixel, float yPixel) { + return new CameraMoveUpdate(xPixel, yPixel); + } /** * Returns a CameraUpdate that shifts the zoom level of the current camera viewpoint. @@ -267,6 +251,38 @@ public class CameraUpdateFactory { } } + public static class CameraMoveUpdate implements CameraUpdate { + + private float x; + private float y; + + public CameraMoveUpdate(float x, float y) { + this.x = x; + this.y = y; + } + + @Override + public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { + MapView mapView = mapboxMap.getMapView(); + + // Calculate the new center point + float viewPortWidth = mapView.getWidth(); + float viewPortHeight = mapView.getHeight(); + PointF targetPoint = new PointF(viewPortWidth / 2 + x, viewPortHeight / 2 + y); + + // Convert point to LatLng + LatLng latLng = mapView.fromScreenLocation(targetPoint); + + CameraPosition cameraPosition = mapboxMap.getCameraPosition(); + return new CameraPosition.Builder() + .target(latLng) + .zoom(cameraPosition.zoom) + .tilt(cameraPosition.tilt) + .bearing(cameraPosition.bearing) + .build(); + } + } + public static class ZoomUpdate implements CameraUpdate { @IntDef({ZOOM_IN, ZOOM_OUT, ZOOM_BY, ZOOM_TO, ZOOM_TO_POINT}) 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 a9fe3a799..38db55ea5 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 @@ -160,10 +160,10 @@ public class MapView extends FrameLayout { } private void initialize(@NonNull Context context, @Nullable AttributeSet attrs) { + mOnMapChangedListener = new ArrayList<>(); mMapboxMap = new MapboxMap(this); mAnnotations = new ArrayList<>(); mIcons = new ArrayList<>(); - mOnMapChangedListener = new ArrayList<>(); View view = LayoutInflater.from(context).inflate(R.layout.mapview_internal, this); @@ -512,6 +512,19 @@ public class MapView extends FrameLayout { }); } + // + // LatLng / CenterCoordinate + // + + /** + * Gets the current LatLng in the center of the MapView + * + * @return The center in LatLng + */ + LatLng getLatLng() { + return mNativeMapView.getLatLng(); + } + // // Pitch / Tilt // 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 d1075454e..deebfbac6 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 @@ -40,6 +40,7 @@ public class MapboxMap { private MapView mMapView; private UiSettings mUiSettings; private CameraPosition mCameraPosition; + private boolean mInvalidCameraPosition; private String mStyleUrl; private List mSelectedMarkers; private List mInfoWindows; @@ -60,6 +61,7 @@ public class MapboxMap { MapboxMap(@NonNull MapView mapView) { mMapView = mapView; + mMapView.addOnMapChangedListener(new MapChangeCameraPositionListener()); mUiSettings = new UiSettings(mapView); mSelectedMarkers = new ArrayList<>(); mInfoWindows = new ArrayList<>(); @@ -89,6 +91,16 @@ public class MapboxMap { * @return The current position of the Camera. */ public final CameraPosition getCameraPosition() { + if (mInvalidCameraPosition) { + // Camera position has changed, need to regenerate position + mCameraPosition = new CameraPosition.Builder(true) + .bearing((float) mMapView.getBearing()) + .target(mMapView.getLatLng()) + .tilt((float) mMapView.getTilt()) + .zoom((float) mMapView.getZoom()) + .build(); + mInvalidCameraPosition = false; + } return mCameraPosition; } @@ -116,6 +128,17 @@ public class MapboxMap { mMapView.jumpTo(mCameraPosition.bearing, mCameraPosition.target, mCameraPosition.tilt, mCameraPosition.zoom); } + /** + * Ease the map according to the update with an animation over a specified duration, and calls an optional callback on completion. See CameraUpdateFactory for a set of updates. + * If getCameraPosition() is called during the animation, it will return the current location of the camera in flight. + * + * @param update The change that should be applied to the camera. + */ + @UiThread + public final void easeCamera(CameraUpdate update) { + easeCamera(update, MapboxConstants.ANIMATION_DURATION); + } + /** * Ease the map according to the update with an animation over a specified duration, and calls an optional callback on completion. See CameraUpdateFactory for a set of updates. * If getCameraPosition() is called during the animation, it will return the current location of the camera in flight. @@ -1036,7 +1059,7 @@ public class MapboxMap { mMapView.invalidateCustomLayers(); } - MapView getMapView(){ + MapView getMapView() { return mMapView; } @@ -1218,4 +1241,15 @@ public class MapboxMap { void onFinish(); } + private class MapChangeCameraPositionListener implements MapView.OnMapChangedListener { + @Override + public void onMapChanged(@MapView.MapChange int change) { + if (!mInvalidCameraPosition && (change == MapView.REGION_DID_CHANGE + || change == MapView.REGION_DID_CHANGE_ANIMATED + || change == MapView.REGION_WILL_CHANGE + || change == MapView.REGION_WILL_CHANGE_ANIMATED)) { + mInvalidCameraPosition = true; + } + } + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 26c271784..c39c462cf 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -23,7 +23,8 @@ - + + + diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_scroll_by.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_scroll_by.xml new file mode 100644 index 000000000..17e0f3572 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_scroll_by.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_drawer.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_drawer.xml index 67857c951..b008a4f63 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_drawer.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_drawer.xml @@ -76,7 +76,7 @@ android:id="@+id/action_mapboxmap" android:checkable="false" android:icon="@drawable/ic_mapboxmap" - android:title="@string/action_mapboxmap"/> + android:title="@string/action_mapboxmap" /> - + + + diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml index fd8fed476..773642f29 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml @@ -24,6 +24,7 @@ Directions Activity Geocoder Activity Double Map Activity + Scroll By Activity Main Activity Controls Main Activity Styles @@ -91,4 +92,7 @@ Zoom + X: %1$d + Y: %1$d +