diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp index 122ec794d..56de8ff1b 100644 --- a/android/cpp/jni.cpp +++ b/android/cpp/jni.cpp @@ -722,7 +722,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } - lonLatClass = env->FindClass("com/mapbox/mapboxgl/lib/LonLat"); + lonLatClass = env->FindClass("com/mapbox/mapboxgl/lib/geometry/LatLng"); if (lonLatClass == nullptr) { env->ExceptionDescribe(); return JNI_ERR; @@ -734,19 +734,19 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } - lonLatLonId = env->GetFieldID(lonLatClass, "lon", "D"); + lonLatLonId = env->GetFieldID(lonLatClass, "longitude", "D"); if (lonLatLonId == nullptr) { env->ExceptionDescribe(); return JNI_ERR; } - lonLatLatId = env->GetFieldID(lonLatClass, "lat", "D"); + lonLatLatId = env->GetFieldID(lonLatClass, "latitude", "D"); if (lonLatLatId == nullptr) { env->ExceptionDescribe(); return JNI_ERR; } - lonLatZoomClass = env->FindClass("com/mapbox/mapboxgl/lib/LonLatZoom"); + lonLatZoomClass = env->FindClass("com/mapbox/mapboxgl/lib/LatLngZoom"); if (lonLatClass == nullptr) { env->ExceptionDescribe(); return JNI_ERR; @@ -758,13 +758,13 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } - lonLatZoomLonId = env->GetFieldID(lonLatZoomClass, "lon", "D"); + lonLatZoomLonId = env->GetFieldID(lonLatZoomClass, "longitude", "D"); if (lonLatZoomLonId == nullptr) { env->ExceptionDescribe(); return JNI_ERR; } - lonLatZoomLatId = env->GetFieldID(lonLatZoomClass, "lat", "D"); + lonLatZoomLatId = env->GetFieldID(lonLatZoomClass, "latitude", "D"); if (lonLatZoomLatId == nullptr) { env->ExceptionDescribe(); return JNI_ERR; @@ -892,9 +892,9 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { reinterpret_cast(&nativeGetAccessToken)}, {"nativeCancelTransitions", "(J)V", reinterpret_cast(&nativeCancelTransitions)}, {"nativeMoveBy", "(JDDJ)V", reinterpret_cast(&nativeMoveBy)}, - {"nativeSetLonLat", "(JLcom/mapbox/mapboxgl/lib/LonLat;J)V", + {"nativeSetLonLat", "(JLcom/mapbox/mapboxgl/lib/geometry/LatLng;J)V", reinterpret_cast(&nativeSetLonLat)}, - {"nativeGetLonLat", "(J)Lcom/mapbox/mapboxgl/lib/LonLat;", + {"nativeGetLonLat", "(J)Lcom/mapbox/mapboxgl/lib/geometry/LatLng;", reinterpret_cast(&nativeGetLonLat)}, {"nativeStartPanning", "(J)V", reinterpret_cast(&nativeStartPanning)}, {"nativeStopPanning", "(J)V", reinterpret_cast(&nativeStopPanning)}, @@ -904,9 +904,9 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { {"nativeGetScale", "(J)D", reinterpret_cast(&nativeGetScale)}, {"nativeSetZoom", "(JDJ)V", reinterpret_cast(&nativeSetZoom)}, {"nativeGetZoom", "(J)D", reinterpret_cast(&nativeGetZoom)}, - {"nativeSetLonLatZoom", "(JLcom/mapbox/mapboxgl/lib/LonLatZoom;J)V", + {"nativeSetLonLatZoom", "(JLcom/mapbox/mapboxgl/lib/LatLngZoom;J)V", reinterpret_cast(&nativeSetLonLatZoom)}, - {"nativeGetLonLatZoom", "(J)Lcom/mapbox/mapboxgl/lib/LonLatZoom;", + {"nativeGetLonLatZoom", "(J)Lcom/mapbox/mapboxgl/lib/LatLngZoom;", reinterpret_cast(&nativeGetLonLatZoom)}, {"nativeResetZoom", "(J)V", reinterpret_cast(&nativeResetZoom)}, {"nativeStartPanning", "(J)V", reinterpret_cast(&nativeStartScaling)}, diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LonLatZoom.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LatLngZoom.java similarity index 61% rename from android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LonLatZoom.java rename to android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LatLngZoom.java index 7b379863e..5cbb48d36 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LonLatZoom.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LatLngZoom.java @@ -2,32 +2,33 @@ package com.mapbox.mapboxgl.lib; import android.os.Parcel; import android.os.Parcelable; +import com.mapbox.mapboxgl.lib.geometry.LatLng; -public class LonLatZoom extends LonLat implements Parcelable { +public class LatLngZoom extends LatLng implements Parcelable { - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - public LonLatZoom createFromParcel(Parcel in) { - return new LonLatZoom(in); + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public LatLngZoom createFromParcel(Parcel in) { + return new LatLngZoom(in); } - public LonLatZoom[] newArray(int size) { - return new LonLatZoom[size]; + public LatLngZoom[] newArray(int size) { + return new LatLngZoom[size]; } }; private double zoom; - public LonLatZoom(double lon, double lat, double zoom) { - super(lon, lat); + public LatLngZoom(double lat, double lon, double zoom) { + super(lat, lon); this.zoom = zoom; } - public LonLatZoom(LonLat lonLat, double zoom) { - super(lonLat.getLon(), lonLat.getLat()); + public LatLngZoom(LatLng latLng, double zoom) { + super(latLng.getLatitude(), latLng.getLongitude()); this.zoom = zoom; } - private LonLatZoom(Parcel in) { + private LatLngZoom(Parcel in) { super(in); zoom = in.readDouble(); } @@ -61,14 +62,13 @@ public class LonLatZoom extends LonLat implements Parcelable { if (getClass() != obj.getClass()) { return false; } - LonLatZoom other = (LonLatZoom) obj; + LatLngZoom other = (LatLngZoom) obj; return super.equals(obj) && Double.doubleToLongBits(zoom) == Double.doubleToLongBits(other.zoom); } @Override public String toString() { - return "LonLatZoom [lon=" + super.getLon() + ", lat=" + super.getLat() + ", zoom=" + zoom - + "]"; + return "LatLngZoom [lat=" + super.getLatitude() + ", lon=" + super.getLongitude() + ", zoom=" + zoom + "]"; } @Override diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LonLat.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LonLat.java deleted file mode 100644 index accc71732..000000000 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/LonLat.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.mapbox.mapboxgl.lib; - -import android.os.Parcel; -import android.os.Parcelable; - -public class LonLat implements Parcelable { - - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - public LonLat createFromParcel(Parcel in) { - return new LonLat(in); - } - - public LonLat[] newArray(int size) { - return new LonLat[size]; - } - }; - - private double lon; - private double lat; - - public LonLat(double lon, double lat) { - this.lon = lon; - this.lat = lat; - } - - protected LonLat(Parcel in) { - lon = in.readDouble(); - lat = in.readDouble(); - } - - public double getLon() { - return lon; - } - - public void setLon(double lon) { - this.lon = lon; - } - - public double getLat() { - return lat; - } - - public void setLat(double lat) { - this.lat = lat; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - long temp; - temp = Double.doubleToLongBits(lat); - result = prime * result + (int) (temp ^ (temp >>> 32)); - temp = Double.doubleToLongBits(lon); - result = prime * result + (int) (temp ^ (temp >>> 32)); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - LonLat other = (LonLat) obj; - return Double.doubleToLongBits(lat) == Double.doubleToLongBits(other.lat) && Double.doubleToLongBits(lon) == Double.doubleToLongBits(other.lon); - } - - @Override - public String toString() { - return "LonLat [lon=" + lon + ", lat=" + lat + "]"; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel out, int flags) { - out.writeDouble(lon); - out.writeDouble(lat); - } - -} diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/MapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/MapView.java index 73b64711e..d157cc07f 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/MapView.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/MapView.java @@ -29,6 +29,7 @@ import android.widget.ZoomButtonsController; import com.almeros.android.multitouch.gesturedetectors.RotateGestureDetector; import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector; +import com.mapbox.mapboxgl.lib.geometry.LatLng; import org.apache.commons.validator.routines.UrlValidator; @@ -154,7 +155,7 @@ public class MapView extends SurfaceView { try { double centerLongitude = typedArray.getFloat(R.styleable.MapView_centerLongitude, 0.0f); double centerLatitude = typedArray.getFloat(R.styleable.MapView_centerLatitude, 0.0f); - LonLat centerCoordinate = new LonLat(centerLongitude, centerLatitude); + LatLng centerCoordinate = new LatLng(centerLongitude, centerLatitude); setCenterCoordinate(centerCoordinate); setZoomLevel(typedArray.getFloat(R.styleable.MapView_zoomLevel, 0.0f)); // need to set zoom level first because of limitation on rotating when zoomed out setDirection(typedArray.getFloat(R.styleable.MapView_direction, 0.0f)); @@ -211,27 +212,27 @@ public class MapView extends SurfaceView { // Property methods // - public LonLat getCenterCoordinate() { - return mNativeMapView.getLonLat(); + public LatLng getCenterCoordinate() { + return mNativeMapView.getLatLng(); } - public void setCenterCoordinate(LonLat centerCoordinate) { + public void setCenterCoordinate(LatLng centerCoordinate) { setCenterCoordinate(centerCoordinate, false); } - public void setCenterCoordinate(LonLat centerCoordinate, boolean animated) { + public void setCenterCoordinate(LatLng centerCoordinate, boolean animated) { long duration = animated ? ANIMATION_DURATION : 0; - mNativeMapView.setLonLat(centerCoordinate, duration); + mNativeMapView.setLatLng(centerCoordinate, duration); } - public void setCenterCoordinate(LonLatZoom centerCoordinate) { + public void setCenterCoordinate(LatLngZoom centerCoordinate) { setCenterCoordinate(centerCoordinate, false); } - public void setCenterCoordinate(LonLatZoom centerCoordinate, + public void setCenterCoordinate(LatLngZoom centerCoordinate, boolean animated) { long duration = animated ? ANIMATION_DURATION : 0; - mNativeMapView.setLonLatZoom(centerCoordinate, duration); + mNativeMapView.setLatLngZoom(centerCoordinate, duration); } public double getDirection() { @@ -398,7 +399,7 @@ public class MapView extends SurfaceView { public void onCreate(Bundle savedInstanceState) { Log.v(TAG, "onCreate"); if (savedInstanceState != null) { - setCenterCoordinate((LonLat) savedInstanceState.getParcelable(STATE_CENTER_COORDINATE)); + setCenterCoordinate((LatLng) savedInstanceState.getParcelable(STATE_CENTER_COORDINATE)); setZoomLevel(savedInstanceState.getDouble(STATE_ZOOM_LEVEL)); // need to set zoom level first because of limitation on rotating when zoomed out setDirection(savedInstanceState.getDouble(STATE_CENTER_DIRECTION)); setDirection(savedInstanceState.getDouble(STATE_DIRECTION)); diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/NativeMapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/NativeMapView.java index de8e8df75..17a95fa32 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/NativeMapView.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/NativeMapView.java @@ -2,6 +2,8 @@ package com.mapbox.mapboxgl.lib; import android.view.Surface; +import com.mapbox.mapboxgl.lib.geometry.LatLng; + import java.util.List; // Class that wraps the native methods for convenience @@ -241,15 +243,15 @@ class NativeMapView { nativeMoveBy(mNativeMapViewPtr, dx, dy, duration); } - public void setLonLat(LonLat lonLat) { - setLonLat(lonLat, 0); + public void setLatLng(LatLng latLng) { + setLatLng(latLng, 0); } - public void setLonLat(LonLat lonLat, long duration) { - nativeSetLonLat(mNativeMapViewPtr, lonLat, duration); + public void setLatLng(LatLng latLng, long duration) { + nativeSetLonLat(mNativeMapViewPtr, latLng, duration); } - public LonLat getLonLat() { + public LatLng getLatLng() { return nativeGetLonLat(mNativeMapViewPtr); } @@ -305,15 +307,15 @@ class NativeMapView { return nativeGetZoom(mNativeMapViewPtr); } - public void setLonLatZoom(LonLatZoom lonLatZoom) { - setLonLatZoom(lonLatZoom, 0); + public void setLatLngZoom(LatLngZoom lonLatZoom) { + setLatLngZoom(lonLatZoom, 0); } - public void setLonLatZoom(LonLatZoom lonLatZoom, long duration) { + public void setLatLngZoom(LatLngZoom lonLatZoom, long duration) { nativeSetLonLatZoom(mNativeMapViewPtr, lonLatZoom, duration); } - public LonLatZoom getLonLatZoom() { + public LatLngZoom getLatLngZoom() { return nativeGetLonLatZoom(mNativeMapViewPtr); } @@ -488,10 +490,10 @@ class NativeMapView { private native void nativeMoveBy(long nativeMapViewPtr, double dx, double dy, long duration); - private native void nativeSetLonLat(long nativeMapViewPtr, LonLat lonLat, + private native void nativeSetLonLat(long nativeMapViewPtr, LatLng latLng, long duration); - private native LonLat nativeGetLonLat(long nativeMapViewPtr); + private native LatLng nativeGetLonLat(long nativeMapViewPtr); private native void nativeStartPanning(long nativeMapViewPtr); @@ -513,9 +515,9 @@ class NativeMapView { private native double nativeGetZoom(long nativeMapViewPtr); private native void nativeSetLonLatZoom(long nativeMapViewPtr, - LonLatZoom lonLatZoom, long duration); + LatLngZoom lonLatZoom, long duration); - private native LonLatZoom nativeGetLonLatZoom(long nativeMapViewPtr); + private native LatLngZoom nativeGetLonLatZoom(long nativeMapViewPtr); private native void nativeResetZoom(long nativeMapViewPtr); diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/constants/GeoConstants.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/constants/GeoConstants.java new file mode 100644 index 000000000..d7f674d7b --- /dev/null +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/constants/GeoConstants.java @@ -0,0 +1,10 @@ +package com.mapbox.mapboxgl.lib.constants; + +public interface GeoConstants { + // http://en.wikipedia.org/wiki/Earth_radius#Equatorial_radius + public static final int RADIUS_EARTH_METERS = 6378137; + public static final double MIN_LATITUDE = -85.05112878; + public static final double MAX_LATITUDE = 85.05112878; + public static final double MIN_LONGITUDE = -180; + public static final double MAX_LONGITUDE = 180; +} diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/constants/MathConstants.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/constants/MathConstants.java new file mode 100644 index 000000000..3e4a3dea5 --- /dev/null +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/constants/MathConstants.java @@ -0,0 +1,8 @@ +package com.mapbox.mapboxgl.lib.constants; + +public interface MathConstants { + public static final float DEG2RAD = (float) (Math.PI / 180.0); + public static final float RAD2DEG = (float) (180.0 / Math.PI); + + public static final float PI = (float) Math.PI; +} diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/BoundingBox.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/BoundingBox.java new file mode 100644 index 000000000..3a8141e3c --- /dev/null +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/BoundingBox.java @@ -0,0 +1,291 @@ +package com.mapbox.mapboxgl.lib.geometry; + +import android.os.Parcel; +import android.os.Parcelable; +import java.io.Serializable; +import java.util.List; + +/** + * A rectangular geographical area defined in latitude and longitude units. + */ +public final class BoundingBox implements Parcelable, Serializable { + + static final long serialVersionUID = 2L; + + private final double mLatNorth; + private final double mLatSouth; + private final double mLonEast; + private final double mLonWest; + + private final boolean mIsValid; + + /** + * Construct a new bounding box based on its corners, given in NESW + * order. + * + * @param north Northern Coordinate + * @param east Eastern Coordinate + * @param south Southern Coordinate + * @param west Western Coordinate + */ + public BoundingBox(final double north, final double east, final double south, final double west) { + this.mLatNorth = north; + this.mLonEast = east; + this.mLatSouth = south; + this.mLonWest = west; + this.mIsValid = ((this.mLonWest < this.mLonEast) && (this.mLatNorth > this.mLatSouth)); + } + + /** + * Construct a new bounding box based on its corners, given in NESW + * order. + * + * @param northEast Coordinate + * @param southWest Coordinate + */ + public BoundingBox(final LatLng northEast, final LatLng southWest) { + this(northEast.getLatitude(), northEast.getLongitude(), southWest.getLatitude(), southWest.getLongitude()); + } + + /** + * Create a bounding box from another bounding box + * + * @param other the other bounding box + */ + public BoundingBox(final BoundingBox other) { + this.mLatNorth = other.getLatNorth(); + this.mLonEast = other.getLonEast(); + this.mLatSouth = other.getLatSouth(); + this.mLonWest = other.getLonWest(); + this.mIsValid = other.isValid(); + } + + /** + * Create a new BoundingBox with no size centered at 0, 0, also known as null island + */ + public BoundingBox() { + this(0, 0, 0, 0); + } + + /** + * Calculates the centerpoint of this bounding box by simple interpolation and returns + * it as a point. This is a non-geodesic calculation which is not the geographic center. + * + * @return LatLng center of this BoundingBox + */ + public LatLng getCenter() { + return new LatLng((this.mLatNorth + this.mLatSouth) / 2, + (this.mLonEast + this.mLonWest) / 2); + } + + public double getLatNorth() { + return this.mLatNorth; + } + + public double getLatSouth() { + return this.mLatSouth; + } + + public double getLonEast() { + return this.mLonEast; + } + + public double getLonWest() { + return this.mLonWest; + } + + public boolean isValid() { + return this.mIsValid; + } + + /** + * Get the absolute distance, in degrees, between the north and + * south boundaries of this bounding box + * + * @return Span distance + */ + public double getLatitudeSpan() { + return Math.abs(this.mLatNorth - this.mLatSouth); + } + + /** + * Get the absolute distance, in degrees, between the west and + * east boundaries of this bounding box + * + * @return Span distance + */ + public double getLongitudeSpan() { + return Math.abs(this.mLonEast - this.mLonWest); + } + + @Override + public String toString() { + return new StringBuffer().append("N:") + .append(this.mLatNorth) + .append("; E:") + .append(this.mLonEast) + .append("; S:") + .append(this.mLatSouth) + .append("; W:") + .append(this.mLonWest) + .toString(); + } + + /** + * Constructs a bounding box that contains all of a list of LatLng + * objects. Empty lists will yield invalid bounding boxes. + * + * @param latLngs List of LatLng objects + * @return BoundingBox + */ + public static BoundingBox fromLatLngs(final List latLngs) { + double minLat = 90, + minLon = 180, + maxLat = -90, + maxLon = -180; + + for (final ILatLng gp : latLngs) { + final double latitude = gp.getLatitude(); + final double longitude = gp.getLongitude(); + + minLat = Math.min(minLat, latitude); + minLon = Math.min(minLon, longitude); + maxLat = Math.max(maxLat, latitude); + maxLon = Math.max(maxLon, longitude); + } + + return new BoundingBox(maxLat, maxLon, minLat, minLon); + } + + /** + * Determines whether this bounding box matches another one via coordinates. + * + * @param other another bounding box + * @return a boolean indicating whether the bounding boxes are equal + */ + public boolean equals(final BoundingBox other) { + + if (other == null) { + return false; + } + if (other == this) { + return true; + } + + return mLatNorth == other.getLatNorth() + && mLatSouth == other.getLatSouth() + && mLonEast == other.getLonEast() + && mLonWest == other.getLonWest(); + } + + /** + * Determines whether this bounding box contains a point and the point + * does not touch its boundary. + * + * @param pGeoPoint the point which may be contained + * @return true, if the point is contained within the box. + */ + public boolean contains(final ILatLng pGeoPoint) { + final double latitude = pGeoPoint.getLatitude(); + final double longitude = pGeoPoint.getLongitude(); + return ((latitude < this.mLatNorth) && (latitude > this.mLatSouth)) && ((longitude + < this.mLonEast) && (longitude > this.mLonWest)); + } + + /** + * Returns a new BoundingBox that stretches to contain both this and another BoundingBox. + * + * @param box BoundingBox to add + * @return BoundingBox + */ + public BoundingBox union(BoundingBox box) { + return union(box.getLatNorth(), box.getLonEast(), box.getLatSouth(), box.getLonWest()); + } + + /** + * Returns a new BoundingBox that stretches to include another bounding box, + * given by corner points. + * + * @param pLatNorth Northern Coordinate + * @param pLonEast Eastern Coordinate + * @param pLatSouth Southern Coordinate + * @param pLonWest Western Coordinate + * @return BoundingBox + */ + public BoundingBox union(final double pLatNorth, final double pLonEast, final double pLatSouth, + final double pLonWest) { + return new BoundingBox((this.mLatNorth < pLatNorth) ? pLatNorth : this.mLatNorth, + (this.mLonEast < pLonEast) ? pLonEast : this.mLonEast, + (this.mLatSouth > pLatSouth) ? pLatSouth : this.mLatSouth, + (this.mLonWest > pLonWest) ? pLonWest : this.mLonWest); + } + + /** + * Returns a new BoundingBox that is the intersection of this with another box + * + * @param box BoundingBox to intersect with + * @return BoundingBox + */ + public BoundingBox intersect(BoundingBox box) { + double maxLonWest = Math.max(this.mLonWest, box.getLonWest()); + double minLonEast = Math.min(this.mLonEast, box.getLonEast()); + double maxLatNorth = Math.min(this.mLatNorth, box.getLatNorth()); + double minLatSouth = Math.max(this.mLatSouth, box.getLatSouth()); + return new BoundingBox(maxLatNorth, minLonEast, minLatSouth, maxLonWest); + } + + /** + * Returns a new BoundingBox that is the intersection of this with another box + * + * @param north Northern Coordinate + * @param east Eastern Coordinate + * @param south Southern Coordinate + * @param west Western Coordinate + * @return BoundingBox + */ + public BoundingBox intersect(double north, double east, double south, double west) { + return intersect(new BoundingBox(north, east, south, west)); + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + @Override + public BoundingBox createFromParcel(final Parcel in) { + return readFromParcel(in); + } + + @Override + public BoundingBox[] newArray(final int size) { + return new BoundingBox[size]; + } + }; + + @Override + public int hashCode() { + return (int) ((mLatNorth + 90) + + ((mLatSouth + 90) * 1000) + + ((mLonEast + 180) * 1000000) + + ((mLonEast + 180) * 1000000000)); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(final Parcel out, final int arg1) { + out.writeDouble(this.mLatNorth); + out.writeDouble(this.mLonEast); + out.writeDouble(this.mLatSouth); + out.writeDouble(this.mLonWest); + } + + private static BoundingBox readFromParcel(final Parcel in) { + final double latNorth = in.readDouble(); + final double lonEast = in.readDouble(); + final double latSouth = in.readDouble(); + final double lonWest = in.readDouble(); + return new BoundingBox(latNorth, lonEast, latSouth, lonWest); + } +} diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/CoordinateRegion.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/CoordinateRegion.java new file mode 100644 index 000000000..1d2409b46 --- /dev/null +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/CoordinateRegion.java @@ -0,0 +1,30 @@ +package com.mapbox.mapboxgl.lib.geometry; + +/** + * Implementation of iOS MKCoordinateRegion + */ +public class CoordinateRegion { + private LatLng center; + private CoordinateSpan span; + + public CoordinateRegion(LatLng center, CoordinateSpan span) { + this.center = center; + this.span = span; + } + + public LatLng getCenter() { + return center; + } + + public void setCenter(LatLng center) { + this.center = center; + } + + public CoordinateSpan getSpan() { + return span; + } + + public void setSpan(CoordinateSpan span) { + this.span = span; + } +} diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/CoordinateSpan.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/CoordinateSpan.java new file mode 100644 index 000000000..4fa239f66 --- /dev/null +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/CoordinateSpan.java @@ -0,0 +1,31 @@ +package com.mapbox.mapboxgl.lib.geometry; + +/** + * Implementation of iOS MKCoordinateSpan + */ +public final class CoordinateSpan { + + private double latitudeSpan; + private double longitudeSpan; + + public CoordinateSpan(double latitudeSpan, double longitudeSpan) { + this.latitudeSpan = latitudeSpan; + this.longitudeSpan = longitudeSpan; + } + + public double getLatitudeSpan() { + return latitudeSpan; + } + + public void setLatitudeSpan(double latitudeSpan) { + this.latitudeSpan = latitudeSpan; + } + + public double getLongitudeSpan() { + return longitudeSpan; + } + + public void setLongitudeSpan(double longitudeSpan) { + this.longitudeSpan = longitudeSpan; + } +} diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/ILatLng.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/ILatLng.java new file mode 100644 index 000000000..c22a8ae39 --- /dev/null +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/ILatLng.java @@ -0,0 +1,12 @@ +package com.mapbox.mapboxgl.lib.geometry; + +/** + * A Latitude, Longitude point. + */ +public interface ILatLng { + double getLatitude(); + + double getLongitude(); + + double getAltitude(); +} diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/LatLng.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/LatLng.java new file mode 100644 index 000000000..89c51f2da --- /dev/null +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/lib/geometry/LatLng.java @@ -0,0 +1,148 @@ +package com.mapbox.mapboxgl.lib.geometry; + +import android.location.Location; +import android.os.Parcel; +import android.os.Parcelable; +import com.mapbox.mapboxgl.lib.constants.GeoConstants; +import com.mapbox.mapboxgl.lib.constants.MathConstants; +import java.io.Serializable; + +public class LatLng implements ILatLng, GeoConstants, MathConstants, Parcelable, Serializable { + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public LatLng createFromParcel(Parcel in) { + return new LatLng(in); + } + + public LatLng[] newArray(int size) { + return new LatLng[size]; + } + }; + + private double longitude; + private double latitude; + private double altitude = 0f; + + /** + * Construct a new latitude, longitude point given float arguments + * @param latitude Latitude in degrees + * @param longitude Longitude in degress + */ + public LatLng(double latitude, double longitude) { + this.latitude = latitude; + this.longitude = longitude; + } + + /** + * Construct a new latitude, longitude, altitude point given float arguments + * @param latitude Latitude in degrees + * @param longitude Longitude in degress + * @param altitude Altitude in meters + */ + public LatLng(final double latitude, final double longitude, final double altitude) { + this.latitude = latitude; + this.longitude = longitude; + this.altitude = altitude; + } + + /** + * Transform a Location into a LatLng point + * @param location Android Location + */ + public LatLng(final Location location) { + this(location.getLatitude(), location.getLongitude(), location.getAltitude()); + } + + /** + * Clone an existing latitude longitude point + * @param aLatLng LatLng + */ + public LatLng(final LatLng aLatLng) { + this.latitude = aLatLng.latitude; + this.longitude = aLatLng.longitude; + this.altitude = aLatLng.altitude; + } + + protected LatLng(Parcel in) { + longitude = in.readDouble(); + latitude = in.readDouble(); + altitude = in.readDouble(); + } + + @Override + public double getLatitude() { + return latitude; + } + + @Override + public double getLongitude() { + return longitude; + } + + @Override + public double getAltitude() { + return altitude; + } + + @Override + public boolean equals(final Object obj) { + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + if (!obj.getClass().equals(this.getClass())) { + return false; + } + final LatLng rhs = (LatLng) obj; + return rhs.latitude == this.latitude + && rhs.longitude == this.longitude + && rhs.altitude == this.altitude; + } + + @Override + public int hashCode() { + return (int) (37.0 * (17.0 * latitude * 1E6d + longitude * 1E6d) + altitude); + } + + @Override + public String toString() { + return "LatLng [longitude=" + longitude + ", latitude=" + latitude + "]"; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + out.writeDouble(longitude); + out.writeDouble(latitude); + out.writeDouble(altitude); + } + + /** + * Calculate distance between two points + * @param other Other LatLng to compare to + * @return distance in meters + */ + public int distanceTo(final LatLng other) { + + final double a1 = DEG2RAD * this.latitude; + final double a2 = DEG2RAD * this.longitude; + final double b1 = DEG2RAD * other.getLatitude(); + final double b2 = DEG2RAD * other.getLongitude(); + + final double cosa1 = Math.cos(a1); + final double cosb1 = Math.cos(b1); + + final double t1 = cosa1 * Math.cos(a2) * cosb1 * Math.cos(b2); + final double t2 = cosa1 * Math.sin(a2) * cosb1 * Math.sin(b2); + final double t3 = Math.sin(a1) * Math.sin(b1); + final double tt = Math.acos(t1 + t2 + t3); + + return (int) (RADIUS_EARTH_METERS * tt); + } +} diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/app/MainActivity.java b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/app/MainActivity.java index c4b1f496d..e8b1d578b 100644 --- a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/app/MainActivity.java +++ b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/app/MainActivity.java @@ -15,7 +15,7 @@ import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView; -import com.mapbox.mapboxgl.lib.LonLatZoom; +import com.mapbox.mapboxgl.lib.LatLngZoom; import com.mapbox.mapboxgl.lib.MapView; import com.mapzen.android.lost.LocationClient; import com.mapzen.android.lost.LocationListener; @@ -201,7 +201,7 @@ public class MainActivity extends ActionBarActivity { // Handles location updates from GPS private void updateLocation(Location location) { if (location != null) { - LonLatZoom coordinate = new LonLatZoom(location.getLongitude(), location.getLatitude(), 16); + LatLngZoom coordinate = new LatLngZoom(location.getLongitude(), location.getLatitude(), 16); mMapFragment.getMap().setCenterCoordinate(coordinate, true); } }