Here is a class I wrote to handle locations within my world. I use Latitude and Longitude and this class will let me work out the distance between 2 points as well. (Not sure the maths is 100% correct, but it could be useful to someone)
Andy.
public class GPSLocation {
private static final int worldRadius = 6378; // Earth radius in km.
private float latDegrees;
private int latMinutes;
private int latSeconds;
private char latDirection;
private float lonDegrees;
private int lonMinutes;
private int lonSeconds;
private char lonDirection;
private int altitude; // Measured in meters above sea level.
public GPSLocation() {
latDegrees = 0;
latMinutes = 0;
latSeconds = 0;
latDirection = 'N';
lonDegrees = 0;
lonMinutes = 0;
lonSeconds = 0;
lonDirection = 'E';
altitude = 0;
}
// Get methods.
public float getLatDegrees() {
return latDegrees;
}
public int getLatMinutes() {
return latMinutes;
}
public int getLatSeconds() {
return latSeconds;
}
public char getLatDirection() {
return latDirection;
}
public float getLonDegrees() {
return lonDegrees;
}
public int getLonMinutes() {
return lonMinutes;
}
public int getLonSeconds() {
return lonSeconds;
}
public char getLonDirection() {
return lonDirection;
}
public int getAltitude() {
return altitude;
}
public double getLatRadians() {
double res1 = latDegrees + (latMinutes + latSeconds/60) /60;
if (latDirection == 'S') {
res1 *= -1;
}
return Math.toRadians(res1);
}
public double getLonRadians() {
double res1 = lonDegrees + (lonMinutes + lonSeconds/60) /60;
if (lonDirection == 'W') {
res1 *= -1;
}
return Math.toRadians(res1);
}
// Set methods.
public void setLatDegrees(float pLatDegrees) {
latDegrees = pLatDegrees;
}
public void setLatMinutes(int pLatMinutes) {
latMinutes = pLatMinutes;
}
public void setLatSeconds(int pLatSeconds) {
latSeconds = pLatSeconds;
}
public void setLatDirection(char pLatDirection) throws Exception {
char dir = Character.toUpperCase(pLatDirection);
if ((dir == 'N') || (dir == 'S')) {
latDirection = dir;
} else {
throw new Exception("Invalid direction for Latitude, (N)orth or (S)outh only.");
}
}
public void setLonDegrees(float pLonDegrees) {
lonDegrees = pLonDegrees;
}
public void setLonMinutes(int pLonMinutes) {
lonMinutes = pLonMinutes;
}
public void setLonSeconds(int pLonSeconds) {
lonSeconds = pLonSeconds;
}
public void setLonDirection(char pLonDirection) throws Exception {
char dir = Character.toUpperCase(pLonDirection);
if ((dir == 'E') || (dir == 'W')) {
lonDirection = dir;
} else {
throw new Exception("Invalid direction for Longitude, (E)ast or (W)est only.");
}
}
public void setAltitude(int pAltitude) {
altitude = pAltitude;
}
// Utility methods.
public double getDistance(GPSLocation otherLocation) {
double a = this.getLatRadians();
double b = this.getLonRadians();
double c = otherLocation.getLatRadians();
double d = otherLocation.getLonRadians();
// This calculation should take into account the altitude of the 2 locations.
if ((a == c) && (b == d)) {
return 0;
}
double res1 = Math.sin(a) * Math.sin(c) + Math.cos(a) * Math.cos(c) * Math.cos(b-d);
if (res1 > 1) {
return worldRadius * Math.acos(1);
} else {
return worldRadius * Math.acos(res1);
}
}
} // Class