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

黄舟
黄舟Original
2017-02-28 11:12:342390browse


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)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn