Calculate distance of two geo points in km c#

前端 未结 7 655
夕颜
夕颜 2020-12-03 00:21

I`d like calculate the distance of two geo points. the points are given in longitude and latitude.

the coordinates are:

point 1: 36.578581, -118.291994

相关标签:
7条回答
  • 2020-12-03 00:50

    I used the formula from Wikipedia and put it in a lambda function:

    Func<double, double, double, double, double> CalcDistance = (lat1, lon1, lat2, lon2) =>
            {
                Func<double, double> Radians = (angle) =>
                {
                    return angle * (180.0 / Math.PI);
                };
    
                const double radius = 6371;
    
                double delataSigma = Math.Acos(Math.Sin(Radians(lat1)) * Math.Sin(Radians(lat2)) +
                    Math.Cos(Radians(lat1)) * Math.Cos(Radians(lat2)) * Math.Cos(Math.Abs(Radians(lon2) - Radians(lon1))));
    
                double distance = radius * delataSigma;
    
                return distance;
            };
    
    0 讨论(0)
提交回复
热议问题