Home > Article > Backend Development > C# Calculate the distance function between two points with known longitude and latitude
C# Calculate the distance function between two points with known longitude and latitude
public Task<double> GetDistance(double lat1, double lon1, double lat2, double lon2) { int R = 6371; double rLat1 = ToRadian(lat1); double rLat2 = ToRadian(lat2); double dLat = rLat2 - rLat1; double dLon = ToRadian(lon2 - lon1); double a = Math.Pow(Math.Sin(dLat / 2), 2) + Math.Pow(Math.Sin(dLon / 2), 2) * Math.Cos(rLat1) * Math.Cos(rLat2); double b = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); return Task.FromResult((R * b)); }
The above is the content of C# Calculate the distance function between two points with known longitude and latitude. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!