The calculation formula is as follows:
illustrate:
1. The longitude and latitude in the formula are expressed in radians;
2. Lat1 Lng1 represents the longitude and latitude of point A respectively, Lat2 Lng2 represents the longitude and latitude of point B respectively;
3. a=Lng1 -Lng2 is the difference of latitude of two points; b=Lat1 - Lat2 is the difference of longitude of two points;
4. 6378.137 is the radius of the earth in kilometers;
The c# implementation function is as follows:
public double DistanceOfTwoPoints(double lat1, double lng1, double lat2, double lng2)
{
double radLng1 = lng1 * Math.PI / 180.0;
double radLng2 = lng2 * Math.PI / 180.0;
double a = radLng1 - radLng2;
double b = (lat1-lat2) * Math.PI / 180.0;
double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a/2), 2) +
Math.Cos(radLng1) * Math.Cos(radLng2) * Math.Pow(Math.Sin(b/2), 2)))* 6378.137;
s =Math.Round(s * 10000) / 10000;
return s;
}
It was passed correctly after actual verification (the unit of the returned result is: kilometers).