Calculate distance of two geo points in km c#

前端 未结 7 715
夕颜
夕颜 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 CalcDistance = (lat1, lon1, lat2, lon2) =>
            {
                Func 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;
            };
    

提交回复
热议问题