根据坐标经纬度计算两点之间的距离

时间:2022-04-01 19:15:23

地球是一个近乎标准的椭球体,它的赤道半径为6378.140千米,极半径为 6356.755千米,平均半径6371.004千米。

 

根据坐标经纬度计算两点之间的距离根据坐标经纬度计算两点之间的距离
        /// <summary>
        /// 给定的经度1,纬度1;经度2,纬度2. 计算2个经纬度之间的距离。
        /// </summary>
        /// <param name="lat1">纬度1</param>
        /// <param name="lon1">经度1</param>
        /// <param name="lat2">纬度2</param>
        /// <param name="lon2">经度2</param>
        /// <returns>距离(公里、千米)</returns>
        private double Distance(double lat1, double lon1, double lat2, double lon2)
        {
            double EARTH_RADIUS = 6371.0;

            //用haversine公式计算球面两点间的距离。
            //经纬度转换成弧度
            lat1 = ConvertDegreesToRadians(lat1);
            lon1 = ConvertDegreesToRadians(lon1);
            lat2 = ConvertDegreesToRadians(lat2);
            lon2 = ConvertDegreesToRadians(lon2);

            //差值
            var vLon = Math.Abs(lon1 - lon2);
            var vLat = Math.Abs(lat1 - lat2);

            // h is the great circle distance in radians, great circle
            // 就是一个球体上的切面,它的圆心即是球心的一个周长最大的圆。
            var h = HaverSin(vLat) + Math.Cos(lat1) * Math.Cos(lat2) * HaverSin(vLon);

            var distance = 2 * EARTH_RADIUS * Math.Asin(Math.Sqrt(h));

            return distance;
        }

        /// <summary>
        /// 将角度换算为弧度。
        /// </summary>
        /// <param name="degrees">角度</param>
        /// <returns>弧度</returns>
        private double ConvertDegreesToRadians(double degrees)
        {
            return degrees * Math.PI / 180;
        }

        /// <summary>
        /// 将弧度换算为角度。
        /// </summary>
        /// <param name="radian"></param>
        /// <returns></returns>
        private double ConvertRadiansToDegrees(double radian)
        {
            return radian * 180.0 / Math.PI;
        }

        /// <summary>
        /// HaverSin
        /// </summary>
        /// <param name="theta"></param>
        /// <returns></returns>
        private double HaverSin(double theta)
        {
            var v = Math.Sin(theta / 2);
            return v * v;
        }
View Code

 

 

在线计算经纬度距离工具 http://www.gpsspg.com/distance.htm

 

 

参考 https://blog.csdn.net/xiejm2333/article/details/73297004

https://www.cnblogs.com/zhoug2020/p/3950933.html