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 deleted file mode 100644 index 93a593073..000000000 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java +++ /dev/null @@ -1,415 +0,0 @@ -package com.mapbox.mapboxsdk.camera; - -import android.content.res.TypedArray; -import android.os.Parcel; -import android.os.Parcelable; - -import androidx.annotation.FloatRange; -import androidx.annotation.Keep; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.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, new double[] {0, 0, 0, 0}); - - public static final Parcelable.Creator CREATOR = - new Parcelable.Creator() { - public CameraPosition createFromParcel(Parcel in) { - double bearing = in.readDouble(); - LatLng target = in.readParcelable(LatLng.class.getClassLoader()); - double tilt = in.readDouble(); - double zoom = in.readDouble(); - - double[] padding = null; - int paddingSize = in.readInt(); - if (paddingSize > 0) { - padding = new double[paddingSize]; - for (int i = 0; i < paddingSize; i++) { - padding[i] = in.readDouble(); - } - } - - return new CameraPosition(target, zoom, tilt, bearing, padding); - } - - public CameraPosition[] newArray(int size) { - return new CameraPosition[size]; - } - }; - - /** - * Direction that the camera is pointing in, in degrees clockwise from north. - */ - @Keep - public final double bearing; - - /** - * The location that the camera is pointing at. - */ - @Keep - public final LatLng target; - - /** - * The angle, in degrees, of the camera angle from the nadir (directly facing the Earth). - * 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 {@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. - * @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. - * @deprecated use {@link CameraPosition#CameraPosition(LatLng, double, double, double, double[])} instead. - */ - @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; - } - - /** - * 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); - - if (padding != null) { - int length = padding.length; - out.writeInt(length); - for (double v : padding) { - out.writeDouble(v); - } - } else { - out.writeInt(-1); - } - } - - /** - * 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 - + ", Padding:" + Arrays.toString(padding); - } - - /** - * 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(@Nullable 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; - } else if (!Arrays.equals(padding, cameraPosition.padding)) { - 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; - 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; - } - - /** - * Builder for composing CameraPosition objects. - */ - public static final class Builder { - - private double bearing = -1; - @Nullable - private LatLng target = null; - private double tilt = -1; - private double zoom = -1; - private double[] padding = null; - - /** - * Create an empty builder. - */ - public Builder() { - super(); - } - - /** - * Create a builder with an existing CameraPosition data. - * - * @param previous Existing CameraPosition values to use - */ - public Builder(@Nullable CameraPosition previous) { - super(); - if (previous != null) { - this.bearing = previous.bearing; - this.target = previous.target; - this.tilt = previous.tilt; - this.zoom = previous.zoom; - this.padding = previous.padding; - } - } - - /** - * Create a builder with an existing CameraPosition data. - * - * @param typedArray TypedArray containing attribute values - */ - public Builder(@Nullable 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 a builder from an existing CameraPositionUpdate update. - * - * @param update Update containing camera options - */ - public Builder(@Nullable CameraUpdateFactory.CameraPositionUpdate update) { - super(); - if (update != null) { - bearing = update.getBearing(); - target = update.getTarget(); - tilt = update.getTilt(); - zoom = update.getZoom(); - padding = update.getPadding(); - } - } - - /** - * Create builder from an existing CameraPositionUpdate update. - * - * @param update Update containing camera options - */ - public Builder(@Nullable CameraUpdateFactory.ZoomUpdate update) { - super(); - if (update != null) { - this.zoom = update.getZoom(); - } - } - - /** - * Sets the direction that the camera is pointing in, in degrees clockwise from north. - * - * @param bearing Bearing - * @return this - */ - @NonNull - public Builder bearing(double bearing) { - double direction = bearing; - - while (direction >= 360) { - direction -= 360; - } - while (direction < 0) { - direction += 360; - } - - this.bearing = direction; - return this; - } - - /** - * Sets the location where the camera is pointing at. - * - * @param location target of the camera - * @return this - */ - @NonNull - public Builder target(LatLng location) { - this.target = location; - return this; - } - - /** - * Set the tilt of the camera in degrees - *

- * value is clamped to {@link MapboxConstants#MINIMUM_TILT} and {@link MapboxConstants#MAXIMUM_TILT}. - *

- * - * @param tilt Tilt value of the camera - * @return this - */ - @NonNull - public Builder tilt(@FloatRange(from = MapboxConstants.MINIMUM_TILT, - to = MapboxConstants.MAXIMUM_TILT) double tilt) { - this.tilt = MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT); - return this; - } - - /** - * Set the zoom of the camera - *

- * Zoom ranges from {@link MapboxConstants#MINIMUM_ZOOM} to {@link MapboxConstants#MAXIMUM_ZOOM} - *

- * - * @param zoom Zoom value of the camera - * @return this - */ - @NonNull - public Builder zoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, - to = MapboxConstants.MAXIMUM_ZOOM) double zoom) { - this.zoom = zoom; - return this; - } - - /** - * Padding in pixels that shifts the viewport by the specified amount. - * Applied padding is going to persist and impact following camera transformations. - *

- * Specified in left, top, right, bottom order. - *

- * - * @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. - *

- * Specified in left, top, right, bottom order. - *

- * - * @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, padding); - } - } -} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.kt b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.kt new file mode 100644 index 000000000..0f769de02 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.kt @@ -0,0 +1,387 @@ +package com.mapbox.mapboxsdk.camera + +import android.content.res.TypedArray +import android.os.Parcel +import android.os.Parcelable +import androidx.annotation.FloatRange +import androidx.annotation.Keep +import androidx.annotation.Size +import com.mapbox.mapboxsdk.R +import com.mapbox.mapboxsdk.camera.CameraPosition.Builder +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory.CameraPositionUpdate +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory.ZoomUpdate +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. + */ +class CameraPosition +/** + * 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 internal constructor( + + /** + * The location that the camera is pointing at. + */ + @field:Keep + @JvmField + val target: LatLng?, + + /** + * Zoom level near the center of the screen. See [Builder.zoom] for the definition of the camera's + * zoom level. + */ + @field:Keep + @JvmField + val zoom: Double, + + /** + * The angle, in degrees, of the camera angle from the nadir (directly facing the Earth). + * See [Builder.tilt] for details of restrictions on the range of values. + */ + @field:Keep + @JvmField + val tilt: Double, + + /** + * Direction that the camera is pointing in, in degrees clockwise from north. + */ + @field:Keep + @JvmField + val bearing: Double, + + /** + * Padding in pixels. Specified in left, top, right, bottom order. + * See [Builder.padding] for the definition of the camera's padding. + */ + @field:Keep + @JvmField + val padding: DoubleArray? + +) : Parcelable { + + /** + * 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. + */ + @Deprecated("use {@link CameraPosition#CameraPosition(LatLng, double, double, double, double[])} instead.") + internal constructor(target: LatLng?, zoom: Double, tilt: Double, bearing: Double) : this(target, zoom, tilt, bearing, null) { + } + + /** + * Describe the kinds of special objects contained in this Parcelable's + * marshalled representation. + * + * @return integer 0. + */ + override fun describeContents(): Int { + 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 + * [.PARCELABLE_WRITE_RETURN_VALUE]. + */ + override fun writeToParcel(out: Parcel, flags: Int) { + out.writeDouble(bearing) + out.writeParcelable(target, flags) + out.writeDouble(tilt) + out.writeDouble(zoom) + if (padding != null) { + val length = padding.size + out.writeInt(length) + for (v in padding) { + out.writeDouble(v) + } + } else { + out.writeInt(-1) + } + } + + /** + * Returns a String with the camera target, zoom, bearing and tilt. + * + * @return A String with CameraPosition information. + */ + override fun toString(): String { + return ("Target: " + target + ", Zoom:" + zoom + ", Bearing:" + bearing + ", Tilt:" + tilt + ", Padding:" + Arrays.toString(padding)) + } + + /** + * Compares this [CameraPosition] object with another [CameraPosition] and + * determines if their target, zoom, tilt, and bearing match. + * + * @param other Another [CameraPosition] to compare with this object. + * @return True if target, zoom, tilt, and bearing match this [CameraPosition] object. + * Else, false. + */ + override fun equals(other: Any?): Boolean { + if (this === other) { + return true + } + if (other == null || javaClass != other.javaClass) { + return false + } + val cameraPosition = other as CameraPosition + if (target != null && target != 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 + } else if (!Arrays.equals(padding, cameraPosition.padding)) { + 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 fun hashCode(): Int { + var result: Int + var temp: Long + temp = java.lang.Double.doubleToLongBits(bearing) + result = (temp xor (temp ushr 32)).toInt() + result = 31 * result + (target?.hashCode() ?: 0) + temp = java.lang.Double.doubleToLongBits(tilt) + result = 31 * result + (temp xor (temp ushr 32)).toInt() + temp = java.lang.Double.doubleToLongBits(zoom) + result = 31 * result + (temp xor (temp ushr 32)).toInt() + result = 31 * result + Arrays.hashCode(padding) + return result + } + + /** + * Builder for composing CameraPosition objects. + */ + class Builder { + private var bearing = -1.0 + private var target: LatLng? = null + private var tilt = -1.0 + private var zoom = -1.0 + private var padding: DoubleArray? = null + + /** + * Create an empty builder. + */ + constructor() : super() {} + + /** + * Create a builder with an existing CameraPosition data. + * + * @param previous Existing CameraPosition values to use + */ + constructor(previous: CameraPosition?) : super() { + if (previous != null) { + bearing = previous.bearing + target = previous.target + tilt = previous.tilt + zoom = previous.zoom + padding = previous.padding + } + } + + /** + * Create a builder with an existing CameraPosition data. + * + * @param typedArray TypedArray containing attribute values + */ + constructor(typedArray: TypedArray?) : super() { + if (typedArray != null) { + bearing = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraBearing, 0.0f).toDouble() + val lat = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTargetLat, 0.0f).toDouble() + val lng = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTargetLng, 0.0f).toDouble() + target = LatLng(lat, lng) + tilt = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTilt, 0.0f).toDouble() + zoom = typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraZoom, 0.0f).toDouble() + } + } + + /** + * Create a builder from an existing CameraPositionUpdate update. + * + * @param update Update containing camera options + */ + constructor(update: CameraPositionUpdate?) : super() { + if (update != null) { + bearing = update.bearing + target = update.target + tilt = update.tilt + zoom = update.zoom + padding = update.padding + } + } + + /** + * Create builder from an existing CameraPositionUpdate update. + * + * @param update Update containing camera options + */ + constructor(update: ZoomUpdate?) : super() { + if (update != null) { + zoom = update.zoom + } + } + + /** + * Sets the direction that the camera is pointing in, in degrees clockwise from north. + * + * @param bearing Bearing + * @return this + */ + fun bearing(bearing: Double): Builder { + var direction = bearing + while (direction >= 360) { + direction -= 360.0 + } + while (direction < 0) { + direction += 360.0 + } + this.bearing = direction + return this + } + + /** + * Sets the location where the camera is pointing at. + * + * @param location target of the camera + * @return this + */ + fun target(location: LatLng?): Builder { + target = location + return this + } + + /** + * Set the tilt of the camera in degrees + * + * + * value is clamped to [MapboxConstants.MINIMUM_TILT] and [MapboxConstants.MAXIMUM_TILT]. + * + * + * @param tilt Tilt value of the camera + * @return this + */ + fun tilt(@FloatRange(from = MapboxConstants.MINIMUM_TILT, to = MapboxConstants.MAXIMUM_TILT) tilt: Double): Builder { + this.tilt = MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT) + return this + } + + /** + * Set the zoom of the camera + * + * + * Zoom ranges from [MapboxConstants.MINIMUM_ZOOM] to [MapboxConstants.MAXIMUM_ZOOM] + * + * + * @param zoom Zoom value of the camera + * @return this + */ + fun zoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM.toDouble(), to = MapboxConstants.MAXIMUM_ZOOM.toDouble()) zoom: Double): Builder { + this.zoom = zoom + return this + } + + /** + * Padding in pixels that shifts the viewport by the specified amount. + * Applied padding is going to persist and impact following camera transformations. + * + * + * Specified in left, top, right, bottom order. + * + * + * @param padding Camera padding + * @return this + */ + fun padding(@Size(4) padding: DoubleArray?): Builder { + 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. + * + * + * Specified in left, top, right, bottom order. + * + * + * @return this + */ + fun padding(left: Double, top: Double, right: Double, bottom: Double): Builder { + padding = doubleArrayOf(left, top, right, bottom) + return this + } + + /** + * Builds the CameraPosition. + * + * @return CameraPosition + */ + fun build(): CameraPosition { + return CameraPosition(target, zoom, tilt, bearing, padding) + } + } + + companion object { + @JvmField + val DEFAULT = CameraPosition(LatLng(), 0.0, 0.0, 0.0, doubleArrayOf(0.0, 0.0, 0.0, 0.0)) + + @JvmField + val CREATOR: Parcelable.Creator = object : Parcelable.Creator { + override fun createFromParcel(`in`: Parcel): CameraPosition { + val bearing = `in`.readDouble() + val target = `in`.readParcelable(LatLng::class.java.classLoader) + val tilt = `in`.readDouble() + val zoom = `in`.readDouble() + var padding: DoubleArray? = null + val paddingSize = `in`.readInt() + if (paddingSize > 0) { + padding = DoubleArray(paddingSize) + for (i in 0 until paddingSize) { + padding[i] = `in`.readDouble() + } + } + return CameraPosition(target, zoom, tilt, bearing, padding) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + } +} 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 deleted file mode 100644 index 5296af02a..000000000 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.mapbox.mapboxsdk.camera; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.mapbox.mapboxsdk.maps.MapboxMap; - -/** - * Interface definition for camera updates. - */ -public interface CameraUpdate { - - /** - * Get the camera position from the camera update. - * - * @param mapboxMap Map object to build the position from - * @return the camera position from the implementing camera update - */ - @Nullable - CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap); - -} - diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.kt b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.kt new file mode 100644 index 000000000..5cbf20174 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.kt @@ -0,0 +1,16 @@ +package com.mapbox.mapboxsdk.camera + +import com.mapbox.mapboxsdk.maps.MapboxMap + +/** + * Interface definition for camera updates. + */ +interface CameraUpdate { + /** + * Get the camera position from the camera update. + * + * @param mapboxMap Map object to build the position from + * @return the camera position from the implementing camera update + */ + fun getCameraPosition(mapboxMap: MapboxMap): CameraPosition? +} 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 deleted file mode 100644 index caac82012..000000000 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java +++ /dev/null @@ -1,638 +0,0 @@ -package com.mapbox.mapboxsdk.camera; - -import android.graphics.Point; -import android.graphics.PointF; - -import androidx.annotation.IntDef; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.mapbox.mapboxsdk.geometry.LatLng; -import com.mapbox.mapboxsdk.geometry.LatLngBounds; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Projection; -import com.mapbox.mapboxsdk.maps.UiSettings; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.util.Arrays; - -/** - * Factory for creating CameraUpdate objects. - */ -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, cameraPosition.padding); - } - - /** - * 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, null); - } - - /** - * Returns a CameraUpdate that transforms the camera such that the specified - * latitude/longitude bounds are centered on screen at the greatest possible zoom level while maintaining - * current camera position bearing and tilt values. - *

- * 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. - *

- * - * @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 CameraUpdate that transforms the camera such that the specified - * latitude/longitude bounds are centered on screen at the greatest possible zoom level while using - * provided bearing and tilt values. - *

- * 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. - *

- * - * @param bounds Bounds to match Camera position with - * @param bearing Bearing to take in account when generating the bounds - * @param tilt Tilt to take in account when generating the bounds - * @param padding Padding added to the bounds - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate newLatLngBounds(@NonNull LatLngBounds bounds, double bearing, double tilt, int padding) { - return newLatLngBounds(bounds, bearing, tilt, padding, padding, padding, padding); - } - - /** - * Returns a CameraUpdate that transforms the camera such that the specified - * latitude/longitude bounds are centered on screen at the greatest possible zoom level while maintaining - * current camera position bearing and tilt values. - *

- * 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. - *

- * - * @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, null, null, paddingLeft, paddingTop, paddingRight, paddingBottom); - } - - /** - * Returns a CameraUpdate that transforms the camera such that the specified - * latitude/longitude bounds are centered on screen at the greatest possible zoom level while using - * provided bearing and tilt values. - *

- * 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. - *

- * - * @param bounds Bounds to base the Camera position out of - * @param bearing Bearing to take in account when generating the bounds - * @param tilt Tilt to take in account when generating the bounds - * @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, double bearing, double tilt, - int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) { - return new CameraBoundsUpdate(bounds, bearing, tilt, paddingLeft, paddingTop, paddingRight, paddingBottom); - } - - /** - * Returns a CameraUpdate that moves the center of the screen to a latitude and longitude - * 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, 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}); - } - - /** - * 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); - } - - /** - * Returns a CameraUpdate that moves the camera viewpoint to a particular bearing. - * - * @param bearing Bearing to change to - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate bearingTo(double bearing) { - return new CameraPositionUpdate(bearing, null, -1, -1, null); - } - - /** - * Returns a CameraUpdate that moves the camera viewpoint to a particular tilt. - * - * @param tilt Tilt to change to - * @return CameraUpdate Final Camera Position - */ - public static CameraUpdate tiltTo(double tilt) { - 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. - *

- * Specified in left, top, right, bottom order. - *

- * - * @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. - *

- * Specified in left, top, right, bottom order. - *

- * - * @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}); - } - - // - // CameraUpdate types - // - - static final class CameraPositionUpdate implements CameraUpdate { - - private final double bearing; - 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, double[] padding) { - this.bearing = bearing; - this.target = target; - this.tilt = tilt; - this.zoom = zoom; - this.padding = padding; - } - - public LatLng getTarget() { - return target; - } - - public double getBearing() { - return bearing; - } - - public double getTilt() { - return tilt; - } - - public double getZoom() { - return zoom; - } - - public double[] getPadding() { - return padding; - } - - @Override - public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { - if (target == null) { - CameraPosition previousPosition = mapboxMap.getCameraPosition(); - return new CameraPosition.Builder(this) - .target(previousPosition.target) - .build(); - } - return new CameraPosition.Builder(this).build(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - CameraPositionUpdate that = (CameraPositionUpdate) o; - - if (Double.compare(that.bearing, bearing) != 0) { - return false; - } - if (Double.compare(that.tilt, tilt) != 0) { - return false; - } - if (Double.compare(that.zoom, zoom) != 0) { - return false; - } - if (target != null ? !target.equals(that.target) : that.target != null) { - return false; - } - return Arrays.equals(padding, that.padding); - } - - @Override - public int hashCode() { - 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; - } - - @Override - public String toString() { - return "CameraPositionUpdate{" - + "bearing=" + bearing - + ", target=" + target - + ", tilt=" + tilt - + ", zoom=" + zoom - + ", padding=" + Arrays.toString(padding) - + '}'; - } - } - - static final class CameraBoundsUpdate implements CameraUpdate { - - private final LatLngBounds bounds; - private final int[] padding; - private final Double bearing; - private final Double tilt; - - CameraBoundsUpdate(LatLngBounds bounds, Double bearing, Double tilt, int[] padding) { - this.bounds = bounds; - this.padding = padding; - this.bearing = bearing; - this.tilt = tilt; - } - - CameraBoundsUpdate(LatLngBounds bounds, Double bearing, Double tilt, int paddingLeft, - int paddingTop, int paddingRight, int paddingBottom) { - this(bounds, bearing, tilt, new int[] {paddingLeft, paddingTop, paddingRight, paddingBottom}); - } - - public LatLngBounds getBounds() { - return bounds; - } - - public int[] getPadding() { - return padding; - } - - @Override - public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { - if (bearing == null && tilt == null) { - // use current camera position tilt and bearing - return mapboxMap.getCameraForLatLngBounds(bounds, padding); - } else { - // use provided tilt and bearing - assert bearing != null; - assert tilt != null; - return mapboxMap.getCameraForLatLngBounds(bounds, padding, bearing, tilt); - } - } - - @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - CameraBoundsUpdate that = (CameraBoundsUpdate) o; - - if (!bounds.equals(that.bounds)) { - return false; - } - return Arrays.equals(padding, that.padding); - } - - @Override - public int hashCode() { - int result = bounds.hashCode(); - result = 31 * result + Arrays.hashCode(padding); - return result; - } - - @Override - public String toString() { - return "CameraBoundsUpdate{" - + "bounds=" + bounds - + ", padding=" + Arrays.toString(padding) - + '}'; - } - } - - 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(); - int[] padding = mapboxMap.getPadding(); - - // we inverse the map padding, is reapplied when using moveTo/easeTo or animateTo - PointF targetPoint = new PointF( - (viewPortWidth - padding[0] + padding[1]) / 2 + x, - (viewPortHeight + padding[1] - padding[3]) / 2 + y - ); - - LatLng latLng = projection.fromScreenLocation(targetPoint); - CameraPosition previousPosition = mapboxMap.getCameraPosition(); - CameraPosition position = - new CameraPosition.Builder() - .target(latLng) - .zoom(previousPosition.zoom) - .tilt(previousPosition.tilt) - .bearing(previousPosition.bearing) - .build(); - return position; - } - - @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - CameraMoveUpdate that = (CameraMoveUpdate) o; - - if (Float.compare(that.x, x) != 0) { - return false; - } - return Float.compare(that.y, y) == 0; - } - - @Override - public int hashCode() { - int result = (x != +0.0f ? Float.floatToIntBits(x) : 0); - result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0); - return result; - } - - @Override - public String toString() { - return "CameraMoveUpdate{" - + "x=" + x - + ", y=" + y - + '}'; - } - } - - 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 boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - ZoomUpdate that = (ZoomUpdate) o; - - if (type != that.type) { - return false; - } - if (Double.compare(that.zoom, zoom) != 0) { - return false; - } - if (Float.compare(that.x, x) != 0) { - return false; - } - return Float.compare(that.y, y) == 0; - } - - @Override - public int hashCode() { - int result; - long temp; - result = type; - temp = Double.doubleToLongBits(zoom); - result = 31 * result + (int) (temp ^ (temp >>> 32)); - result = 31 * result + (x != +0.0f ? Float.floatToIntBits(x) : 0); - result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0); - return result; - } - - @Override - public String toString() { - return "ZoomUpdate{" - + "type=" + type - + ", zoom=" + zoom - + ", x=" + x - + ", y=" + y - + '}'; - } - } -} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.kt b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.kt new file mode 100644 index 000000000..ea7aa9661 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.kt @@ -0,0 +1,510 @@ +package com.mapbox.mapboxsdk.camera + +import android.graphics.Point +import android.graphics.PointF +import androidx.annotation.IntDef +import com.mapbox.mapboxsdk.geometry.LatLng +import com.mapbox.mapboxsdk.geometry.LatLngBounds +import com.mapbox.mapboxsdk.maps.MapboxMap +import timber.log.Timber +import java.lang.Double.min +import java.util.Arrays + +/** + * Factory for creating CameraUpdate objects. + */ +object CameraUpdateFactory { + /** + * Returns a CameraUpdate that moves the camera to a specified CameraPosition. + * + * @param cameraPosition Camera Position to change to + * @return CameraUpdate Final Camera Position + */ + @JvmStatic + fun newCameraPosition(cameraPosition: CameraPosition): CameraUpdate { + return CameraPositionUpdate(cameraPosition.bearing, cameraPosition.target, cameraPosition.tilt, cameraPosition.zoom, cameraPosition.padding) + } + + /** + * 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 + */ + @JvmStatic + fun newLatLng(latLng: LatLng): CameraUpdate { + return CameraPositionUpdate(-1.0, latLng, -1.0, -1.0, null) + } + + /** + * Returns a CameraUpdate that transforms the camera such that the specified + * latitude/longitude bounds are centered on screen at the greatest possible zoom level while maintaining + * current camera position bearing and tilt values. + * + * + * 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. + * + * + * @param bounds Bounds to match Camera position with + * @param padding Padding added to the bounds + * @return CameraUpdate Final Camera Position + */ + @JvmStatic + fun newLatLngBounds(bounds: LatLngBounds, padding: Int): CameraUpdate { + return newLatLngBounds(bounds, padding, padding, padding, padding) + } + + /** + * Returns a CameraUpdate that transforms the camera such that the specified + * latitude/longitude bounds are centered on screen at the greatest possible zoom level while using + * provided bearing and tilt values. + * + * + * 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. + * + * + * @param bounds Bounds to match Camera position with + * @param bearing Bearing to take in account when generating the bounds + * @param tilt Tilt to take in account when generating the bounds + * @param padding Padding added to the bounds + * @return CameraUpdate Final Camera Position + */ + @JvmStatic + fun newLatLngBounds(bounds: LatLngBounds, bearing: Double, tilt: Double, padding: Int): CameraUpdate { + return newLatLngBounds(bounds, bearing, tilt, padding, padding, padding, padding) + } + + /** + * Returns a CameraUpdate that transforms the camera such that the specified + * latitude/longitude bounds are centered on screen at the greatest possible zoom level while maintaining + * current camera position bearing and tilt values. + * + * + * 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. + * + * + * @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 + */ + @JvmStatic + fun newLatLngBounds(bounds: LatLngBounds, paddingLeft: Int, paddingTop: Int, paddingRight: Int, paddingBottom: Int): CameraUpdate { + return CameraBoundsUpdate(bounds, null, null, paddingLeft, paddingTop, paddingRight, paddingBottom) + } + + /** + * Returns a CameraUpdate that transforms the camera such that the specified + * latitude/longitude bounds are centered on screen at the greatest possible zoom level while using + * provided bearing and tilt values. + * + * + * 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. + * + * + * @param bounds Bounds to base the Camera position out of + * @param bearing Bearing to take in account when generating the bounds + * @param tilt Tilt to take in account when generating the bounds + * @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 + */ + @JvmStatic + fun newLatLngBounds(bounds: LatLngBounds, bearing: Double, tilt: Double, paddingLeft: Int, paddingTop: Int, paddingRight: Int, paddingBottom: Int): CameraUpdate { + return CameraBoundsUpdate(bounds, bearing, tilt, paddingLeft, paddingTop, paddingRight, paddingBottom) + } + + /** + * Returns a CameraUpdate that moves the center of the screen to a latitude and longitude + * 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 + */ + @JvmStatic + fun newLatLngZoom(latLng: LatLng, zoom: Double): CameraUpdate { + return CameraPositionUpdate(-1.0, latLng, -1.0, 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 + */ + @JvmStatic + fun newLatLngPadding(latLng: LatLng, left: Double, top: Double, right: Double, bottom: Double): CameraUpdate { + return CameraPositionUpdate(-1.0, latLng, -1.0, -1.0, doubleArrayOf(left, top, right, bottom)) + } + + /** + * 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 + */ + @JvmStatic + fun zoomBy(amount: Double, focus: Point): CameraUpdate { + return ZoomUpdate(amount, focus.x.toFloat(), focus.y.toFloat()) + } + + /** + * 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 + */ + @JvmStatic + fun zoomBy(amount: Double): CameraUpdate { + return 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 + */ + @JvmStatic + fun zoomIn(): CameraUpdate { + return 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 + */ + @JvmStatic + fun zoomOut(): CameraUpdate { + return 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 + */ + @JvmStatic + fun zoomTo(zoom: Double): CameraUpdate { + return ZoomUpdate(ZoomUpdate.ZOOM_TO, zoom) + } + + /** + * Returns a CameraUpdate that moves the camera viewpoint to a particular bearing. + * + * @param bearing Bearing to change to + * @return CameraUpdate Final Camera Position + */ + @JvmStatic + fun bearingTo(bearing: Double): CameraUpdate { + return CameraPositionUpdate(bearing, null, -1.0, -1.0, null) + } + + /** + * Returns a CameraUpdate that moves the camera viewpoint to a particular tilt. + * + * @param tilt Tilt to change to + * @return CameraUpdate Final Camera Position + */ + @JvmStatic + fun tiltTo(tilt: Double): CameraUpdate { + return CameraPositionUpdate(-1.0, null, tilt, -1.0, null) + } + + /** + * Returns a CameraUpdate that when animated changes the camera padding. + * Applied padding is going to persist and impact following camera transformations. + * + * + * Specified in left, top, right, bottom order. + * + * + * @param padding Padding to change to + * @return CameraUpdate Final Camera Position + */ + @JvmStatic + fun paddingTo(padding: DoubleArray?): CameraUpdate { + return CameraPositionUpdate(-1.0, null, -1.0, -1.0, padding) + } + + /** + * Returns a CameraUpdate that when animated changes the camera padding. + * Applied padding is going to persist and impact following camera transformations. + * + * + * Specified in left, top, right, bottom order. + * + * + * @return CameraUpdate Final Camera Position + */ + @JvmStatic + fun paddingTo(left: Double, top: Double, right: Double, bottom: Double): CameraUpdate { + return paddingTo(doubleArrayOf(left, top, right, bottom)) + } + + // + // CameraUpdate types + // + class CameraPositionUpdate(val bearing: Double, val target: LatLng?, val tilt: Double, val zoom: Double, val padding: DoubleArray?) : CameraUpdate { + + override fun getCameraPosition(mapboxMap: MapboxMap): CameraPosition { + if (target == null) { + val previousPosition = mapboxMap.cameraPosition + return CameraPosition.Builder(this).target(previousPosition.target).build() + } + return CameraPosition.Builder(this).build() + } + + override fun equals(other: Any?): Boolean { + if (this === other) { + return true + } + if (other == null || javaClass != other.javaClass) { + return false + } + val that = other as CameraPositionUpdate + if (java.lang.Double.compare(that.bearing, bearing) != 0) { + return false + } + if (java.lang.Double.compare(that.tilt, tilt) != 0) { + return false + } + if (java.lang.Double.compare(that.zoom, zoom) != 0) { + return false + } + return if (if (target != null) target != that.target else that.target != null) { + false + } else { + Arrays.equals(padding, that.padding) + } + } + + override fun hashCode(): Int { + var result: Int + var temp: Long + temp = java.lang.Double.doubleToLongBits(bearing) + result = (temp xor (temp ushr 32)).toInt() + result = 31 * result + (target?.hashCode() ?: 0) + temp = java.lang.Double.doubleToLongBits(tilt) + result = 31 * result + (temp xor (temp ushr 32)).toInt() + temp = java.lang.Double.doubleToLongBits(zoom) + result = 31 * result + (temp xor (temp ushr 32)).toInt() + result = 31 * result + Arrays.hashCode(padding) + return result + } + + override fun toString(): String { + return ("CameraPositionUpdate{" + "bearing=" + bearing + ", target=" + target + ", tilt=" + tilt + ", zoom=" + zoom + ", padding=" + Arrays.toString(padding) + '}') + } + } + + internal class CameraBoundsUpdate(val bounds: LatLngBounds, private val bearing: Double?, private val tilt: Double?, val padding: IntArray) : CameraUpdate { + constructor(bounds: LatLngBounds, bearing: Double?, tilt: Double?, paddingLeft: Int, paddingTop: Int, paddingRight: Int, paddingBottom: Int) : this( + bounds, + bearing, + tilt, + intArrayOf(paddingLeft, paddingTop, paddingRight, paddingBottom) + ) { + } + + override fun getCameraPosition(mapboxMap: MapboxMap): CameraPosition? { + return if (bearing == null && tilt == null) { + // use current camera position tilt and bearing + mapboxMap.getCameraForLatLngBounds(bounds, padding) + } else { + // use provided tilt and bearing + assert(bearing != null) + assert(tilt != null) + mapboxMap.getCameraForLatLngBounds(bounds, padding, bearing!!, tilt!!) + } + } + + override fun equals(other: Any?): Boolean { + if (this === other) { + return true + } + if (other == null || javaClass != other.javaClass) { + return false + } + val that = other as CameraBoundsUpdate + return if (bounds != that.bounds) { + false + } else { + padding.contentEquals(that.padding) + } + } + + override fun hashCode(): Int { + var result = bounds.hashCode() + result = 31 * result + Arrays.hashCode(padding) + return result + } + + override fun toString(): String { + return ("CameraBoundsUpdate{" + "bounds=" + bounds + ", padding=" + Arrays.toString(padding) + '}') + } + } + + internal class CameraMoveUpdate(private val x: Float, private val y: Float) : CameraUpdate { + override fun getCameraPosition(mapboxMap: MapboxMap): CameraPosition { + val uiSettings = mapboxMap.uiSettings + val projection = mapboxMap.projection + // Calculate the new center point + val viewPortWidth = uiSettings.width + val viewPortHeight = uiSettings.height + val padding = mapboxMap.padding + + // we inverse the map padding, is reapplied when using moveTo/easeTo or animateTo + val targetPoint = PointF((viewPortWidth - padding[0] + padding[1]) / 2 + x, (viewPortHeight + padding[1] - padding[3]) / 2 + y) + val latLng = projection.fromScreenLocation(targetPoint) + val previousPosition = mapboxMap.cameraPosition + return CameraPosition.Builder().target(latLng).zoom(previousPosition.zoom).tilt(previousPosition.tilt).bearing(previousPosition.bearing).build() + } + + override fun equals(other: Any?): Boolean { + if (this === other) { + return true + } + if (other == null || javaClass != other.javaClass) { + return false + } + val that = other as CameraMoveUpdate + return if (java.lang.Float.compare(that.x, x) != 0) { + false + } else { + that.y.compareTo(y) == 0 + } + } + + override fun hashCode(): Int { + var result = if (x != +0.0f) java.lang.Float.floatToIntBits(x) else 0 + result = 31 * result + if (y != +0.0f) java.lang.Float.floatToIntBits(y) else 0 + return result + } + + override fun toString(): String { + return ("CameraMoveUpdate{" + "x=" + x + ", y=" + y + '}') + } + } + + class ZoomUpdate : CameraUpdate { + @IntDef(ZOOM_IN, ZOOM_OUT, ZOOM_BY, ZOOM_TO, ZOOM_TO_POINT) + @Retention(AnnotationRetention.SOURCE) + internal annotation class Type + + @get:Type + @Type + val type: Int + val zoom: Double + var x = 0f + private set + var y = 0f + private set + + constructor(@Type type: Int) { + this.type = type + zoom = 0.0 + } + + constructor(@Type type: Int, zoom: Double) { + this.type = type + this.zoom = zoom + } + + constructor(zoom: Double, x: Float, y: Float) { + type = ZOOM_TO_POINT + this.zoom = zoom + this.x = x + this.y = y + } + + private fun transformZoom(currentZoomArg: Double): Double { + return when (type) { + ZOOM_IN -> currentZoomArg + 1 + ZOOM_OUT -> { + min(currentZoomArg - 1, 0.0) + } + + ZOOM_TO -> zoom + ZOOM_BY, ZOOM_TO_POINT -> currentZoomArg + zoom + else -> { + Timber.e("Unprocessed when branch") + 4.0 + } + } + } + + override fun getCameraPosition(mapboxMap: MapboxMap): CameraPosition { + val cameraPosition = mapboxMap.cameraPosition + return if (type != ZOOM_TO_POINT) { + CameraPosition.Builder(cameraPosition).zoom(transformZoom(cameraPosition.zoom)).build() + } else { + CameraPosition.Builder(cameraPosition).zoom(transformZoom(cameraPosition.zoom)).target(mapboxMap.projection.fromScreenLocation(PointF(x, y))).build() + } + } + + override fun equals(other: Any?): Boolean { + if (this === other) { + return true + } + if (other == null || javaClass != other.javaClass) { + return false + } + val that = other as ZoomUpdate + if (type != that.type) { + return false + } + if (java.lang.Double.compare(that.zoom, zoom) != 0) { + return false + } + return if (java.lang.Float.compare(that.x, x) != 0) { + false + } else { + that.y.compareTo(y) == 0 + } + } + + override fun hashCode(): Int { + var result: Int + val temp: Long + result = type + temp = java.lang.Double.doubleToLongBits(zoom) + result = 31 * result + (temp xor (temp ushr 32)).toInt() + result = 31 * result + if (x != +0.0f) java.lang.Float.floatToIntBits(x) else 0 + result = 31 * result + if (y != +0.0f) java.lang.Float.floatToIntBits(y) else 0 + return result + } + + override fun toString(): String { + return ("ZoomUpdate{" + "type=" + type + ", zoom=" + zoom + ", x=" + x + ", y=" + y + '}') + } + + companion object { + const val ZOOM_IN = 0 + const val ZOOM_OUT = 1 + const val ZOOM_BY = 2 + const val ZOOM_TO = 3 + const val ZOOM_TO_POINT = 4 + } + } +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/package-info.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/PackageInfo.kt similarity index 63% rename from platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/package-info.java rename to platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/PackageInfo.kt index 04ef699c2..6d39c28a3 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/package-info.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/PackageInfo.kt @@ -1,4 +1,4 @@ /** * Contains the Mapbox Maps Android Camera API classes. */ -package com.mapbox.mapboxsdk.camera; +package com.mapbox.mapboxsdk.camera diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/engine/MapboxFusedLocationEngineImpl.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/engine/MapboxFusedLocationEngineImpl.java index 0513d0413..a97e24f50 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/engine/MapboxFusedLocationEngineImpl.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/engine/MapboxFusedLocationEngineImpl.java @@ -8,10 +8,10 @@ import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Looper; -import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import timber.log.Timber; import static com.mapbox.mapboxsdk.location.engine.Utils.isBetterLocation; @@ -20,7 +20,7 @@ import static com.mapbox.mapboxsdk.location.engine.Utils.isBetterLocation; *

