CalculateDistanceInMiles

From CDYNE Wiki
Jump to navigation Jump to search

Calculates distance between two points using Spherical Geometry.

This function is identical to the one used in the Postal Address Verification.

C#:

 1 public double CalculateDistanceInMiles(double latitude1, double longitude1, double latitude2, double longitude2) 
 2 { 
 3  double x = Math.Sin(latitude1 / 57.2958) * Math.Sin(latitude2 / 57.2958) + 
 4            Math.Cos(latitude1 / 57.2958) * Math.Cos(latitude2 / 57.2958) * Math.Cos ((longitude2 - longitude1) / 57.2958); 
 5  return 3958.75 * Math.Acos(x); 
 6 }
 7 </code>
 8 
 9 VB.NET:
10 <code type=vbnet>
11 Public Function CalculateDistanceInMiles(ByVal latitude1 As Double, ByVal longitude1 As Double, ByVal latitude2 As Double, ByVal longitude2 As Double) As Double
12    Dim x As Double
13    x = (Math.Sin(latitude1 / 57.2958) * Math.Sin(latitude2 / 57.2958)) + _
14      (Math.Cos(latitude1 / 57.2958) * Math.Cos(latitude2 / 57.2958) * _
15       Math.Cos(longitude2 / 57.2958 - longitude1 / 57.2958))
16    Return 3958.75 * Math.Acos(x)
17 End Function