package com.mapbox.mapboxsdk.geometry; import android.location.Location; import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.FloatRange; import com.mapbox.services.android.telemetry.constants.GeoConstants; import com.mapbox.services.android.telemetry.utils.MathUtils; /** * A geographical location which contains a single latitude, longitude pair, with * optional altitude. *
* Latitude and longitude are expressed as decimal degrees * in the WGS84 datum. By default, altitude is 0.0, or sea level. *
** Mapbox GL displays maps in the Mercator Projection and projects geographical * data automatically, so all data enters in the WGS84 datum. *
*/ public class LatLng implements ILatLng, Parcelable { /** * Inner class responsible for recreating Parcels into objects. */ public static final Parcelable.Creator* This value is in the range of [-85.05112878, 85.05112878], see {@link GeoConstants#MIN_LATITUDE} and * {@link GeoConstants#MAX_LATITUDE} *
* * @param latitude the latitude value in degrees * @see GeoConstants#MIN_LATITUDE * @see GeoConstants#MAX_LATITUDE */ public void setLatitude( @FloatRange(from = GeoConstants.MIN_LATITUDE, to = GeoConstants.MAX_LATITUDE) double latitude) { if (Double.isNaN(latitude)) { throw new IllegalArgumentException("latitude must not be NaN"); } if (Math.abs(latitude) > 90.0) { throw new IllegalArgumentException("latitude must be between -90 and 90"); } this.latitude = latitude; } /** * Get the latitude, in degrees. ** This value is in the range of [-85.05112878, 85.05112878], see {@link GeoConstants#MIN_LATITUDE} and * {@link GeoConstants#MAX_LATITUDE} *
* * @return the latitude value in degrees * @see GeoConstants#MIN_LATITUDE * @see GeoConstants#MAX_LATITUDE */ @Override public double getLatitude() { return latitude; } /** * Set the longitude, in degrees. ** This value is in the range of [-180, 180], see {@link GeoConstants#MIN_LONGITUDE} and * {@link GeoConstants#MAX_LONGITUDE} *
* * @param longitude the longitude value in degrees * @see GeoConstants#MIN_LONGITUDE * @see GeoConstants#MAX_LONGITUDE */ public void setLongitude(@FloatRange(from = GeoConstants.MIN_LONGITUDE, to = GeoConstants.MAX_LONGITUDE) double longitude) { if (Double.isNaN(longitude)) { throw new IllegalArgumentException("longitude must not be NaN"); } if (Double.isInfinite(longitude)) { throw new IllegalArgumentException("longitude must not be infinite"); } this.longitude = longitude; } /** * Get the longitude, in degrees. ** This value is in the range of [-180, 180], see {@link GeoConstants#MIN_LONGITUDE} and * {@link GeoConstants#MAX_LONGITUDE} *
* * @return the longitude value in degrees * @see GeoConstants#MIN_LONGITUDE * @see GeoConstants#MAX_LONGITUDE */ @Override public double getLongitude() { return longitude; } /** * Set the altitude, in meters. * * @param altitude the altitude in meters */ public void setAltitude(double altitude) { this.altitude = altitude; } /** * Get the altitude, in meters. * * @return the altitude value in meters */ @Override public double getAltitude() { return altitude; } /** * Return a new LatLng object with a wrapped Longitude. This allows original data object * to remain unchanged. * * @return new LatLng object with wrapped Longitude */ public LatLng wrap() { longitude = MathUtils.wrap(longitude, GeoConstants.MIN_LONGITUDE, GeoConstants.MAX_LONGITUDE); return this; } /** * Indicates whether some other object is "equal to" this one. * * @param object The object to compare * @return True if equal, false if not */ @Override public boolean equals(Object object) { if (this == object) { return true; } if (object == null || getClass() != object.getClass()) { return false; } LatLng latLng = (LatLng) object; return Double.compare(latLng.altitude, altitude) == 0 && Double.compare(latLng.latitude, latitude) == 0 && Double.compare(latLng.longitude, longitude) == 0; } /** * Returns a hash code value for the object. * * @return the hash code value */ @Override public int hashCode() { int result; long temp; temp = Double.doubleToLongBits(latitude); result = (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(longitude); result = 31 * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(altitude); result = 31 * result + (int) (temp ^ (temp >>> 32)); return result; } /** * Returns a string representation of the object. * * @return the string representation */ @Override public String toString() { return "LatLng [latitude=" + latitude + ", longitude=" + longitude + ", altitude=" + altitude + "]"; } /** * Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. * * @return a bitmask indicating the set of special object types marshaled by this Parcelable object instance. */ @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 */ @Override public void writeToParcel(Parcel out, int flags) { out.writeDouble(latitude); out.writeDouble(longitude); out.writeDouble(altitude); } /** * Calculate distance between two points * * @param other Other LatLng to compare to * @return distance in meters */ public double distanceTo(LatLng other) { if (latitude == other.latitude && longitude == other.longitude) { // return 0.0 to avoid a NaN return 0.0; } final double a1 = Math.toRadians(this.latitude); final double a2 = Math.toRadians(this.longitude); final double b1 = Math.toRadians(other.getLatitude()); final double b2 = Math.toRadians(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 GeoConstants.RADIUS_EARTH_METERS * tt; } }