diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera')
3 files changed, 615 insertions, 613 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 40e447debe..79045b68bb 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 @@ -16,304 +16,304 @@ import static com.mapbox.mapboxsdk.utils.MathUtils.convertNativeBearing; */ public final class CameraPosition implements Parcelable { - public static final CameraPosition DEFAULT = new CameraPosition(new LatLng(), 0, 0, 0); - - public static final Parcelable.Creator<CameraPosition> CREATOR - = new Parcelable.Creator<CameraPosition>() { - public CameraPosition createFromParcel(Parcel in) { - double bearing = in.readDouble(); - LatLng target = in.readParcelable(LatLng.class.getClassLoader()); - double tilt = in.readDouble(); - double zoom = in.readDouble(); - return new CameraPosition(target, zoom, tilt, bearing); - } - - public CameraPosition[] newArray(int size) { - return new CameraPosition[size]; - } + public static final CameraPosition DEFAULT = new CameraPosition(new LatLng(), 0, 0, 0); + + public static final Parcelable.Creator<CameraPosition> CREATOR = + new Parcelable.Creator<CameraPosition>() { + public CameraPosition createFromParcel(Parcel in) { + double bearing = in.readDouble(); + LatLng target = in.readParcelable(LatLng.class.getClassLoader()); + double tilt = in.readDouble(); + double zoom = in.readDouble(); + return new CameraPosition(target, zoom, tilt, bearing); + } + + public CameraPosition[] newArray(int size) { + return new CameraPosition[size]; + } }; + /** + * Direction that the camera is pointing in, in degrees clockwise from north. + */ + public final double bearing; + + /** + * The location that the camera is pointing at. + */ + public final LatLng target; + + /** + * 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. + */ + public final double tilt; + + /** + * Zoom level near the center of the screen. See zoom(float) for the definition of the camera's + * zoom level. + */ + public final double zoom; + + /** + * 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. + * @throws NullPointerException if target is null + * @throws IllegalArgumentException if tilt is outside the range of 0 to 90 degrees inclusive. + */ + CameraPosition(LatLng target, double zoom, double tilt, double bearing) { + this.target = target; + this.bearing = bearing; + this.tilt = tilt; + this.zoom = zoom; + } + + /** + * Describe the kinds of special objects contained in this Parcelable's + * marshalled representation. + * + * @return integer 0. + */ + @Override + public int describeContents() { + return 0; + } + + /** + * Flatten this object in to a Parcel. + * + * @param out The Parcel in which the object should be written. + * @param flags Additional flags about how the object should be written. May be 0 or + * {@link #PARCELABLE_WRITE_RETURN_VALUE}. + */ + @Override + public void writeToParcel(Parcel out, int flags) { + out.writeDouble(bearing); + out.writeParcelable(target, flags); + out.writeDouble(tilt); + out.writeDouble(zoom); + } + + /** + * Returns a String with the camera target, zoom, bearing and tilt. + * + * @return A String with CameraPosition information. + */ + @Override + public String toString() { + return "Target: " + target + ", Zoom:" + zoom + ", Bearing:" + bearing + ", Tilt:" + tilt; + } + + /** + * Compares this {@link CameraPosition} object with another {@link CameraPosition} and + * determines if their target, zoom, tilt, and bearing match. + * + * @param o Another {@link CameraPosition} to compare with this object. + * @return True if target, zoom, tilt, and bearing match this {@link CameraPosition} object. + * Else, false. + */ + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + CameraPosition cameraPosition = (CameraPosition) o; + if (target != null && !target.equals(cameraPosition.target)) { + return false; + } else if (zoom != cameraPosition.zoom) { + return false; + } else if (tilt != cameraPosition.tilt) { + return false; + } else if (bearing != cameraPosition.bearing) { + return false; + } + return true; + } + + /** + * Gives an integer which can be used as the bucket number for storing elements of the set/map. + * This bucket number is the address of the element inside the set/map. There's no guarantee + * that this hash value will be consistent between different Java implementations, or even + * between different execution runs of the same program. + * + * @return integer value you can use for storing element. + */ + @Override + public int hashCode() { + int result = 1; + result = 31 * result + (target != null ? target.hashCode() : 0); + return result; + } + + /** + * Builder for composing {@link CameraPosition} objects. + */ + public static final class Builder { + + private double bearing = -1; + private LatLng target = null; + private double tilt = -1; + private double zoom = -1; + /** - * Direction that the camera is pointing in, in degrees clockwise from north. + * Creates an empty builder. */ - public final double bearing; + public Builder() { + super(); + } /** - * The location that the camera is pointing at. + * Create Builder with an existing CameraPosition data. + * + * @param previous Existing CameraPosition values to use */ - public final LatLng target; + public Builder(CameraPosition previous) { + super(); + if (previous != null) { + this.bearing = previous.bearing; + this.target = previous.target; + this.tilt = previous.tilt; + this.zoom = previous.zoom; + } + } /** - * 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. + * Create Builder with an existing CameraPosition data. + * + * @param typedArray TypedArray containgin attribute values */ - public final double tilt; + public Builder(TypedArray typedArray) { + super(); + if (typedArray != null) { + this.bearing = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraBearing, 0.0f); + double lat = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTargetLat, 0.0f); + double lng = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTargetLng, 0.0f); + this.target = new LatLng(lat, lng); + this.tilt = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTilt, 0.0f); + this.zoom = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraZoom, 0.0f); + } + } /** - * Zoom level near the center of the screen. See zoom(float) for the definition of the camera's - * zoom level. + * Create Builder from an existing CameraPositionUpdate update. + * + * @param update Update containing camera options */ - public final double zoom; + public Builder(CameraUpdateFactory.CameraPositionUpdate update) { + super(); + if (update != null) { + bearing = update.getBearing(); + target = update.getTarget(); + tilt = update.getTilt(); + zoom = update.getZoom(); + } + } /** - * Constructs a CameraPosition. + * Create Builder from an existing CameraPositionUpdate update. * - * @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. - * @throws NullPointerException if target is null - * @throws IllegalArgumentException if tilt is outside the range of 0 to 90 degrees inclusive. + * @param update Update containing camera options */ - CameraPosition(LatLng target, double zoom, double tilt, double bearing) { - this.target = target; - this.bearing = bearing; - this.tilt = tilt; - this.zoom = zoom; + public Builder(CameraUpdateFactory.ZoomUpdate update) { + super(); + if (update != null) { + this.zoom = update.getZoom(); + } } /** - * Describe the kinds of special objects contained in this Parcelable's - * marshalled representation. + * Create Builder from an existing array of doubles. + * <p> + * These values conform to map.ccp representation of a camera position. + * </p> * - * @return integer 0. + * @param nativeCameraValues Values containing target, bearing, tilt and zoom */ - @Override - public int describeContents() { - return 0; + public Builder(double[] nativeCameraValues) { + super(); + if (nativeCameraValues != null && nativeCameraValues.length == 5) { + target(new LatLng(nativeCameraValues[0], nativeCameraValues[1])); + bearing(convertNativeBearing(nativeCameraValues[2])); + tilt(nativeCameraValues[3]); + zoom(nativeCameraValues[4]); + } } /** - * Flatten this object in to a Parcel. + * Sets the direction that the camera is pointing in, in degrees clockwise from north. * - * @param out The Parcel in which the object should be written. - * @param flags Additional flags about how the object should be written. May be 0 or - * {@link #PARCELABLE_WRITE_RETURN_VALUE}. + * @param bearing Bearing + * @return Builder */ - @Override - public void writeToParcel(Parcel out, int flags) { - out.writeDouble(bearing); - out.writeParcelable(target, flags); - out.writeDouble(tilt); - out.writeDouble(zoom); + public Builder bearing(double bearing) { + double direction = bearing; + + while (direction >= 360) { + direction -= 360; + } + while (direction < 0) { + direction += 360; + } + + this.bearing = direction; + return this; } /** - * Returns a String with the camera target, zoom, bearing and tilt. + * Builds a CameraPosition. * - * @return A String with CameraPosition information. + * @return CameraPosition */ - @Override - public String toString() { - return "Target: " + target + ", Zoom:" + zoom + ", Bearing:" + bearing + ", Tilt:" + tilt; + public CameraPosition build() { + return new CameraPosition(target, zoom, tilt, bearing); } /** - * Compares this {@link CameraPosition} object with another {@link CameraPosition} and - * determines if their target, zoom, tilt, and bearing match. + * Sets the location that the camera is pointing at. * - * @param o Another {@link CameraPosition} to compare with this object. - * @return True if target, zoom, tilt, and bearing match this {@link CameraPosition} object. - * Else, false. + * @param location Location + * @return Builder */ - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - - if (o == null || getClass() != o.getClass()) { - return false; - } - - CameraPosition cameraPosition = (CameraPosition) o; - if (target != null && !target.equals(cameraPosition.target)) { - return false; - } else if (zoom != cameraPosition.zoom) { - return false; - } else if (tilt != cameraPosition.tilt) { - return false; - } else if (bearing != cameraPosition.bearing) { - return false; - } - return true; + public Builder target(LatLng location) { + this.target = location; + return this; } /** - * Gives an integer which can be used as the bucket number for storing elements of the set/map. - * This bucket number is the address of the element inside the set/map. There's no guarantee - * that this hash value will be consistent between different Java implementations, or even - * between different execution runs of the same program. + * Set the tilt in degrees + * <p> + * value is clamped to 0 and 60. + * <p/> * - * @return integer value you can use for storing element. + * @param tilt Tilt value + * @return Builder */ - @Override - public int hashCode() { - int result = 1; - result = 31 * result + (target != null ? target.hashCode() : 0); - return result; + public Builder tilt(double tilt) { + this.tilt = MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT); + return this; } /** - * Builder for composing {@link CameraPosition} objects. + * Set the zoom + * + * @param zoom Zoom value + * @return Builder */ - public static final class Builder { - - private double bearing = -1; - private LatLng target = null; - private double tilt = -1; - private double zoom = -1; - - /** - * Creates an empty builder. - */ - public Builder() { - super(); - } - - /** - * Create Builder with an existing CameraPosition data. - * - * @param previous Existing CameraPosition values to use - */ - public Builder(CameraPosition previous) { - super(); - if (previous != null) { - this.bearing = previous.bearing; - this.target = previous.target; - this.tilt = previous.tilt; - this.zoom = previous.zoom; - } - } - - /** - * Create Builder with an existing CameraPosition data. - * - * @param typedArray TypedArray containgin attribute values - */ - public Builder(TypedArray typedArray) { - super(); - if (typedArray != null) { - this.bearing = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraBearing, 0.0f); - double lat = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTargetLat, 0.0f); - double lng = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTargetLng, 0.0f); - this.target = new LatLng(lat, lng); - this.tilt = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTilt, 0.0f); - this.zoom = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraZoom, 0.0f); - } - } - - /** - * Create Builder from an existing CameraPositionUpdate update. - * - * @param update Update containing camera options - */ - public Builder(CameraUpdateFactory.CameraPositionUpdate update) { - super(); - if (update != null) { - bearing = update.getBearing(); - target = update.getTarget(); - tilt = update.getTilt(); - zoom = update.getZoom(); - } - } - - /** - * Create Builder from an existing CameraPositionUpdate update. - * - * @param update Update containing camera options - */ - public Builder(CameraUpdateFactory.ZoomUpdate update) { - super(); - if (update != null) { - this.zoom = update.getZoom(); - } - } - - /** - * Create Builder from an existing array of doubles. - * <p> - * These values conform to map.ccp representation of a camera position. - * </p> - * - * @param nativeCameraValues Values containing target, bearing, tilt and zoom - */ - public Builder(double[] nativeCameraValues) { - super(); - if (nativeCameraValues != null && nativeCameraValues.length == 5) { - target(new LatLng(nativeCameraValues[0], nativeCameraValues[1])); - bearing(convertNativeBearing(nativeCameraValues[2])); - tilt(nativeCameraValues[3]); - zoom(nativeCameraValues[4]); - } - } - - /** - * Sets the direction that the camera is pointing in, in degrees clockwise from north. - * - * @param bearing Bearing - * @return Builder - */ - public Builder bearing(double bearing) { - double direction = bearing; - - while (direction >= 360) { - direction -= 360; - } - while (direction < 0) { - direction += 360; - } - - this.bearing = direction; - return this; - } - - /** - * Builds a CameraPosition. - * - * @return CameraPosition - */ - public CameraPosition build() { - return new CameraPosition(target, zoom, tilt, bearing); - } - - /** - * Sets the location that the camera is pointing at. - * - * @param location Location - * @return Builder - */ - public Builder target(LatLng location) { - this.target = location; - return this; - } - - /** - * Set the tilt in degrees - * <p> - * value is clamped to 0 and 60. - * <p/> - * - * @param tilt Tilt value - * @return Builder - */ - public Builder tilt(double tilt) { - this.tilt = MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT); - return this; - } - - /** - * Set the zoom - * - * @param zoom Zoom value - * @return Builder - */ - public Builder zoom(double zoom) { - this.zoom = zoom; - return this; - } + public Builder zoom(double zoom) { + this.zoom = zoom; + return this; } + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java index 94ee4912ce..7e0dbf08fb 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java @@ -9,6 +9,6 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; */ public interface CameraUpdate { - CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap); + CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap); } 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 d568b9637d..aecc51530b 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 @@ -21,395 +21,397 @@ import java.lang.annotation.RetentionPolicy; */ public final class CameraUpdateFactory { - /** - * Returns a CameraUpdate that moves the camera to a specified CameraPosition. - * - * @param cameraPosition Camera Position to change to - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate newCameraPosition(@NonNull CameraPosition cameraPosition) { - return new CameraPositionUpdate(cameraPosition.bearing, cameraPosition.target, cameraPosition.tilt, cameraPosition.zoom); + /** + * Returns a CameraUpdate that moves the camera to a specified CameraPosition. + * + * @param cameraPosition Camera Position to change to + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate newCameraPosition(@NonNull CameraPosition cameraPosition) { + return new CameraPositionUpdate(cameraPosition.bearing, cameraPosition.target, cameraPosition.tilt, + cameraPosition.zoom); + } + + /** + * Returns a CameraUpdate that moves the center of the screen to a latitude and longitude + * specified by a LatLng object. This centers the camera on the LatLng object. + * + * @param latLng Target location to change to + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate newLatLng(@NonNull LatLng latLng) { + return new CameraPositionUpdate(-1, latLng, -1, -1); + } + + /** + * Returns a {@link CameraUpdate} that transforms the camera such that the specified + * latitude/longitude bounds are centered on screen at the greatest possible zoom level. + * You can specify padding, in order to inset the bounding box from the map view's edges. + * The returned CameraUpdate has a bearing of 0 and a tilt of 0. + * + * @param bounds Bounds to match Camera position with + * @param padding Padding added to the bounds + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate newLatLngBounds(@NonNull LatLngBounds bounds, int padding) { + return newLatLngBounds(bounds, padding, padding, padding, padding); + } + + /** + * Returns a {@link CameraUpdate} that transforms the camera such that the specified + * latitude/longitude bounds are centered on screen at the greatest possible zoom level. + * You can specify padding, in order to inset the bounding box from the map view's edges. + * The returned CameraUpdate has a bearing of 0 and a tilt of 0. + * + * @param bounds Bounds to base the Camera position out of + * @param paddingLeft Padding left of the bounds + * @param paddingTop Padding top of the bounds + * @param paddingRight Padding right of the bounds + * @param paddingBottom Padding bottom of the bounds + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate newLatLngBounds(@NonNull LatLngBounds bounds, int paddingLeft, int paddingTop, + int paddingRight, int paddingBottom) { + return new CameraBoundsUpdate(bounds, paddingLeft, paddingTop, paddingRight, paddingBottom); + } + + /** + * 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. + * + * @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); + } + + /** + * 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 Amount of pixels to scroll to in x direction + * @param yPixel Amount of pixels to scroll to in y direction + * @return CameraUpdate Final Camera Position + */ + 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. + * + * @param amount Amount of zoom level to change with + * @param focus Focus point of zoom + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate zoomBy(double amount, Point focus) { + return new ZoomUpdate(amount, focus.x, focus.y); + } + + /** + * Returns a CameraUpdate that shifts the zoom level of the current camera viewpoint. + * + * @param amount Amount of zoom level to change with + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate zoomBy(double amount) { + return new ZoomUpdate(ZoomUpdate.ZOOM_BY, amount); + } + + /** + * Returns a CameraUpdate that zooms in on the map by moving the viewpoint's height closer to + * the Earth's surface. The zoom increment is 1.0. + * + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate zoomIn() { + return new ZoomUpdate(ZoomUpdate.ZOOM_IN); + } + + /** + * Returns a CameraUpdate that zooms out on the map by moving the viewpoint's height farther + * away from the Earth's surface. The zoom increment is -1.0. + * + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate zoomOut() { + return new ZoomUpdate(ZoomUpdate.ZOOM_OUT); + } + + /** + * Returns a CameraUpdate that moves the camera viewpoint to a particular zoom level. + * + * @param zoom Zoom level to change to + * @return CameraUpdate Final Camera Position + */ + public static CameraUpdate zoomTo(double zoom) { + return new ZoomUpdate(ZoomUpdate.ZOOM_TO, zoom); + } + + // + // CameraUpdate types + // + + static final class CameraPositionUpdate implements CameraUpdate { + + private final double bearing; + private final LatLng target; + private final double tilt; + private final double zoom; + + CameraPositionUpdate(double bearing, LatLng target, double tilt, double zoom) { + this.bearing = bearing; + this.target = target; + this.tilt = tilt; + this.zoom = zoom; } - /** - * Returns a CameraUpdate that moves the center of the screen to a latitude and longitude - * specified by a LatLng object. This centers the camera on the LatLng object. - * - * @param latLng Target location to change to - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate newLatLng(@NonNull LatLng latLng) { - return new CameraPositionUpdate(-1, latLng, -1, -1); + public LatLng getTarget() { + return target; } - /** - * Returns a {@link CameraUpdate} that transforms the camera such that the specified - * latitude/longitude bounds are centered on screen at the greatest possible zoom level. - * You can specify padding, in order to inset the bounding box from the map view's edges. - * The returned CameraUpdate has a bearing of 0 and a tilt of 0. - * - * @param bounds Bounds to match Camera position with - * @param padding Padding added to the bounds - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate newLatLngBounds(@NonNull LatLngBounds bounds, int padding) { - return newLatLngBounds(bounds, padding, padding, padding, padding); + public double getBearing() { + return bearing; } - /** - * Returns a {@link CameraUpdate} that transforms the camera such that the specified - * latitude/longitude bounds are centered on screen at the greatest possible zoom level. - * You can specify padding, in order to inset the bounding box from the map view's edges. - * The returned CameraUpdate has a bearing of 0 and a tilt of 0. - * - * @param bounds Bounds to base the Camera position out of - * @param paddingLeft Padding left of the bounds - * @param paddingTop Padding top of the bounds - * @param paddingRight Padding right of the bounds - * @param paddingBottom Padding bottom of the bounds - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate newLatLngBounds(@NonNull LatLngBounds bounds, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) { - return new CameraBoundsUpdate(bounds, paddingLeft, paddingTop, paddingRight, paddingBottom); + public double getTilt() { + return tilt; } - /** - * 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. - * - * @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); + public double getZoom() { + return 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 Amount of pixels to scroll to in x direction - * @param yPixel Amount of pixels to scroll to in y direction - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate scrollBy(float xPixel, float yPixel) { - return new CameraMoveUpdate(xPixel, yPixel); + @Override + public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { + CameraPosition previousPosition = mapboxMap.getCameraPosition(); + if (target == null) { + return new CameraPosition.Builder(this) + .target(previousPosition.target) + .build(); + } + return new CameraPosition.Builder(this).build(); } + } - /** - * Returns a CameraUpdate that shifts the zoom level of the current camera viewpoint. - * - * @param amount Amount of zoom level to change with - * @param focus Focus point of zoom - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate zoomBy(double amount, Point focus) { - return new ZoomUpdate(amount, focus.x, focus.y); + static final class CameraBoundsUpdate implements CameraUpdate { + + private LatLngBounds bounds; + private RectF padding; + + CameraBoundsUpdate(LatLngBounds bounds, RectF padding) { + this.bounds = bounds; + this.padding = padding; } - /** - * Returns a CameraUpdate that shifts the zoom level of the current camera viewpoint. - * - * @param amount Amount of zoom level to change with - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate zoomBy(double amount) { - return new ZoomUpdate(ZoomUpdate.ZOOM_BY, amount); + CameraBoundsUpdate(LatLngBounds bounds, int[] padding) { + this(bounds, new RectF(padding[0], padding[1], padding[2], padding[3])); } - /** - * Returns a CameraUpdate that zooms in on the map by moving the viewpoint's height closer to - * the Earth's surface. The zoom increment is 1.0. - * - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate zoomIn() { - return new ZoomUpdate(ZoomUpdate.ZOOM_IN); + CameraBoundsUpdate(LatLngBounds bounds, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) { + this(bounds, new int[] {paddingLeft, paddingTop, paddingRight, paddingBottom}); } - /** - * Returns a CameraUpdate that zooms out on the map by moving the viewpoint's height farther - * away from the Earth's surface. The zoom increment is -1.0. - * - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate zoomOut() { - return new ZoomUpdate(ZoomUpdate.ZOOM_OUT); + public LatLngBounds getBounds() { + return bounds; + } + + public RectF getPadding() { + return padding; + } + + @Override + public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { + // Get required objects + Projection projection = mapboxMap.getProjection(); + UiSettings uiSettings = mapboxMap.getUiSettings(); + + // calculate correct padding + int[] mapPadding = mapboxMap.getPadding(); + RectF latLngPadding = getPadding(); + RectF padding = new RectF(latLngPadding.left + mapPadding[0], + latLngPadding.top + mapPadding[1], + latLngPadding.right + mapPadding[2], + latLngPadding.bottom + mapPadding[3]); + + // Calculate the bounds of the possibly rotated shape with respect to the viewport + PointF nePixel = new PointF(-Float.MAX_VALUE, -Float.MAX_VALUE); + PointF swPixel = new PointF(Float.MAX_VALUE, Float.MAX_VALUE); + float viewportHeight = uiSettings.getHeight(); + for (LatLng latLng : getBounds().toLatLngs()) { + PointF pixel = projection.toScreenLocation(latLng); + swPixel.x = Math.min(swPixel.x, pixel.x); + nePixel.x = Math.max(nePixel.x, pixel.x); + swPixel.y = Math.min(swPixel.y, viewportHeight - pixel.y); + nePixel.y = Math.max(nePixel.y, viewportHeight - pixel.y); + } + + // Calculate width/height + float width = nePixel.x - swPixel.x; + float height = nePixel.y - swPixel.y; + + double zoom = 0; + float minScale = 1; + // Calculate the zoom level + if (padding != null) { + float scaleX = (uiSettings.getWidth() - padding.left - padding.right) / width; + float scaleY = (uiSettings.getHeight() - padding.top - padding.bottom) / height; + minScale = scaleX < scaleY ? scaleX : scaleY; + zoom = calculateZoom(mapboxMap, minScale); + zoom = MathUtils.clamp(zoom, mapboxMap.getMinZoomLevel(), mapboxMap.getMaxZoomLevel()); + } + + // Calculate the center point + PointF paddedNEPixel = new PointF(nePixel.x + padding.right / minScale, nePixel.y + padding.top / minScale); + PointF paddedSWPixel = new PointF(swPixel.x - padding.left / minScale, swPixel.y - padding.bottom / minScale); + PointF centerPixel = new PointF((paddedNEPixel.x + paddedSWPixel.x) / 2, (paddedNEPixel.y + paddedSWPixel.y) / 2); + centerPixel.y = viewportHeight - centerPixel.y; + LatLng center = projection.fromScreenLocation(centerPixel); + + return new CameraPosition.Builder() + .target(center) + .zoom(zoom) + .tilt(0) + .bearing(0) + .build(); } /** - * Returns a CameraUpdate that moves the camera viewpoint to a particular zoom level. + * Calculates a zoom level based on minimum scale and current scale from MapView * - * @param zoom Zoom level to change to - * @return CameraUpdate Final Camera Position + * @param minScale The minimum scale to calculate the zoom level. + * @return zoom level that fits the MapView. */ - public static CameraUpdate zoomTo(double zoom) { - return new ZoomUpdate(ZoomUpdate.ZOOM_TO, zoom); + public double calculateZoom(MapboxMap mapboxMap, float minScale) { + return Math.log(mapboxMap.getCameraPosition().zoom * minScale) / Math.log(2); + } + } + + static final class CameraMoveUpdate implements CameraUpdate { + + private float x; + private float y; + + CameraMoveUpdate(float x, float y) { + this.x = x; + this.y = y; + } + + @Override + public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { + UiSettings uiSettings = mapboxMap.getUiSettings(); + Projection projection = mapboxMap.getProjection(); + + // Calculate the new center point + float viewPortWidth = uiSettings.getWidth(); + float viewPortHeight = uiSettings.getHeight(); + PointF targetPoint = new PointF(viewPortWidth / 2 + x, viewPortHeight / 2 + y); + + // Convert point to LatLng + LatLng latLng = projection.fromScreenLocation(targetPoint); + + CameraPosition previousPosition = mapboxMap.getCameraPosition(); + return new CameraPosition.Builder() + .target(latLng != null ? latLng : previousPosition.target) + .zoom(previousPosition.zoom) + .tilt(previousPosition.tilt) + .bearing(previousPosition.bearing) + .build(); + } + } + + static final class ZoomUpdate implements CameraUpdate { + + @IntDef( {ZOOM_IN, ZOOM_OUT, ZOOM_BY, ZOOM_TO, ZOOM_TO_POINT}) + @Retention(RetentionPolicy.SOURCE) + @interface Type { + } + + static final int ZOOM_IN = 0; + static final int ZOOM_OUT = 1; + static final int ZOOM_BY = 2; + static final int ZOOM_TO = 3; + static final int ZOOM_TO_POINT = 4; + + @Type + private final int type; + private final double zoom; + private float x; + private float y; + + ZoomUpdate(@Type int type) { + this.type = type; + this.zoom = 0; + } + + ZoomUpdate(@Type int type, double zoom) { + this.type = type; + this.zoom = zoom; + } + + ZoomUpdate(double zoom, float x, float y) { + this.type = ZOOM_TO_POINT; + this.zoom = zoom; + this.x = x; + this.y = y; + } + + public double getZoom() { + return zoom; + } + + @Type + public int getType() { + return type; } - // - // CameraUpdate types - // - - static final class CameraPositionUpdate implements CameraUpdate { - - private final double bearing; - private final LatLng target; - private final double tilt; - private final double zoom; - - CameraPositionUpdate(double bearing, LatLng target, double tilt, double zoom) { - this.bearing = bearing; - this.target = target; - this.tilt = tilt; - this.zoom = zoom; - } - - public LatLng getTarget() { - return target; - } - - public double getBearing() { - return bearing; - } - - public double getTilt() { - return tilt; - } - - public double getZoom() { - return zoom; - } - - @Override - public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { - CameraPosition previousPosition = mapboxMap.getCameraPosition(); - if (target == null) { - return new CameraPosition.Builder(this) - .target(previousPosition.target) - .build(); - } - return new CameraPosition.Builder(this).build(); - } + public float getX() { + return x; } - static final class CameraBoundsUpdate implements CameraUpdate { - - private LatLngBounds bounds; - private RectF padding; - - CameraBoundsUpdate(LatLngBounds bounds, RectF padding) { - this.bounds = bounds; - this.padding = padding; - } - - CameraBoundsUpdate(LatLngBounds bounds, int[] padding) { - this(bounds, new RectF(padding[0], padding[1], padding[2], padding[3])); - } - - CameraBoundsUpdate(LatLngBounds bounds, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) { - this(bounds, new int[]{paddingLeft, paddingTop, paddingRight, paddingBottom}); - } - - public LatLngBounds getBounds() { - return bounds; - } - - public RectF getPadding() { - return padding; - } - - @Override - public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { - // Get required objects - Projection projection = mapboxMap.getProjection(); - UiSettings uiSettings = mapboxMap.getUiSettings(); - - // calculate correct padding - int[] mapPadding = mapboxMap.getPadding(); - RectF latLngPadding = getPadding(); - RectF padding = new RectF(latLngPadding.left + mapPadding[0], - latLngPadding.top + mapPadding[1], - latLngPadding.right + mapPadding[2], - latLngPadding.bottom + mapPadding[3]); - - // Calculate the bounds of the possibly rotated shape with respect to the viewport - PointF nePixel = new PointF(-Float.MAX_VALUE, -Float.MAX_VALUE); - PointF swPixel = new PointF(Float.MAX_VALUE, Float.MAX_VALUE); - float viewportHeight = uiSettings.getHeight(); - for (LatLng latLng : getBounds().toLatLngs()) { - PointF pixel = projection.toScreenLocation(latLng); - swPixel.x = Math.min(swPixel.x, pixel.x); - nePixel.x = Math.max(nePixel.x, pixel.x); - swPixel.y = Math.min(swPixel.y, viewportHeight - pixel.y); - nePixel.y = Math.max(nePixel.y, viewportHeight - pixel.y); - } - - // Calculate width/height - float width = nePixel.x - swPixel.x; - float height = nePixel.y - swPixel.y; - - double zoom = 0; - float minScale = 1; - // Calculate the zoom level - if (padding != null) { - float scaleX = (uiSettings.getWidth() - padding.left - padding.right) / width; - float scaleY = (uiSettings.getHeight() - padding.top - padding.bottom) / height; - minScale = scaleX < scaleY ? scaleX : scaleY; - zoom = calculateZoom(mapboxMap, minScale); - zoom = MathUtils.clamp(zoom, mapboxMap.getMinZoomLevel(), mapboxMap.getMaxZoomLevel()); - } - - // Calculate the center point - PointF paddedNEPixel = new PointF(nePixel.x + padding.right / minScale, nePixel.y + padding.top / minScale); - PointF paddedSWPixel = new PointF(swPixel.x - padding.left / minScale, swPixel.y - padding.bottom / minScale); - PointF centerPixel = new PointF((paddedNEPixel.x + paddedSWPixel.x) / 2, (paddedNEPixel.y + paddedSWPixel.y) / 2); - centerPixel.y = viewportHeight - centerPixel.y; - LatLng center = projection.fromScreenLocation(centerPixel); - - return new CameraPosition.Builder() - .target(center) - .zoom(zoom) - .tilt(0) - .bearing(0) - .build(); - } - - /** - * Calculates a zoom level based on minimum scale and current scale from MapView - * - * @param minScale The minimum scale to calculate the zoom level. - * @return zoom level that fits the MapView. - */ - public double calculateZoom(MapboxMap mapboxMap, float minScale) { - return Math.log(mapboxMap.getCameraPosition().zoom * minScale) / Math.log(2); - } + public float getY() { + return y; } - static final class CameraMoveUpdate implements CameraUpdate { - - private float x; - private float y; - - CameraMoveUpdate(float x, float y) { - this.x = x; - this.y = y; - } - - @Override - public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { - UiSettings uiSettings = mapboxMap.getUiSettings(); - Projection projection = mapboxMap.getProjection(); - - // Calculate the new center point - float viewPortWidth = uiSettings.getWidth(); - float viewPortHeight = uiSettings.getHeight(); - PointF targetPoint = new PointF(viewPortWidth / 2 + x, viewPortHeight / 2 + y); - - // Convert point to LatLng - LatLng latLng = projection.fromScreenLocation(targetPoint); - - CameraPosition previousPosition = mapboxMap.getCameraPosition(); - return new CameraPosition.Builder() - .target(latLng != null ? latLng : previousPosition.target) - .zoom(previousPosition.zoom) - .tilt(previousPosition.tilt) - .bearing(previousPosition.bearing) - .build(); - } + double transformZoom(double currentZoom) { + switch (getType()) { + case CameraUpdateFactory.ZoomUpdate.ZOOM_IN: + currentZoom++; + break; + case CameraUpdateFactory.ZoomUpdate.ZOOM_OUT: + currentZoom--; + if (currentZoom < 0) { + currentZoom = 0; + } + break; + case CameraUpdateFactory.ZoomUpdate.ZOOM_TO: + currentZoom = getZoom(); + break; + case CameraUpdateFactory.ZoomUpdate.ZOOM_BY: + currentZoom = currentZoom + getZoom(); + break; + case CameraUpdateFactory.ZoomUpdate.ZOOM_TO_POINT: + currentZoom = currentZoom + getZoom(); + break; + } + return currentZoom; } - static final class ZoomUpdate implements CameraUpdate { - - @IntDef({ZOOM_IN, ZOOM_OUT, ZOOM_BY, ZOOM_TO, ZOOM_TO_POINT}) - @Retention(RetentionPolicy.SOURCE) - @interface Type { - } - - static final int ZOOM_IN = 0; - static final int ZOOM_OUT = 1; - static final int ZOOM_BY = 2; - static final int ZOOM_TO = 3; - static final int ZOOM_TO_POINT = 4; - - @Type - private final int type; - private final double zoom; - private float x; - private float y; - - ZoomUpdate(@Type int type) { - this.type = type; - this.zoom = 0; - } - - ZoomUpdate(@Type int type, double zoom) { - this.type = type; - this.zoom = zoom; - } - - ZoomUpdate(double zoom, float x, float y) { - this.type = ZOOM_TO_POINT; - this.zoom = zoom; - this.x = x; - this.y = y; - } - - public double getZoom() { - return zoom; - } - - @Type - public int getType() { - return type; - } - - public float getX() { - return x; - } - - public float getY() { - return y; - } - - double transformZoom(double currentZoom) { - switch (getType()) { - case CameraUpdateFactory.ZoomUpdate.ZOOM_IN: - currentZoom++; - break; - case CameraUpdateFactory.ZoomUpdate.ZOOM_OUT: - currentZoom--; - if (currentZoom < 0) { - currentZoom = 0; - } - break; - case CameraUpdateFactory.ZoomUpdate.ZOOM_TO: - currentZoom = getZoom(); - break; - case CameraUpdateFactory.ZoomUpdate.ZOOM_BY: - currentZoom = currentZoom + getZoom(); - break; - case CameraUpdateFactory.ZoomUpdate.ZOOM_TO_POINT: - currentZoom = currentZoom + getZoom(); - break; - } - return currentZoom; - } - - @Override - public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { - CameraPosition cameraPosition = mapboxMap.getCameraPosition(); - if (getType() != CameraUpdateFactory.ZoomUpdate.ZOOM_TO_POINT) { - return new CameraPosition.Builder(cameraPosition) - .zoom(transformZoom(cameraPosition.zoom)) - .build(); - } else { - return new CameraPosition.Builder(cameraPosition) - .zoom(transformZoom(cameraPosition.zoom)) - .target(mapboxMap.getProjection().fromScreenLocation(new PointF(getX(), getY()))) - .build(); - } - } + @Override + public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { + CameraPosition cameraPosition = mapboxMap.getCameraPosition(); + if (getType() != CameraUpdateFactory.ZoomUpdate.ZOOM_TO_POINT) { + return new CameraPosition.Builder(cameraPosition) + .zoom(transformZoom(cameraPosition.zoom)) + .build(); + } else { + return new CameraPosition.Builder(cameraPosition) + .zoom(transformZoom(cameraPosition.zoom)) + .target(mapboxMap.getProjection().fromScreenLocation(new PointF(getX(), getY()))) + .build(); + } } + } } |