Back to project page aBusTripMK.
The source code is released under:
GNU General Public License
If you think the Android project aBusTripMK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
// Created by plusminus on 21:28:12 - 25.09.2008 package org.andnav.osm.util; /* w w w . ja va 2s.c om*/ import org.andnav.osm.util.constants.GeoConstants; import org.andnav.osm.views.util.constants.MathConstants; import android.location.Location; /** * * @author Nicolas Gramlich * */ public class GeoPoint implements MathConstants, GeoConstants{ // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private int mLongitudeE6; private int mLatitudeE6; // =========================================================== // Constructors // =========================================================== public GeoPoint(final int aLatitudeE6, final int aLongitudeE6) { this.mLatitudeE6 = aLatitudeE6; this.mLongitudeE6 = aLongitudeE6; } public GeoPoint(final double aLatitude, final double aLongitude) { this.mLatitudeE6 = (int)(aLatitude * 1E6); this.mLongitudeE6 = (int)(aLongitude * 1E6); } public GeoPoint(Location aLocation) { this(aLocation.getLatitude(), aLocation.getLongitude()); } protected static GeoPoint fromDoubleString(final String s, final char spacer) { final int spacerPos = s.indexOf(spacer); return new GeoPoint((int) (Double.parseDouble(s.substring(0, spacerPos - 1)) * 1E6), (int) (Double.parseDouble(s.substring( spacerPos + 1, s.length())) * 1E6)); } public static GeoPoint fromIntString(final String s){ final int commaPos = s.indexOf(','); return new GeoPoint(Integer.parseInt(s.substring(0,commaPos-1)), Integer.parseInt(s.substring(commaPos+1,s.length()))); } // =========================================================== // Getter & Setter // =========================================================== public int getLongitudeE6() { return this.mLongitudeE6; } public int getLatitudeE6() { return this.mLatitudeE6; } public void setLongitudeE6(final int aLongitudeE6) { this.mLongitudeE6 = aLongitudeE6; } public void setLatitudeE6(final int aLatitudeE6) { this.mLatitudeE6 = aLatitudeE6; } public void setCoordsE6(final int aLatitudeE6, final int aLongitudeE6) { this.mLatitudeE6 = aLatitudeE6; this.mLongitudeE6 = aLongitudeE6; } // =========================================================== // Methods from SuperClass/Interfaces // =========================================================== @Override public String toString(){ return new StringBuilder().append(this.mLatitudeE6).append(",").append(this.mLongitudeE6).toString(); } public String toDoubleString() { return new StringBuilder().append(this.mLatitudeE6 / 1E6).append(",").append(this.mLongitudeE6 / 1E6).toString(); } @Override public boolean equals(Object obj) { if(!(obj instanceof GeoPoint)) return false; GeoPoint g = (GeoPoint)obj; return g.mLatitudeE6 == this.mLatitudeE6 && g.mLongitudeE6 == this.mLongitudeE6; } // =========================================================== // Methods // =========================================================== /** * @see Source@ http://www.geocities.com/DrChengalva/GPSDistance.html * @param gpA * @param gpB * @return distance in meters */ public int distanceTo(final GeoPoint other) { final double a1 = DEG2RAD * (this.mLatitudeE6 / 1E6); final double a2 = DEG2RAD * (this.mLongitudeE6 / 1E6); final double b1 = DEG2RAD * (other.mLatitudeE6 / 1E6); final double b2 = DEG2RAD * (other.mLongitudeE6 / 1E6); 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); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }