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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| public class GPSLocation {
private static final int worldRadius = 6378; 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; public GPSLocation() { latDegrees = 0; latMinutes = 0; latSeconds = 0; latDirection = 'N';
lonDegrees = 0; lonMinutes = 0; lonSeconds = 0; lonDirection = 'E';
altitude = 0; }
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); }
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; }
public double getDistance(GPSLocation otherLocation) { double a = this.getLatRadians(); double b = this.getLonRadians(); double c = otherLocation.getLatRadians(); double d = otherLocation.getLonRadians();
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); } }
} |