GPS Distance Function
Here's a C# method for calculating the distance (in nautical miles) between two GPS coordinates.
Download
Code
1
2
3
4
5
6
7
8
9
10 public static double CalculateDistanceBetweenGPSCoordinates(double lon1, double lat1, double lon2, double lat2) {
11 const double R = 3956;
12 const double degreesToRadians = Math.PI / 180;
13
14
15 lon1 *= degreesToRadians;
16 lat1 *= degreesToRadians;
17 lon2 *= degreesToRadians;
18 lat2 *= degreesToRadians;
19
20 double dlon = lon2 - lon1;
21 double dlat = lat2 - lat1;
22 double a = Math.Pow(Math.Sin(dlat / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / 2), 2);
23 double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
24 double d = R * c;
25
26 return d;
27 }