C#计算两地点最近直线距离

C# 计算两点最近直线距离

代码如下

参数介绍:
X1:起始地点精度
Y1:起始地点纬度
X2:目标地点精度
Y2:目标地点纬度
返回值单位为“米”

public static double CalcMil(double X1, double Y1, double X2, double Y2)
        {
    
    
            double PI = Math.PI;
            double EARTH_RADIUS = 6378137;  //地球半径 

            double CurRadLong = 0;	//两点经纬度的弧度
            double CurRadLat = 0;
            double PreRadLong = 0;
            double PreRadLat = 0;
            double a = 0, b = 0;              //经纬度弧度差
            double MilValue = 0;

            //将经纬度换算成弧度
            CurRadLong = (double)(X1);
            CurRadLong = CurRadLong * PI / 180.0;

            PreRadLong = (double)(X2);
            PreRadLong = PreRadLong * PI / 180.0;

            CurRadLat = (double)(Y1);
            CurRadLat = CurRadLat * PI / 180.0f;

            PreRadLat = (double)(Y2);
            PreRadLat = PreRadLat * PI / 180.0f;

            //计算经纬度差值
            if (CurRadLat > PreRadLat)
            {
    
    
                a = CurRadLat - PreRadLat;
            }
            else
            {
    
    
                a = PreRadLat - CurRadLat;
            }

            if (CurRadLong > PreRadLong)
            {
    
    
                b = CurRadLong - PreRadLong;
            }
            else
            {
    
    
                b = PreRadLong - CurRadLong;
            }

            MilValue = 2 * Math.Asin(Math.Sqrt(Math.Sin(a / 2.0) * Math.Sin(a / 2.0) + Math.Cos(CurRadLat) * Math.Cos(PreRadLat) * Math.Sin(b / 2.0) * Math.Sin(b / 2.0)));
            MilValue = (double)(EARTH_RADIUS * MilValue);
            return MilValue;
        }

猜你喜欢

转载自blog.csdn.net/qq_42455262/article/details/127654316