已知用经纬度表示的两点,求两点之间的直线距离

最近在做一个用GDI+绘图,用到了对经纬度的处理,以下是将经纬度换算成直线距离的代码,留着以后有用,免得又忘记了。

 private const double EARTH_RADIUS = 6378137;//地球半径,单位米
        private double rad(double d)
        {
            return d * Math.PI / 180.0;
        }

        private double GetDistance(string lat1, string lng1, string lat2, string lng2)
        {

            double latD1=ConvertToDouble(lat1);

            double lngD1=ConvertToDouble(lng1);

            double latD2=ConvertToDouble(lat2);

            double lngD2=ConvertToDouble(lng2);

            double radLat1 = rad(latD1);
            double radLat2 = rad(latD2);
            double a = radLat1 - radLat2;
            double b = rad(lngD1) - rad(lngD2);

            double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
             Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
            s = s * EARTH_RADIUS;
            s = Math.Round(s * 10000) / 10000;
            return s;
        }

        //将经度从度分转换为double换算成度
        private double ConvertToDouble(string logi)
        {
            double result = 0;
            string temp = logi.Replace(" ", "");
            string[] du = logi.Split('°');
            string[] fen = du[1].Split('′');
            string second = fen[1].Replace("″", "");
            double fenConvertDu = Convert.ToDouble(fen[0]) / 60;
            double secondConvertDu = Convert.ToDouble(second) / 3600;
            result = Convert.ToDouble(du[0]) + fenConvertDu + secondConvertDu;
            return result;
        }

 

你可能感兴趣的:(随笔)