diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera')
2 files changed, 158 insertions, 19 deletions
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 f44c0f1904..e2341029ff 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 @@ -5,20 +5,23 @@ import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.FloatRange; import android.support.annotation.Keep; - import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.Size; + import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.utils.MathUtils; +import java.util.Arrays; + /** * Resembles the position, angle, zoom and tilt of the user's viewpoint. */ public final class CameraPosition implements Parcelable { - public static final CameraPosition DEFAULT = new CameraPosition(new LatLng(), 0, 0, 0); + public static final CameraPosition DEFAULT = new CameraPosition(new LatLng(), 0, 0, 0, new double[] {0, 0, 0, 0}); public static final Parcelable.Creator<CameraPosition> CREATOR = new Parcelable.Creator<CameraPosition>() { @@ -27,7 +30,9 @@ public final class CameraPosition implements Parcelable { LatLng target = in.readParcelable(LatLng.class.getClassLoader()); double tilt = in.readDouble(); double zoom = in.readDouble(); - return new CameraPosition(target, zoom, tilt, bearing); + double[] padding = new double[4]; + in.readDoubleArray(padding); + return new CameraPosition(target, zoom, tilt, bearing, padding); } public CameraPosition[] newArray(int size) { @@ -49,19 +54,26 @@ public final class CameraPosition implements Parcelable { /** * The angle, in degrees, of the camera angle from the nadir (directly facing the Earth). - * See tilt(float) for details of restrictions on the range of values. + * See {@link Builder#tilt(double)} for details of restrictions on the range of values. */ @Keep public final double tilt; /** - * Zoom level near the center of the screen. See zoom(float) for the definition of the camera's + * Zoom level near the center of the screen. See {@link Builder#zoom(double)} for the definition of the camera's * zoom level. */ @Keep public final double zoom; /** + * Padding in pixels. Specified in left, top, right, bottom order. + * See {@link Builder#padding(double[])} for the definition of the camera's padding. + */ + @Keep + public final double[] padding; + + /** * Constructs a CameraPosition. * * @param target The target location to align with the center of the screen. @@ -73,13 +85,34 @@ public final class CameraPosition implements Parcelable { * exclusive. * @throws NullPointerException if target is null * @throws IllegalArgumentException if tilt is outside the range of 0 to 90 degrees inclusive. + * @deprecated use {@link CameraPosition#CameraPosition(LatLng, double, double, double, double[])} instead. */ - @Keep + @Deprecated CameraPosition(LatLng target, double zoom, double tilt, double bearing) { + this(target, zoom, tilt, bearing, null); + } + + /** + * Constructs a CameraPosition. + * + * @param target The target location to align with the center of the screen. + * @param zoom Zoom level at target. See zoom(float) for details of restrictions. + * @param tilt The camera angle, in degrees, from the nadir (directly down). See tilt(float) + * for details of restrictions. + * @param bearing Direction that the camera is pointing in, in degrees clockwise from north. + * This value will be normalized to be within 0 degrees inclusive and 360 degrees + * exclusive. + * @param padding Padding in pixels. Specified in left, top, right, bottom order. + * @throws NullPointerException if target is null + * @throws IllegalArgumentException if tilt is outside the range of 0 to 90 degrees inclusive. + */ + @Keep + CameraPosition(LatLng target, double zoom, double tilt, double bearing, double[] padding) { this.target = target; this.bearing = bearing; this.tilt = tilt; this.zoom = zoom; + this.padding = padding; } /** @@ -106,6 +139,7 @@ public final class CameraPosition implements Parcelable { out.writeParcelable(target, flags); out.writeDouble(tilt); out.writeDouble(zoom); + out.writeDoubleArray(padding); } /** @@ -115,7 +149,8 @@ public final class CameraPosition implements Parcelable { */ @Override public String toString() { - return "Target: " + target + ", Zoom:" + zoom + ", Bearing:" + bearing + ", Tilt:" + tilt; + return "Target: " + target + ", Zoom:" + zoom + ", Bearing:" + bearing + ", Tilt:" + tilt + + ", Padding:" + Arrays.toString(padding); } /** @@ -145,6 +180,8 @@ public final class CameraPosition implements Parcelable { return false; } else if (bearing != cameraPosition.bearing) { return false; + } else if (!Arrays.equals(padding, cameraPosition.padding)) { + return false; } return true; } @@ -159,8 +196,16 @@ public final class CameraPosition implements Parcelable { */ @Override public int hashCode() { - int result = 1; + int result; + long temp; + temp = Double.doubleToLongBits(bearing); + result = (int) (temp ^ (temp >>> 32)); result = 31 * result + (target != null ? target.hashCode() : 0); + temp = Double.doubleToLongBits(tilt); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(zoom); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + result = 31 * result + Arrays.hashCode(padding); return result; } @@ -174,6 +219,7 @@ public final class CameraPosition implements Parcelable { private LatLng target = null; private double tilt = -1; private double zoom = -1; + private double[] padding = null; /** * Create an empty builder. @@ -194,6 +240,7 @@ public final class CameraPosition implements Parcelable { this.target = previous.target; this.tilt = previous.tilt; this.zoom = previous.zoom; + this.padding = previous.padding; } } @@ -226,6 +273,7 @@ public final class CameraPosition implements Parcelable { target = update.getTarget(); tilt = update.getTilt(); zoom = update.getZoom(); + padding = update.getPadding(); } } @@ -307,12 +355,43 @@ public final class CameraPosition implements Parcelable { } /** + * Padding in pixels that shifts the viewport by the specified amount. + * Applied padding is going to persist and impact following camera transformations. + * <p> + * Specified in left, top, right, bottom order. + * </p> + * + * @param padding Camera padding + * @return this + */ + @NonNull + public Builder padding(@Size(4) double[] padding) { + this.padding = padding; + return this; + } + + /** + * Padding in pixels that shifts the viewport by the specified amount. + * Applied padding is going to persist and impact following camera transformations. + * <p> + * Specified in left, top, right, bottom order. + * </p> + * + * @return this + */ + @NonNull + public Builder padding(double left, double top, double right, double bottom) { + this.padding = new double[] {left, top, right, bottom}; + return this; + } + + /** * Builds the CameraPosition. * * @return CameraPosition */ public CameraPosition build() { - return new CameraPosition(target, zoom, tilt, bearing); + return new CameraPosition(target, zoom, tilt, bearing, padding); } } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java index 16285468ee..b611456f43 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java @@ -5,6 +5,7 @@ import android.graphics.PointF; import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.Nullable; + import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.maps.MapboxMap; @@ -28,7 +29,7 @@ public final class CameraUpdateFactory { */ public static CameraUpdate newCameraPosition(@NonNull CameraPosition cameraPosition) { return new CameraPositionUpdate(cameraPosition.bearing, cameraPosition.target, cameraPosition.tilt, - cameraPosition.zoom); + cameraPosition.zoom, cameraPosition.padding); } /** @@ -39,7 +40,7 @@ public final class CameraUpdateFactory { * @return CameraUpdate Final Camera Position */ public static CameraUpdate newLatLng(@NonNull LatLng latLng) { - return new CameraPositionUpdate(-1, latLng, -1, -1); + return new CameraPositionUpdate(-1, latLng, -1, -1, null); } /** @@ -48,6 +49,7 @@ public final class CameraUpdateFactory { * current camera position bearing and tilt values. * <p> * You can specify padding, in order to inset the bounding box from the map view's edges. + * The padding will not persist and impact following camera transformations. * </p> * * @param bounds Bounds to match Camera position with @@ -64,6 +66,7 @@ public final class CameraUpdateFactory { * provided bearing and tilt values. * <p> * You can specify padding, in order to inset the bounding box from the map view's edges. + * The padding will not persist and impact following camera transformations. * </p> * * @param bounds Bounds to match Camera position with @@ -82,6 +85,7 @@ public final class CameraUpdateFactory { * current camera position bearing and tilt values. * <p> * You can specify padding, in order to inset the bounding box from the map view's edges. + * The padding will not persist and impact following camera transformations. * </p> * * @param bounds Bounds to base the Camera position out of @@ -102,6 +106,7 @@ public final class CameraUpdateFactory { * provided bearing and tilt values. * <p> * You can specify padding, in order to inset the bounding box from the map view's edges. + * The padding will not persist and impact following camera transformations. * </p> * * @param bounds Bounds to base the Camera position out of @@ -120,14 +125,31 @@ public final class CameraUpdateFactory { /** * 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. + * specified by a LatLng object taking the specified padding into account. * * @param latLng Target location to change to * @param zoom Zoom level to change to * @return CameraUpdate Final Camera Position */ public static CameraUpdate newLatLngZoom(@NonNull LatLng latLng, double zoom) { - return new CameraPositionUpdate(-1, latLng, -1, zoom); + return new CameraPositionUpdate(-1, latLng, -1, zoom, null); + } + + /** + * 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. + * Applied padding is going to persist and impact following camera transformations. + * + * @param latLng Target location to change to + * @param left Left padding + * @param top Top padding + * @param right Right padding + * @param bottom Bottom padding + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate newLatLngPadding(@NonNull LatLng latLng, + double left, double top, double right, double bottom) { + return new CameraPositionUpdate(-1, latLng, -1, -1, new double[] {left, top, right, bottom}); } /** @@ -188,7 +210,7 @@ public final class CameraUpdateFactory { * @return CameraUpdate Final Camera Position */ public static CameraUpdate bearingTo(double bearing) { - return new CameraPositionUpdate(bearing, null, -1, -1); + return new CameraPositionUpdate(bearing, null, -1, -1, null); } /** @@ -198,7 +220,34 @@ public final class CameraUpdateFactory { * @return CameraUpdate Final Camera Position */ public static CameraUpdate tiltTo(double tilt) { - return new CameraPositionUpdate(-1, null, tilt, -1); + return new CameraPositionUpdate(-1, null, tilt, -1, null); + } + + /** + * Returns a CameraUpdate that when animated changes the camera padding. + * Applied padding is going to persist and impact following camera transformations. + * <p> + * Specified in left, top, right, bottom order. + * </p> + * + * @param padding Padding to change to + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate paddingTo(double[] padding) { + return new CameraPositionUpdate(-1, null, -1, -1, padding); + } + + /** + * Returns a CameraUpdate that when animated changes the camera padding. + * Applied padding is going to persist and impact following camera transformations. + * <p> + * Specified in left, top, right, bottom order. + * </p> + * + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate paddingTo(double left, double top, double right, double bottom) { + return paddingTo(new double[] {left, top, right, bottom}); } // @@ -211,12 +260,14 @@ public final class CameraUpdateFactory { private final LatLng target; private final double tilt; private final double zoom; + private final double[] padding; - CameraPositionUpdate(double bearing, LatLng target, double tilt, double zoom) { + CameraPositionUpdate(double bearing, LatLng target, double tilt, double zoom, double[] padding) { this.bearing = bearing; this.target = target; this.tilt = tilt; this.zoom = zoom; + this.padding = padding; } public LatLng getTarget() { @@ -235,10 +286,14 @@ public final class CameraUpdateFactory { return zoom; } + public double[] getPadding() { + return padding; + } + @Override public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { - CameraPosition previousPosition = mapboxMap.getCameraPosition(); if (target == null) { + CameraPosition previousPosition = mapboxMap.getCameraPosition(); return new CameraPosition.Builder(this) .target(previousPosition.target) .build(); @@ -247,7 +302,7 @@ public final class CameraUpdateFactory { } @Override - public boolean equals(@Nullable Object o) { + public boolean equals(Object o) { if (this == o) { return true; } @@ -266,7 +321,10 @@ public final class CameraUpdateFactory { if (Double.compare(that.zoom, zoom) != 0) { return false; } - return target != null ? target.equals(that.target) : that.target == null; + if (target != null ? !target.equals(that.target) : that.target != null) { + return false; + } + return Arrays.equals(padding, that.padding); } @Override @@ -280,6 +338,7 @@ public final class CameraUpdateFactory { result = 31 * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(zoom); result = 31 * result + (int) (temp ^ (temp >>> 32)); + result = 31 * result + Arrays.hashCode(padding); return result; } @@ -290,6 +349,7 @@ public final class CameraUpdateFactory { + ", target=" + target + ", tilt=" + tilt + ", zoom=" + zoom + + ", padding=" + Arrays.toString(padding) + '}'; } } |