c# calculate distance between latitude longitude code example
Example 1: distance between two points latitude longitude c#
float DeltaFi = (float)ConvertToRadians(lat2 - lat1);
float DeltaLambda = (float)ConvertToRadians(lon2 - lon1);
float a = Mathf.Sin(DeltaFi / 2) * Mathf.Sin(DeltaFi / 2) + Mathf.Cos(fi1) * Mathf.Cos(fi2) * Mathf.Sin(DeltaLambda / 2) * Mathf.Sin(DeltaLambda / 2);
float c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1 - a));
float distance = earthD * c;
Example 2: calculate distance using latitude and longitude c#
public static double DistanceTo(double lat1, double lon1, double lat2, double lon2, char unit = 'K')
{
double rlat1 = Math.PI*lat1/180;
double rlat2 = Math.PI*lat2/180;
double theta = lon1 - lon2;
double rtheta = Math.PI*theta/180;
double dist =
Math.Sin(rlat1)*Math.Sin(rlat2) + Math.Cos(rlat1)*
Math.Cos(rlat2)*Math.Cos(rtheta);
dist = Math.Acos(dist);
dist = dist*180/Math.PI;
dist = dist*60*1.1515;
switch (unit)
{
case 'K':
return dist*1.609344;
case 'N':
return dist*0.8684;
case 'M':
return dist;
}
return dist;
}