#868 - Bringing over ILatLng and refactoring LonLat to match LatLng

This commit is contained in:
Brad Leege
2015-02-12 16:28:22 -08:00
parent 3a84a7d264
commit 476ca373fe
9 changed files with 216 additions and 123 deletions

View File

@@ -3,31 +3,33 @@ package com.mapbox.mapboxgl.lib;
import android.os.Parcel;
import android.os.Parcelable;
public class LonLatZoom extends LonLat implements Parcelable {
import com.mapbox.mapboxgl.lib.geometry.LatLng;
public static final Parcelable.Creator<LonLatZoom> CREATOR = new Parcelable.Creator<LonLatZoom>() {
public LonLatZoom createFromParcel(Parcel in) {
return new LonLatZoom(in);
public class LatLngZoom extends LatLng implements Parcelable {
public static final Parcelable.Creator<LatLngZoom> CREATOR = new Parcelable.Creator<LatLngZoom>() {
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) {
public LatLngZoom(double lon, double lat, double zoom) {
super(lon, lat);
this.zoom = zoom;
}
public LonLatZoom(LonLat lonLat, double zoom) {
super(lonLat.getLon(), lonLat.getLat());
public LatLngZoom(LatLng latLng, double zoom) {
super(latLng.getLon(), latLng.getLat());
this.zoom = zoom;
}
private LonLatZoom(Parcel in) {
private LatLngZoom(Parcel in) {
super(in);
zoom = in.readDouble();
}
@@ -61,13 +63,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 [lon=" + super.getLon() + ", lat=" + super.getLat() + ", zoom=" + zoom
+ "]";
}

View File

@@ -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<LonLat> CREATOR = new Parcelable.Creator<LonLat>() {
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);
}
}

View File

@@ -28,6 +28,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;
@@ -153,7 +154,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));
@@ -206,24 +207,24 @@ public class MapView extends SurfaceView {
// Property methods
//
public LonLat getCenterCoordinate() {
public LatLng getCenterCoordinate() {
return mNativeMapView.getLonLat();
}
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);
}
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);
@@ -393,7 +394,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));

View File

@@ -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 setLonLat(LatLng latLng) {
setLonLat(latLng, 0);
}
public void setLonLat(LonLat lonLat, long duration) {
nativeSetLonLat(mNativeMapViewPtr, lonLat, duration);
public void setLonLat(LatLng latLng, long duration) {
nativeSetLonLat(mNativeMapViewPtr, latLng, duration);
}
public LonLat getLonLat() {
public LatLng getLonLat() {
return nativeGetLonLat(mNativeMapViewPtr);
}
@@ -305,15 +307,15 @@ class NativeMapView {
return nativeGetZoom(mNativeMapViewPtr);
}
public void setLonLatZoom(LonLatZoom lonLatZoom) {
public void setLonLatZoom(LatLngZoom lonLatZoom) {
setLonLatZoom(lonLatZoom, 0);
}
public void setLonLatZoom(LonLatZoom lonLatZoom, long duration) {
public void setLonLatZoom(LatLngZoom lonLatZoom, long duration) {
nativeSetLonLatZoom(mNativeMapViewPtr, lonLatZoom, duration);
}
public LonLatZoom getLonLatZoom() {
public LatLngZoom getLonLatZoom() {
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);

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -0,0 +1,12 @@
package com.mapbox.mapboxgl.lib.geometry;
/**
* A Latitude, Longitude point.
*/
public interface ILatLng {
double getLatitude();
double getLongitude();
double getAltitude();
}

View File

@@ -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<LatLng> CREATOR = new Parcelable.Creator<LatLng>() {
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);
}
}