C# 2 地点間の最も近い直線距離を計算する

C#2点間の最も近い直線距離を計算する.

コードは以下のように表示されます

パラメータの紹介:
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