C# Calculate the closest straight line distance between two places

C# Calculate the closest straight line distance between two points

code show as below

Parameter introduction:
X1: Starting point accuracy
Y1: Starting point latitude
X2: Target point accuracy
Y2: Target point latitude
Return value unit is "meter"

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;
        }

Guess you like

Origin blog.csdn.net/qq_42455262/article/details/127654316