Diverence between two geo locations

For the Raspberry Pi I am going to make some kind of GPS tracker. I am going to use a GPS receiver by Adafruit.
I will store data to the SD card. But to be more efficient I will write no data whenever my module is not moving. I will have a property set to some kind of distance threshold, say 10 feet or so. Or maybe I will throw some other algorithms in as well, At higher speeds record less measurements. When a sharp curve is detected record more measurements.
Additionally I sync the data of my SD card whenever I connect to it with an iOS app, or whenever my GPS tracker connects with with my WiFi, whenever my car is parked on my driveway.

The problem is, I have no clue how to calculate the distance between two geo locations. The distance of 1 degree at the equator is greater than 1 degree in the Arctic area. I can;t really see build in formulas or methods that can help me out. Or am I not looking good enough? :wink:

Google “GREAT CIRCLE” algorithms

10 feet requires quite a few decimal place of precision…

in the US… 1/1000 of a degree is approx 0.06 miles or 316 feet, so 10 feet would be around 0.000008 degrees I think

(radius * acos((sin(lat1)*sin(lat2)) +(cos(lat1)*cos(lat2)*cos(long2-long1))

where radius is 3963 miles (convert to KM for metric)

lat and long must be converted to RADIANS first

I did some google searches…
And I found two helpful links:

Stack Overflow
Movable-type website

So, I am making a class with a few shared methods, I think.

Here’s one I made earlier!

[code]Function getDistanceWAD(myLatitude1 As Double, myLongitude1 As Double, myLatitude2 As Double, myLongitude2 As Double, Units As String, numFormat As String) As String
'Dim distance,lat1,lat2,long1,long2,r As Double
Dim R, pi, a, c, d As Double 'x, y
Dim dLat, dLong As Double
Dim Lat1, Long1, Lat2, Long2 As Double
'Dim compareDistance As String = "Distances are: "

'if Lat1 = 0 or Long1 = 0 or Lat2 = 0 or Long2 = 0 then Return “”
if myLatitude1 = 0 or myLongitude1 = 0 or myLatitude2 = 0 or myLongitude2 = 0 then
Return “”
end if

pi = 3.141592653589793

'Where R is the radius of the earth in whatever units you desire.
select case Units
case “Statute Miles”
R = 3437.74677
case “Kilometers”, “Kilometres”, “Km”, “K”, “”
R = 6371 '6371 = mead radius, 6378 = equatorial radius
case “Miles”
R = 3963
case else
R = 6371
end select

''http://www.movable-type.co.uk/scripts/latlong.html
'Lat1 = myLatitude1
'Long1 = myLongitude1
'Lat2 = myLatitude2
'Long2 = myLongitude2
''var R = 6371; // km
''var dLat = (lat2-lat1).toRad();
''var dLon = (lon2-lon1).toRad();
''var lat1 = lat1.toRad();
''var lat2 = lat2.toRad();
''var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
''var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
''var d = R * c;
'dLat = (Lat2 - Lat1) * pi/180
'dLong = (Long2 - Long1) * pi/180
'Lat1 = Lat1 * pi/180
'Lat2 = Lat2 * pi/180
'a = Sin(dLat / 2) * Sin(dLat / 2) + Sin(dLong / 2) * Sin(dLong / 2) * Cos(Lat1) * Cos(Lat2)
'c = 2 * ATan2(Sqrt(a), Sqrt(1 - a))
'd = R * c
''Return str(R * y) + " " + Units
'compareDistance = compareDistance + "Moveable-Type = " + str(d) + " " + Units

''http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
'Lat1 = myLatitude1
'Long1 = myLongitude1
'Lat2 = myLatitude2
'Long2 = myLongitude2
''var R = 6371; // Radius of the earth in km
''var dLat = (lat2-lat1).toRad(); // Javascript functions in radians
''var dLon = (lon2-lon1).toRad();
''var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * Math.sin(dLon/2) * Math.sin(dLon/2);
''var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
''var d = R * c; // Distance in km
'dLat = (Lat2 - Lat1) * pi/180
'dLong = (Long2 - Long1) * pi/180
'a = sin(dLat/2) * sin(dLat/2) + cos(lat1 * pi/180) * cos(lat2 * pi/180) * sin(dLong/2) * sin(dLong/2)
'c = 2 * atan2(sqrt(a), sqrt(1 - a))
'd = R * c
''Return str(d) + " " + Units
'compareDistance = compareDistance + ", versus StackOverflow1 = " + str(d) + " " + Units

'php function distance($lat1, $lon1, $lat2, $lon2) {
'$pi80 = M_PI / 180;
'$lat1 *= $pi80;
'$lon1 *= $pi80;
'$lat2 *= $pi80;
'$lon2 *= $pi80;

'$r = 6372.797; // mean radius of Earth in km
'$dlat = $lat2 - $lat1;
'$dlon = $lon2 - $lon1;
'$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlon / 2) * sin($dlon / 2);
'$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
'$km = $r * $c;
//echo ‘
’.$km;
'return $km;
Lat1 = myLatitude1 * pi/180
Long1 = myLongitude1 * pi/180
Lat2 = myLatitude2 * pi/180
Long2 = myLongitude2 * pi/180

dLat = Lat2 - Lat1
dLong = Long2 - Long1
a = sin(dLat / 2) * sin(dLat / 2) + cos(lat1) * cos(lat2) * sin(dLong / 2) * sin(dLong/2)
c = 2 * atan2(sqrt(a), sqrt(1 - a))
d = R * c
'compareDistance = compareDistance + ", versus StackOverflow2 = " + str(d) + " " + Units

'compareDistance = compareDistance + " (" + str(Lat1) + “,” + str(Long1) + " : " + str(Lat2) + “,” + str(Long2) + “)”
'if d > 60 then MsgBox compareDistance

if numFormat = “” then
Return str(d) + " " + Units
'Return str(r * y) + " " + Units
'Return str(ACos(Sin(Lat1) * SIN(Lat2) + COS(Lat1) * COS(Lat2) * COS(Long2 - Long1)) * r * pi/180) + " " + Units
else
Return Format(d, numFormat) + " " + Units
'Return Format(r * y, numFormat) + " " + Units
'Return Format(ACos(Sin(Lat1) * SIN(Lat2) + COS(Lat1) * COS(Lat2) * COS(Long2 - Long1)) * r * pi/180, numFormat) + " " + Units
end if

'var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
'Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
'var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
'var d = R * c;

'x = sin((lat2-lat1)/2) * Sin(

'x = (lat2 - lat1) * pi/180
'y = (long2 - long1) * pi/180



'x = 69.1 * (lat2 - lat1)
'y = 53 * (long2 - long1)

//Approximate distance in miles
'Return Str(Sqrt(x * x + y * y)) + " " + Units

'Excel: =ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(lon2-lon1))*6371

Exception err
commonOS.doHandleExceptionWAD(err, "Method: " + CurrentMethodName)

'double haversine(double lat1, double lng1, double lat2, double lng2) {
'double dLat = Math.toRadians(lat2 - lat1);
'double dLon = Math.toRadians(lng2 - lng1);
'double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) *
'Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
'return 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) * 6371; // 6371 = average earth radius

End Function
[/code]

Why so complicated… its a ONE LINE equation… as I stated above.

Most of mine is comments! I have broken the equation down into it’s components. Also allowed for metric vs imperial units and formatting the resulting value.