aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/geopoint/Geopoint.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/geopoint/Geopoint.java')
-rw-r--r--main/src/cgeo/geocaching/geopoint/Geopoint.java92
1 files changed, 62 insertions, 30 deletions
diff --git a/main/src/cgeo/geocaching/geopoint/Geopoint.java b/main/src/cgeo/geocaching/geopoint/Geopoint.java
index ef08401..ff97a4c 100644
--- a/main/src/cgeo/geocaching/geopoint/Geopoint.java
+++ b/main/src/cgeo/geocaching/geopoint/Geopoint.java
@@ -1,6 +1,6 @@
package cgeo.geocaching.geopoint;
-import cgeo.geocaching.Settings;
+import cgeo.geocaching.ICoordinates;
import cgeo.geocaching.geopoint.GeopointFormatter.Format;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
@@ -11,6 +11,8 @@ import org.json.JSONArray;
import org.json.JSONObject;
import android.location.Location;
+import android.os.Parcel;
+import android.os.Parcelable;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -18,8 +20,7 @@ import java.math.RoundingMode;
/**
* Abstraction of geographic point.
*/
-public final class Geopoint
-{
+public final class Geopoint implements ICoordinates, Parcelable {
public static final double deg2rad = Math.PI / 180;
public static final double rad2deg = 180 / Math.PI;
public static final float erad = 6371.0f;
@@ -47,19 +48,6 @@ public final class Geopoint
}
/**
- * Creates new Geopoint with given latitude and longitude (both microdegree).
- *
- * @param lat
- * latitude
- * @param lon
- * longitude
- */
- public Geopoint(final int lat, final int lon)
- {
- this(lat / 1e6, lon / 1e6);
- }
-
- /**
* Creates new Geopoint with latitude and longitude parsed from string.
*
* @param text
@@ -98,6 +86,17 @@ public final class Geopoint
}
/**
+ * Create new Geopoint from Parcel.
+ *
+ * @param in
+ * a Parcel to read the saved data from
+ */
+ public Geopoint(final Parcel in) {
+ latitude = in.readDouble();
+ longitude = in.readDouble();
+ }
+
+ /**
* Get latitude in degree.
*
* @return latitude
@@ -114,7 +113,7 @@ public final class Geopoint
*/
public int getLatitudeE6()
{
- return (int) (latitude * 1E6);
+ return (int) Math.round(latitude * 1E6);
}
/**
@@ -134,7 +133,7 @@ public final class Geopoint
*/
public int getLongitudeE6()
{
- return (int) (longitude * 1E6);
+ return (int) Math.round(longitude * 1E6);
}
/**
@@ -200,16 +199,21 @@ public final class Geopoint
return new Geopoint(rlat * rad2deg, rlon * rad2deg);
}
- /**
- * Checks if given Geopoint is identical with this Geopoint.
- *
- * @param gp
- * Geopoint to check
- * @return true if identical, false otherwise
- */
- public boolean isEqualTo(Geopoint gp)
- {
- return null != gp && gp.latitude == latitude && gp.longitude == longitude;
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || !(obj instanceof Geopoint)) {
+ return false;
+ }
+ final Geopoint gp = (Geopoint) obj;
+ return getLatitudeE6() == gp.getLatitudeE6() && getLongitudeE6() == gp.getLongitudeE6();
+ }
+
+ @Override
+ public int hashCode() {
+ return getLatitudeE6() ^ getLongitudeE6();
}
/**
@@ -242,7 +246,7 @@ public final class Geopoint
/**
* Returns formatted coordinates with default format.
* Default format is decimalminutes, e.g. N 52° 36.123 E 010° 03.456
- *
+ *
* @return formatted coordinates
*/
@Override
@@ -506,10 +510,38 @@ public final class Geopoint
return result.getDouble("elevation");
}
} catch (Exception e) {
- Log.w(Settings.tag, "cgBase.getElevation: " + e.toString());
+ Log.w("cgBase.getElevation: " + e.toString());
}
return null;
}
+ //FIXME: this interface implementation is totally confusing as it returns the class itself.
+ // it can therefore be removed completely (and any invocation of it) without any disadvantages
+ @Override
+ public Geopoint getCoords() {
+ return this;
+ }
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ @Override
+ public void writeToParcel(final Parcel dest, final int flags) {
+ dest.writeDouble(latitude);
+ dest.writeDouble(longitude);
+ }
+
+ public static final Parcelable.Creator<Geopoint> CREATOR = new Parcelable.Creator<Geopoint>() {
+ public Geopoint createFromParcel(final Parcel in) {
+ return new Geopoint(in);
+ }
+
+ public Geopoint[] newArray(final int size) {
+ return new Geopoint[size];
+ }
+ };
+
}