CalculateDistanceInMiles

From CDYNE Wiki
Jump to: navigation, search

Calculates distance between two points using Spherical Geometry.

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

C#:

public double CalculateDistanceInMiles(double latitude1, double longitude1, double latitude2, double longitude2) 
{
double x = Math.Sin(latitude1 / 57.2958) * Math.Sin(latitude2 / 57.2958) +
Math.Cos(latitude1 / 57.2958) * Math.Cos(latitude2 / 57.2958) * Math.Cos ((longitude2 - longitude1) / 57.2958);
return 3958.75 * Math.Acos(x);
}

VB.NET:

Public Function CalculateDistanceInMiles(ByVal latitude1 As Double, ByVal longitude1 As Double, ByVal latitude2 As Double, ByVal longitude2 As Double) As Double
Dim x As Double
x = (Math.Sin(latitude1 / 57.2958) * Math.Sin(latitude2 / 57.2958)) + _
(Math.Cos(latitude1 / 57.2958) * Math.Cos(latitude2 / 57.2958) * _
Math.Cos(longitude2 / 57.2958 - longitude1 / 57.2958))
Return 3958.75 * Math.Acos(x)
End Function
Personal tools
Namespaces

Variants
Actions
Navigation
Tools

Trial Key