* Note: fusion will not work in background mode. */ -class MapboxFusedLocationEngineImpl extends AndroidLocationEngineImpl { +public class MapboxFusedLocationEngineImpl extends AndroidLocationEngineImpl { private static final String TAG = "MapboxLocationEngine"; MapboxFusedLocationEngineImpl(@NonNull Context context) { @@ -121,17 +121,17 @@ class MapboxFusedLocationEngineImpl extends AndroidLocationEngineImpl { @Override public void onStatusChanged(String provider, int status, Bundle extras) { - Log.d(TAG, "onStatusChanged: " + provider); + Timber.d( "onStatusChanged: " + provider); } @Override public void onProviderEnabled(String provider) { - Log.d(TAG, "onProviderEnabled: " + provider); + Timber.d( "onProviderEnabled: " + provider); } @Override public void onProviderDisabled(String provider) { - Log.d(TAG, "onProviderDisabled: " + provider); + Timber.d("onProviderDisabled: " + provider); } } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactoryTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactoryTest.kt index bf06676db..2ec663792 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactoryTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactoryTest.kt @@ -37,8 +37,8 @@ class CameraUpdateFactoryTest : BaseTest() { mapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0)) val cameraPosition = mapboxMap.cameraPosition - assertEquals("latitude should match:", 60.0, cameraPosition.target.latitude, 0.1) - assertEquals("longitude should match:", 24.0, cameraPosition.target.longitude, 0.1) + assertEquals("latitude should match:", 60.0, cameraPosition.target!!.latitude, 0.1) + assertEquals("longitude should match:", 24.0, cameraPosition.target!!.longitude, 0.1) assertEquals("bearing should match:", 0.0, cameraPosition.bearing, 0.1) assertEquals("zoom should match", 5.5, cameraPosition.zoom, 0.1) assertEquals("tilt should match:", 0.0, cameraPosition.tilt, 0.1) @@ -61,8 +61,8 @@ class CameraUpdateFactoryTest : BaseTest() { mapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0)) val cameraPosition = mapboxMap.cameraPosition - assertEquals("latitude should match:", 60.0, cameraPosition.target.latitude, 0.1) - assertEquals("longitude should match:", 24.0, cameraPosition.target.longitude, 0.1) + assertEquals("latitude should match:", 60.0, cameraPosition.target!!.latitude, 0.1) + assertEquals("longitude should match:", 24.0, cameraPosition.target!!.longitude, 0.1) assertEquals("bearing should match:", 0.0, cameraPosition.bearing, 0.1) assertEquals("zoom should match", 6.0, cameraPosition.zoom, 0.1) assertEquals("tilt should match:", 45.0, cameraPosition.tilt, 0.1) @@ -85,8 +85,8 @@ class CameraUpdateFactoryTest : BaseTest() { mapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0)) val cameraPosition = mapboxMap.cameraPosition - assertEquals("latitude should match:", 60.0, cameraPosition.target.latitude, 0.1) - assertEquals("longitude should match:", 24.0, cameraPosition.target.longitude, 0.1) + assertEquals("latitude should match:", 60.0, cameraPosition.target!!.latitude, 0.1) + assertEquals("longitude should match:", 24.0, cameraPosition.target!!.longitude, 0.1) assertEquals("bearing should match:", 30.0, cameraPosition.bearing, 0.1) assertEquals("zoom should match", 5.3, cameraPosition.zoom, 0.1) assertEquals("tilt should match:", 0.0, cameraPosition.tilt, 0.1) @@ -109,8 +109,8 @@ class CameraUpdateFactoryTest : BaseTest() { mapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0)) val cameraPosition = mapboxMap.cameraPosition - assertEquals("latitude should match:", 60.0, cameraPosition.target.latitude, 0.1) - assertEquals("longitude should match:", 24.0, cameraPosition.target.longitude, 0.1) + assertEquals("latitude should match:", 60.0, cameraPosition.target!!.latitude, 0.1) + assertEquals("longitude should match:", 24.0, cameraPosition.target!!.longitude, 0.1) assertEquals("bearing should match:", 30.0, cameraPosition.bearing, 0.1) assertEquals("zoom should match", 5.6, cameraPosition.zoom, 0.1) assertEquals("tilt should match:", 45.0, cameraPosition.tilt, 0.1) @@ -133,8 +133,8 @@ class CameraUpdateFactoryTest : BaseTest() { mapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 30.0, 40.0, 0)) val cameraPosition = mapboxMap.cameraPosition - assertEquals("latitude should match:", 60.0, cameraPosition.target.latitude, 0.1) - assertEquals("longitude should match:", 24.0, cameraPosition.target.longitude, 0.1) + assertEquals("latitude should match:", 60.0, cameraPosition.target!!.latitude, 0.1) + assertEquals("longitude should match:", 24.0, cameraPosition.target!!.longitude, 0.1) assertEquals("bearing should match:", 30.0, cameraPosition.bearing, 0.1) assertEquals("zoom should match", 5.6, cameraPosition.zoom, 0.1) assertEquals("tilt should match:", 40.0, cameraPosition.tilt, 0.1) @@ -144,7 +144,7 @@ class CameraUpdateFactoryTest : BaseTest() { @UiThreadTest fun withPadding_cameraInvalidated_paddingPersisting() { val initialCameraPosition = mapboxMap.cameraPosition - val initialPoint = mapboxMap.projection.toScreenLocation(initialCameraPosition.target) + val initialPoint = mapboxMap.projection.toScreenLocation(initialCameraPosition.target!!) val bottomPadding = mapView.height / 4 val leftPadding = mapView.width / 4 @@ -154,10 +154,10 @@ class CameraUpdateFactoryTest : BaseTest() { Assert.assertArrayEquals(intArrayOf(leftPadding, 0, 0, bottomPadding), mapboxMap.padding) val resultingCameraPosition = mapboxMap.cameraPosition - assertEquals(initialCameraPosition.target, resultingCameraPosition.target) + assertEquals(initialCameraPosition.target!!, resultingCameraPosition.target!!) assertEquals( PointF(initialPoint.x + leftPadding / 2, initialPoint.y - bottomPadding / 2), - mapboxMap.projection.toScreenLocation(resultingCameraPosition.target) + mapboxMap.projection.toScreenLocation(resultingCameraPosition.target!!) ) Assert.assertArrayEquals(padding, resultingCameraPosition.padding, 0.0001) } @@ -175,12 +175,12 @@ class CameraUpdateFactoryTest : BaseTest() { Assert.assertArrayEquals(intArrayOf(0, topPadding, rightPadding, 0), mapboxMap.padding) val resultingCameraPosition = mapboxMap.cameraPosition - assertEquals(expectedTarget.latitude, resultingCameraPosition.target.latitude, 0.1) - assertEquals(expectedTarget.longitude, resultingCameraPosition.target.longitude, 0.1) + assertEquals(expectedTarget.latitude, resultingCameraPosition.target!!.latitude, 0.1) + assertEquals(expectedTarget!!.longitude, resultingCameraPosition.target!!.longitude, 0.1) val centerLatLng = mapboxMap.projection.fromScreenLocation(PointF((mapView.width / 2).toFloat(), (mapView.height / 2).toFloat())) - assertTrue(centerLatLng.latitude > resultingCameraPosition.target.latitude) - assertTrue(centerLatLng.longitude > resultingCameraPosition.target.longitude) + assertTrue(centerLatLng.latitude > resultingCameraPosition.target!!.latitude) + assertTrue(centerLatLng.longitude > resultingCameraPosition.target!!.longitude) Assert.assertArrayEquals(padding, resultingCameraPosition.padding, 0.0001) } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/location/LocationComponentTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/location/LocationComponentTest.kt index 1dc4e7b7d..c426d0ed7 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/location/LocationComponentTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/location/LocationComponentTest.kt @@ -1093,8 +1093,8 @@ class LocationComponentTest : EspressoTest() { uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS) assertEquals(77.0, mapboxMap.cameraPosition.bearing, 0.1) - assertEquals(location.latitude, mapboxMap.cameraPosition.target.latitude, 0.1) - assertEquals(location.longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + assertEquals(location.latitude, mapboxMap.cameraPosition.target!!.latitude, 0.1) + assertEquals(location.longitude, mapboxMap.cameraPosition.target!!.longitude, 0.1) location.bearing = 92f location.latitude = 30.0 @@ -1104,8 +1104,8 @@ class LocationComponentTest : EspressoTest() { uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS) // Waiting for the animation to finish assertEquals(92.0, mapboxMap.cameraPosition.bearing, 0.1) - assertEquals(location.latitude, mapboxMap.cameraPosition.target.latitude, 0.1) - assertEquals(location.longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + assertEquals(location.latitude, mapboxMap.cameraPosition.target!!.latitude, 0.1) + assertEquals(location.longitude, mapboxMap.cameraPosition.target!!.longitude, 0.1) } } @@ -1130,8 +1130,8 @@ class LocationComponentTest : EspressoTest() { ) component.isLocationComponentEnabled = true component.cameraMode = CameraMode.NONE_GPS - val latitude = mapboxMap.cameraPosition.target.latitude - val longitude = mapboxMap.cameraPosition.target.longitude + val latitude = mapboxMap.cameraPosition.target!!.latitude + val longitude = mapboxMap.cameraPosition.target!!.longitude location.bearing = 77f component.forceLocationUpdate(location) @@ -1139,8 +1139,8 @@ class LocationComponentTest : EspressoTest() { uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS) assertEquals(77.0, mapboxMap.cameraPosition.bearing, 0.1) - assertEquals(latitude, mapboxMap.cameraPosition.target.latitude, 0.1) - assertEquals(longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + assertEquals(latitude, mapboxMap.cameraPosition.target!!.latitude, 0.1) + assertEquals(longitude, mapboxMap.cameraPosition.target!!.longitude, 0.1) location.bearing = 92f location.latitude = 30.0 @@ -1150,8 +1150,8 @@ class LocationComponentTest : EspressoTest() { uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS) assertEquals(92.0, mapboxMap.cameraPosition.bearing, 0.1) - assertEquals(latitude, mapboxMap.cameraPosition.target.latitude, 0.1) - assertEquals(longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + assertEquals(latitude, mapboxMap.cameraPosition.target!!.latitude, 0.1) + assertEquals(longitude, mapboxMap.cameraPosition.target!!.longitude, 0.1) } } @@ -1176,8 +1176,8 @@ class LocationComponentTest : EspressoTest() { ) component.isLocationComponentEnabled = true component.cameraMode = CameraMode.NONE - val latitude = mapboxMap.cameraPosition.target.latitude - val longitude = mapboxMap.cameraPosition.target.longitude + val latitude = mapboxMap.cameraPosition.target!!.latitude + val longitude = mapboxMap.cameraPosition.target!!.longitude val bearing = mapboxMap.cameraPosition.bearing location.bearing = 77f @@ -1186,8 +1186,8 @@ class LocationComponentTest : EspressoTest() { uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS) assertEquals(bearing, mapboxMap.cameraPosition.bearing, 0.1) - assertEquals(latitude, mapboxMap.cameraPosition.target.latitude, 0.1) - assertEquals(longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + assertEquals(latitude, mapboxMap.cameraPosition.target!!.latitude, 0.1) + assertEquals(longitude, mapboxMap.cameraPosition.target!!.longitude, 0.1) location.bearing = 92f location.latitude = 30.0 @@ -1197,8 +1197,8 @@ class LocationComponentTest : EspressoTest() { uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS) assertEquals(bearing, mapboxMap.cameraPosition.bearing, 0.1) - assertEquals(latitude, mapboxMap.cameraPosition.target.latitude, 0.1) - assertEquals(longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + assertEquals(latitude, mapboxMap.cameraPosition.target!!.latitude, 0.1) + assertEquals(longitude, mapboxMap.cameraPosition.target!!.longitude, 0.1) } } @@ -1294,7 +1294,7 @@ class LocationComponentTest : EspressoTest() { } @Test - @Ignore + @Ignore("No explanation") fun animators_zoomWhileTrackingCanceledOnModeChange() { val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction { override fun onLocationComponentAction( @@ -1391,7 +1391,7 @@ class LocationComponentTest : EspressoTest() { } @Test - @Ignore + @Ignore("No explanation") fun animators_cancelZoomWhileTracking() { val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction { override fun onLocationComponentAction( @@ -1482,7 +1482,7 @@ class LocationComponentTest : EspressoTest() { } @Test - @Ignore + @Ignore("No explanation") fun animators_tiltWhileTrackingCanceledOnModeChange() { val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction { override fun onLocationComponentAction( @@ -1578,7 +1578,7 @@ class LocationComponentTest : EspressoTest() { } @Test - @Ignore + @Ignore("No explanation") fun animators_cancelTiltWhileTracking() { val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction { override fun onLocationComponentAction( @@ -1635,8 +1635,8 @@ class LocationComponentTest : EspressoTest() { uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS) assertEquals(location.bearing.toDouble(), mapboxMap.cameraPosition.bearing, 0.1) - assertEquals(location.latitude, mapboxMap.cameraPosition.target.latitude, 0.1) - assertEquals(location.longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + assertEquals(location.latitude, mapboxMap.cameraPosition.target!!.latitude, 0.1) + assertEquals(location.longitude, mapboxMap.cameraPosition.target!!.longitude, 0.1) } } @@ -1667,12 +1667,12 @@ class LocationComponentTest : EspressoTest() { mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(target)) mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(15.0)) mapboxMap.moveCamera(CameraUpdateFactory.bearingTo(90.0)) - assertTrue(Utils.immediateAnimation(mapboxMap.projection, mapboxMap.cameraPosition.target, LatLng(location))) + assertTrue(Utils.immediateAnimation(mapboxMap.projection, mapboxMap.cameraPosition.target!!, LatLng(location))) component.cameraMode = CameraMode.TRACKING_GPS assertEquals(location.bearing.toDouble(), mapboxMap.cameraPosition.bearing, 0.1) - assertEquals(location.latitude, mapboxMap.cameraPosition.target.latitude, 0.1) - assertEquals(location.longitude, mapboxMap.cameraPosition.target.longitude, 0.1) + assertEquals(location.latitude, mapboxMap.cameraPosition.target!!.latitude, 0.1) + assertEquals(location.longitude, mapboxMap.cameraPosition.target!!.longitude, 0.1) } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.kt index 0aa35b678..10498acb9 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.kt @@ -674,8 +674,8 @@ class LocationLayerControllerTest : EspressoTest() { assertThat( Math.abs(zoom - mapboxMap.cameraPosition.zoom) < 0.1 && - Math.abs(target.latitude - mapboxMap.cameraPosition.target.latitude) < 0.1 && - Math.abs(target.longitude - mapboxMap.cameraPosition.target.longitude) < 0.1, + Math.abs(target.latitude - mapboxMap.cameraPosition.target!!.latitude) < 0.1 && + Math.abs(target!!.longitude - mapboxMap.cameraPosition.target!!.longitude) < 0.1, `is`(true) ) @@ -720,8 +720,8 @@ class LocationLayerControllerTest : EspressoTest() { assertThat( abs(zoom - mapboxMap.cameraPosition.zoom) < 0.1 && - abs(target.latitude - mapboxMap.cameraPosition.target.latitude) < 0.1 && - abs(target.longitude - mapboxMap.cameraPosition.target.longitude) < 0.1, + abs(target.latitude - mapboxMap.cameraPosition.target!!.latitude) < 0.1 && + abs(target!!.longitude - mapboxMap.cameraPosition.target!!.longitude) < 0.1, `is`(true) ) diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapGestureDetectorTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapGestureDetectorTest.kt index de0af6286..bbdc1e6fc 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapGestureDetectorTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapGestureDetectorTest.kt @@ -61,8 +61,8 @@ class MapGestureDetectorTest : BaseTest() { // move to expected target onView(withId(R.id.mapView)).perform(move(-maxWidth / 2f, -maxHeight / 2f, withVelocity = false)) rule.runOnUiThread { - Assert.assertNotEquals(initialCameraPosition!!.target.latitude, mapboxMap.cameraPosition.target.latitude, 1.0) - Assert.assertNotEquals(initialCameraPosition!!.target.longitude, mapboxMap.cameraPosition.target.longitude, 1.0) + Assert.assertNotEquals(initialCameraPosition!!.target!!.latitude, mapboxMap.cameraPosition.target!!.latitude, 1.0) + Assert.assertNotEquals(initialCameraPosition!!.target!!.longitude, mapboxMap.cameraPosition.target!!.longitude, 1.0) } } @@ -72,14 +72,14 @@ class MapGestureDetectorTest : BaseTest() { validateTestSetup() var initialTarget: LatLng? = null rule.runOnUiThread { - initialTarget = mapboxMap.cameraPosition.target + initialTarget = mapboxMap.cameraPosition.target!! } onView(withId(R.id.mapView)).perform(quickScale(maxHeight / 2f)) rule.runOnUiThread { // camera did not move - Assert.assertEquals(initialTarget!!.latitude, mapboxMap.cameraPosition.target.latitude, 1.0) - Assert.assertEquals(initialTarget!!.longitude, mapboxMap.cameraPosition.target.longitude, 1.0) + Assert.assertEquals(initialTarget!!.latitude, mapboxMap.cameraPosition.target!!.latitude, 1.0) + Assert.assertEquals(initialTarget!!.longitude, mapboxMap.cameraPosition.target!!.longitude, 1.0) } } @@ -100,8 +100,8 @@ class MapGestureDetectorTest : BaseTest() { // move to expected target onView(withId(R.id.mapView)).perform(move(-maxWidth / 2f, -maxHeight / 2f, withVelocity = false)) rule.runOnUiThread { - Assert.assertNotEquals(initialCameraPosition!!.target.latitude, mapboxMap.cameraPosition.target.latitude, 1.0) - Assert.assertNotEquals(initialCameraPosition!!.target.longitude, mapboxMap.cameraPosition.target.longitude, 1.0) + Assert.assertNotEquals(initialCameraPosition!!.target!!.latitude, mapboxMap.cameraPosition.target!!.latitude, 1.0) + Assert.assertNotEquals(initialCameraPosition!!.target!!.longitude, mapboxMap.cameraPosition.target!!.longitude, 1.0) } } @@ -172,8 +172,8 @@ class MapGestureDetectorTest : BaseTest() { // move to expected target onView(withId(R.id.mapView)).perform(move(-maxWidth / 2f, -maxHeight / 2f, withVelocity = false)) rule.runOnUiThread { - Assert.assertNotEquals(initialCameraPosition!!.target.latitude, mapboxMap.cameraPosition.target.latitude, 1.0) - Assert.assertNotEquals(initialCameraPosition!!.target.longitude, mapboxMap.cameraPosition.target.longitude, 1.0) + Assert.assertNotEquals(initialCameraPosition!!.target!!.latitude, mapboxMap.cameraPosition.target!!.latitude, 1.0) + Assert.assertNotEquals(initialCameraPosition!!.target!!.longitude, mapboxMap.cameraPosition.target!!.longitude, 1.0) } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt index 414bf8c4f..aa296aa98 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt @@ -172,8 +172,8 @@ class NativeMapViewTest : AppCenter() { nativeMapView.contentPadding = doubleArrayOf(1.0, 2.0, 3.0, 4.0) nativeMapView.jumpTo(LATLNG_TEST, ZOOM_TEST, PITCH_TEST, BEARING_TEST, PADDING_TEST) val actual = nativeMapView.cameraPosition - assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, DELTA) - assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, DELTA) + assertEquals("Latitude should match", expected.target!!.latitude, actual.target!!.latitude, DELTA) + assertEquals("Longitude should match", expected.target!!.longitude, actual.target!!.longitude, DELTA) assertEquals("Bearing should match", expected.bearing, actual.bearing, DELTA) assertEquals("Pitch should match", expected.tilt, actual.tilt, DELTA) assertEquals("Zoom should match", expected.zoom, actual.zoom, DELTA) @@ -322,11 +322,11 @@ class NativeMapViewTest : AppCenter() { .build() // verify that the lazily set padding is ignored when a value is provided with the camera nativeMapView.contentPadding = doubleArrayOf(1.0, 2.0, 3.0, 4.0) - nativeMapView.flyTo(expected.target, expected.zoom, expected.bearing, expected.tilt, PADDING_TEST, 0) + nativeMapView.flyTo(expected.target!!, expected.zoom, expected.bearing, expected.tilt, PADDING_TEST, 0) val actual = nativeMapView.cameraPosition assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) - assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) - assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Latitude should match", expected.target!!.latitude, actual.target!!.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target!!.longitude, actual.target!!.longitude, TestConstants.LAT_LNG_DELTA) assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) Assert.assertArrayEquals(expected.padding, actual.padding, DELTA) @@ -344,11 +344,11 @@ class NativeMapViewTest : AppCenter() { .build() // verify that the lazily set padding is ignored when a value is provided with the camera nativeMapView.contentPadding = doubleArrayOf(1.0, 2.0, 3.0, 4.0) - nativeMapView.easeTo(expected.target, expected.zoom, expected.bearing, expected.tilt, PADDING_TEST, 0, false) + nativeMapView.easeTo(expected.target!!, expected.zoom, expected.bearing, expected.tilt, PADDING_TEST, 0, false) val actual = nativeMapView.cameraPosition assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) - assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) - assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Latitude should match", expected.target!!.latitude, actual.target!!.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target!!.longitude, actual.target!!.longitude, TestConstants.LAT_LNG_DELTA) assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) Assert.assertArrayEquals(expected.padding, actual.padding, DELTA) @@ -368,8 +368,8 @@ class NativeMapViewTest : AppCenter() { nativeMapView.resetPosition() val actual = nativeMapView.cameraPosition assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) - assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) - assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Latitude should match", expected.target!!.latitude, actual.target!!.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target!!.longitude, actual.target!!.longitude, TestConstants.LAT_LNG_DELTA) assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) Assert.assertArrayEquals(expected.padding, actual.padding, DELTA) @@ -391,8 +391,8 @@ class NativeMapViewTest : AppCenter() { 0.0 ) assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) - assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) - assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Latitude should match", expected.target!!.latitude, actual.target!!.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target!!.longitude, actual.target!!.longitude, TestConstants.LAT_LNG_DELTA) assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) } @@ -409,8 +409,8 @@ class NativeMapViewTest : AppCenter() { nativeMapView.moveBy(12.0, 12.0, 0) val actual = nativeMapView.cameraPosition assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) - assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) - assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Latitude should match", expected.target!!.latitude, actual.target!!.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target!!.longitude, actual.target!!.longitude, TestConstants.LAT_LNG_DELTA) assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/widgets/CompassViewTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/widgets/CompassViewTest.java index 09b242e48..79ef5be05 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/widgets/CompassViewTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/widgets/CompassViewTest.java @@ -29,7 +29,7 @@ public class CompassViewTest extends EspressoTest { } @Test - @Ignore + @Ignore("No explanation given") public void testVisible() { validateTestSetup(); invoke(mapboxMap, (uiController, mapboxMap) -> { @@ -38,7 +38,7 @@ public class CompassViewTest extends EspressoTest { .bearing(45) .zoom(1) .target(new LatLng()) - .build() + .build() )); uiController.loopMainThreadForAtLeast(500); }); @@ -46,7 +46,7 @@ public class CompassViewTest extends EspressoTest { } @Test - @Ignore + @Ignore("No explanation given") public void testClick() { validateTestSetup(); invoke(mapboxMap, (uiController, mapboxMap) -> mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition( @@ -61,7 +61,7 @@ public class CompassViewTest extends EspressoTest { onView(withTagValue(is("compassView"))).check(matches(not(isDisplayed()))); invoke(mapboxMap, (uiController, mapboxMap) -> { CameraPosition cameraPosition = mapboxMap.getCameraPosition(); - assertEquals("Camera bearing should face north, ", 0, cameraPosition.bearing, TestConstants.BEARING_DELTA); + assertEquals("Camera bearing should face north, ", 0, cameraPosition.getBearing(), TestConstants.BEARING_DELTA); }); } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraAnimatorActivity.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraAnimatorActivity.kt index 4324bafc1..dfbc1a84b 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraAnimatorActivity.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraAnimatorActivity.kt @@ -32,9 +32,9 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { super.onCreate(savedInstanceState) setContentView(R.layout.activity_camera_animator) mapView = findViewById(R.id.mapView) as MapView - if (mapView != null) { - mapView!!.onCreate(savedInstanceState) - mapView!!.getMapAsync(this) + if (::mapView.isInitialized) { + mapView.onCreate(savedInstanceState) + mapView.getMapAsync(this) } } @@ -54,8 +54,8 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { .zoom(14.5) .bearing(135.0) .build() - set = createExampleAnimator(mapboxMap!!.cameraPosition, animatedPosition) - set!!.start() + set = createExampleAnimator(mapboxMap.cameraPosition, animatedPosition) + set.start() } } @@ -67,7 +67,7 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { targetPosition: CameraPosition ): Animator { val animatorSet = AnimatorSet() - animatorSet.play(createLatLngAnimator(currentPosition.target, targetPosition.target)) + animatorSet.play(createLatLngAnimator(currentPosition.target!!, targetPosition.target!!)) animatorSet.play(createZoomAnimator(currentPosition.zoom, targetPosition.zoom)) animatorSet.play(createBearingAnimator(currentPosition.bearing, targetPosition.bearing)) animatorSet.play(createTiltAnimator(currentPosition.tilt, targetPosition.tilt)) @@ -80,7 +80,7 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { latLngAnimator.duration = (1000 * ANIMATION_DELAY_FACTOR).toLong() latLngAnimator.interpolator = FastOutSlowInInterpolator() latLngAnimator.addUpdateListener { animation: ValueAnimator -> - mapboxMap!!.moveCamera( + mapboxMap.moveCamera( CameraUpdateFactory.newLatLng((animation.animatedValue as LatLng)) ) } @@ -93,7 +93,7 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { zoomAnimator.startDelay = (600 * ANIMATION_DELAY_FACTOR).toLong() zoomAnimator.interpolator = AnticipateOvershootInterpolator() zoomAnimator.addUpdateListener { animation: ValueAnimator -> - mapboxMap!!.moveCamera( + mapboxMap.moveCamera( CameraUpdateFactory.zoomTo((animation.animatedValue as Float).toDouble()) ) } @@ -107,7 +107,7 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { bearingAnimator.startDelay = (1000 * ANIMATION_DELAY_FACTOR).toLong() bearingAnimator.interpolator = FastOutLinearInInterpolator() bearingAnimator.addUpdateListener { animation: ValueAnimator -> - mapboxMap!!.moveCamera( + mapboxMap.moveCamera( CameraUpdateFactory.bearingTo((animation.animatedValue as Float).toDouble()) ) } @@ -119,7 +119,7 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { tiltAnimator.duration = (1000 * ANIMATION_DELAY_FACTOR).toLong() tiltAnimator.startDelay = (1500 * ANIMATION_DELAY_FACTOR).toLong() tiltAnimator.addUpdateListener { animation: ValueAnimator -> - mapboxMap!!.moveCamera( + mapboxMap.moveCamera( CameraUpdateFactory.tiltTo((animation.animatedValue as Float).toDouble()) ) } @@ -139,7 +139,7 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { } override fun onOptionsItemSelected(item: MenuItem): Boolean { - if (mapboxMap == null) { + if (!::mapboxMap.isInitialized) { return false } if (item.itemId != android.R.id.home) { @@ -151,7 +151,7 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { } private fun resetCameraPosition() { - mapboxMap!!.moveCamera( + mapboxMap.moveCamera( CameraUpdateFactory.newCameraPosition( CameraPosition.Builder() .target(START_LAT_LNG) @@ -176,7 +176,7 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { zoomAnimator.duration = (duration * ANIMATION_DELAY_FACTOR).toLong() zoomAnimator.interpolator = interpolator zoomAnimator.addUpdateListener { animation: ValueAnimator -> - mapboxMap!!.moveCamera( + mapboxMap.moveCamera( CameraUpdateFactory.zoomTo((animation.animatedValue as Float).toDouble()) ) } @@ -188,43 +188,47 @@ class CameraAnimatorActivity : AppCompatActivity(), OnMapReadyCallback { // override fun onStart() { super.onStart() - mapView!!.onStart() + mapView.onStart() } override fun onResume() { super.onResume() - mapView!!.onResume() + mapView.onResume() } override fun onPause() { super.onPause() - mapView!!.onPause() + mapView.onPause() } override fun onStop() { super.onStop() - mapView!!.onStop() + mapView.onStop() for (i in 0 until animators.size()) { animators[animators.keyAt(i)]!!.cancel() } if (set != null) { - set!!.cancel() + set.cancel() } } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - mapView!!.onSaveInstanceState(outState) + mapView.onSaveInstanceState(outState) } override fun onDestroy() { super.onDestroy() - mapView!!.onDestroy() + if (::mapView.isInitialized) { + mapView.onDestroy() + } } override fun onLowMemory() { super.onLowMemory() - mapView!!.onLowMemory() + if (::mapView.isInitialized) { + mapView.onLowMemory() + } } /** Helper class to evaluate LatLng objects with a ValueAnimator */ diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraPositionActivity.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraPositionActivity.kt index 2564ac917..331d53432 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraPositionActivity.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/CameraPositionActivity.kt @@ -28,8 +28,7 @@ import com.mapbox.mapboxsdk.testapp.R import timber.log.Timber /** Test activity showcasing how to listen to camera change events. */ -class CameraPositionActivity : - FragmentActivity(), OnMapReadyCallback, View.OnClickListener, OnMapLongClickListener { +class CameraPositionActivity : FragmentActivity(), OnMapReadyCallback, View.OnClickListener, OnMapLongClickListener { private lateinit var mapView: MapView private lateinit var mapboxMap: MapboxMap private lateinit var fab: FloatingActionButton @@ -56,7 +55,7 @@ class CameraPositionActivity : toggleLogCameraChanges() // listen to long click events to toggle logging camera changes - mapboxMap!!.addOnMapLongClickListener(this) + mapboxMap.addOnMapLongClickListener(this) } } @@ -68,8 +67,7 @@ class CameraPositionActivity : @SuppressLint("InflateParams") override fun onClick(view: View) { val context = view.context - val dialogContent = - LayoutInflater.from(context).inflate(R.layout.dialog_camera_position, null) + val dialogContent = LayoutInflater.from(context).inflate(R.layout.dialog_camera_position, null) val builder = AlertDialog.Builder(context) builder.setTitle(R.string.dialog_camera_position) builder.setView(onInflateDialogContent(dialogContent)) @@ -82,49 +80,51 @@ class CameraPositionActivity : private fun toggleLogCameraChanges() { logCameraChanges = !logCameraChanges if (logCameraChanges) { - mapboxMap!!.addOnCameraIdleListener(idleListener) - mapboxMap!!.addOnCameraMoveCancelListener(moveCanceledListener) - mapboxMap!!.addOnCameraMoveListener(moveListener) - mapboxMap!!.addOnCameraMoveStartedListener(moveStartedListener) + mapboxMap.addOnCameraIdleListener(idleListener) + mapboxMap.addOnCameraMoveCancelListener(moveCanceledListener) + mapboxMap.addOnCameraMoveListener(moveListener) + mapboxMap.addOnCameraMoveStartedListener(moveStartedListener) } else { - mapboxMap!!.removeOnCameraIdleListener(idleListener) - mapboxMap!!.removeOnCameraMoveCancelListener(moveCanceledListener) - mapboxMap!!.removeOnCameraMoveListener(moveListener) - mapboxMap!!.removeOnCameraMoveStartedListener(moveStartedListener) + mapboxMap.removeOnCameraIdleListener(idleListener) + mapboxMap.removeOnCameraMoveCancelListener(moveCanceledListener) + mapboxMap.removeOnCameraMoveListener(moveListener) + mapboxMap.removeOnCameraMoveStartedListener(moveStartedListener) } } override fun onStart() { super.onStart() - mapView!!.onStart() + mapView.onStart() } override fun onResume() { super.onResume() - mapView!!.onResume() + mapView.onResume() } override fun onPause() { super.onPause() - mapView!!.onPause() + mapView.onPause() } override fun onStop() { super.onStop() - mapView!!.onStop() + mapView.onStop() } override fun onDestroy() { super.onDestroy() - if (mapboxMap != null) { - mapboxMap!!.removeOnMapLongClickListener(this) + if (::mapboxMap.isInitialized) { + mapboxMap.removeOnMapLongClickListener(this) + } + if (::mapView.isInitialized) { + mapView.onDestroy() } - mapView!!.onDestroy() } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - mapView!!.onSaveInstanceState(outState) + mapView.onSaveInstanceState(outState) } private fun onInflateDialogContent(view: View): View { @@ -136,13 +136,7 @@ class CameraPositionActivity : return view } - private fun linkTextView( - view: View, - @IdRes textViewRes: Int, - @IdRes seekBarRes: Int, - listener: ValueChangeListener, - defaultValue: Int - ) { + private fun linkTextView(view: View, @IdRes textViewRes: Int, @IdRes seekBarRes: Int, listener: ValueChangeListener, defaultValue: Int) { val value = view.findViewById(textViewRes) as TextView val seekBar = view.findViewById(seekBarRes) as SeekBar listener.setLinkedValueView(value) @@ -152,42 +146,30 @@ class CameraPositionActivity : private val idleListener = OnCameraIdleListener { Timber.e("OnCameraIdle") - fab!!.setColorFilter( + fab.setColorFilter( ContextCompat.getColor(this@CameraPositionActivity, android.R.color.holo_green_dark) ) } private val moveListener = OnCameraMoveListener { Timber.e("OnCameraMove") - fab!!.setColorFilter( - ContextCompat.getColor( - this@CameraPositionActivity, - android.R.color.holo_orange_dark - ) + fab.setColorFilter( + ContextCompat.getColor(this@CameraPositionActivity, android.R.color.holo_orange_dark) ) } private val moveCanceledListener = OnCameraMoveCanceledListener { Timber.e("OnCameraMoveCanceled") } - private val moveStartedListener: OnCameraMoveStartedListener = - object : OnCameraMoveStartedListener { - private val REASONS = - arrayOf( - "REASON_API_GESTURE", - "REASON_DEVELOPER_ANIMATION", - "REASON_API_ANIMATION" - ) + private val moveStartedListener: OnCameraMoveStartedListener = object : OnCameraMoveStartedListener { + private val REASONS = arrayOf("REASON_API_GESTURE", "REASON_DEVELOPER_ANIMATION", "REASON_API_ANIMATION") - override fun onCameraMoveStarted(reason: Int) { - // reason ranges from 1 <-> 3 - fab!!.setColorFilter( - ContextCompat.getColor( - this@CameraPositionActivity, - android.R.color.holo_red_dark - ) - ) - Timber.e("OnCameraMoveStarted: %s", REASONS[reason - 1]) - } + override fun onCameraMoveStarted(reason: Int) { + // reason ranges from 1 <-> 3 + fab.setColorFilter( + ContextCompat.getColor(this@CameraPositionActivity, android.R.color.holo_red_dark) + ) + Timber.e("OnCameraMoveStarted: %s", REASONS[reason - 1]) } + } private open inner class ValueChangeListener : OnSeekBarChangeListener { protected var textView: TextView? = null @@ -208,58 +190,20 @@ class CameraPositionActivity : } } - private class DialogClickListener( - private val mapboxMap: MapboxMap?, - private val dialogContent: View - ) : DialogInterface.OnClickListener { + private class DialogClickListener(private val mapboxMap: MapboxMap?, private val dialogContent: View) : DialogInterface.OnClickListener { override fun onClick(dialog: DialogInterface, which: Int) { - val latitude = - (dialogContent.findViewById(R.id.value_lat) as TextView) - .text - .toString() - .toDouble() - val longitude = - (dialogContent.findViewById(R.id.value_lon) as TextView) - .text - .toString() - .toDouble() - val zoom = - (dialogContent.findViewById(R.id.value_zoom) as TextView) - .text - .toString() - .toDouble() - val bearing = - (dialogContent.findViewById(R.id.value_bearing) as TextView) - .text - .toString() - .toDouble() - val tilt = - (dialogContent.findViewById(R.id.value_tilt) as TextView) - .text - .toString() - .toDouble() - if (latitude < GeometryConstants.MIN_LATITUDE || - latitude > GeometryConstants.MAX_LATITUDE - ) { - Toast.makeText( - dialogContent.context, - "latitude value must be set " + - " between " + - GeometryConstants.MIN_LATITUDE + - " and " + - GeometryConstants.MAX_LATITUDE, - Toast.LENGTH_SHORT - ) - .show() + val latitude = (dialogContent.findViewById(R.id.value_lat) as TextView).text.toString().toDouble() + val longitude = (dialogContent.findViewById(R.id.value_lon) as TextView).text.toString().toDouble() + val zoom = (dialogContent.findViewById(R.id.value_zoom) as TextView).text.toString().toDouble() + val bearing = (dialogContent.findViewById(R.id.value_bearing) as TextView).text.toString().toDouble() + val tilt = (dialogContent.findViewById(R.id.value_tilt) as TextView).text.toString().toDouble() + if (latitude < GeometryConstants.MIN_LATITUDE || latitude > GeometryConstants.MAX_LATITUDE) { + Toast.makeText(dialogContent.context, "latitude value must be set between " + GeometryConstants.MIN_LATITUDE + " and " + GeometryConstants.MAX_LATITUDE, Toast.LENGTH_SHORT).show() return } - val cameraPosition = - CameraPosition.Builder() - .target(LatLng(latitude, longitude)) - .zoom(zoom) - .bearing(bearing) - .tilt(tilt) - .build() + + val cameraPosition = CameraPosition.Builder().target(LatLng(latitude, longitude)).zoom(zoom).bearing(bearing).tilt(tilt).build() + mapboxMap!!.animateCamera( CameraUpdateFactory.newCameraPosition(cameraPosition), 5000,