• C# 各种地图坐标系的转换


      1 public class ConvertGPS
      2     {
      3         private static double pi = 3.1415926535897932384626;
      4         private static double a = 6378245.0;
      5         private static double ee = 0.00669342162296594323;
      6         private static double bd_pi = 3.14159265358979324 * 3000.0 / 180.0;
      7 
      8         static Boolean outOfChina(double lat, double lon)
      9         {
     10             if (lon < 72.004 || lon > 137.8347)
     11                 return true;
     12             if (lat < 0.8293 || lat > 55.8271)
     13                 return true;
     14             return false;
     15         }
     16 
     17         static double transformLat(double x, double y)
     18         {
     19             double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
     20                     + 0.2 * Math.Sqrt(Math.Abs(x));
     21             ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
     22             ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
     23             ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
     24             return ret;
     25         }
     26 
     27         static double transformLon(double x, double y)
     28         {
     29             double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
     30                     * Math.Sqrt(Math.Abs(x));
     31             ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
     32             ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
     33             ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0
     34                     * pi)) * 2.0 / 3.0;
     35             return ret;
     36         }
     37 
     38         /** 
     39         * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System 
     40         *  
     41         * @param lat 
     42         * @param lon 
     43         * @return 
     44         */
     45 
     46         public static PointLatLng gps84_To_Gcj02(PointLatLng Gpoint)
     47         {
     48             if (outOfChina(Gpoint.Lat, Gpoint.Lng))
     49             {
     50                 return new PointLatLng(0, 0);
     51             }
     52             double dLat = transformLat(Gpoint.Lng - 105.0, Gpoint.Lat - 35.0);
     53             double dLon = transformLon(Gpoint.Lng - 105.0, Gpoint.Lat - 35.0);
     54             double radLat = Gpoint.Lat / 180.0 * pi;
     55             double magic = Math.Sin(radLat);
     56             magic = 1 - ee * magic * magic;
     57             double sqrtMagic = Math.Sqrt(magic);
     58             dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
     59             dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
     60             double mgLat = Gpoint.Lat + dLat;
     61             double mgLon = Gpoint.Lng + dLon;
     62             return new PointLatLng(mgLat, mgLon);
     63         }
     64         static PointLatLng transform(PointLatLng Gpoint)
     65         {
     66             if (outOfChina(Gpoint.Lat, Gpoint.Lng))
     67             {
     68                 return new PointLatLng(Gpoint.Lat, Gpoint.Lng);
     69             }
     70             double dLat = transformLat(Gpoint.Lng - 105.0, Gpoint.Lat - 35.0);
     71             double dLon = transformLon(Gpoint.Lng - 105.0, Gpoint.Lat - 35.0);
     72             double radLat = Gpoint.Lat / 180.0 * pi;
     73             double magic = Math.Sin(radLat);
     74             magic = 1 - ee * magic * magic;
     75             double sqrtMagic = Math.Sqrt(magic);
     76             dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
     77             dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
     78             double mgLat = Gpoint.Lat + dLat;
     79             double mgLon = Gpoint.Lng + dLon;
     80             return new PointLatLng(mgLat, mgLon);
     81         }
     82 
     83         /** 
     84          * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return 
     85          * */
     86         public static PointLatLng gcj02_To_Gps84(PointLatLng Gpoint)
     87         {
     88             PointLatLng gps = transform(Gpoint);
     89             double lontitude = Gpoint.Lng * 2 - gps.Lng;
     90             double latitude = Gpoint.Lat * 2 - gps.Lat;
     91             return new PointLatLng(latitude, lontitude);
     92         }
     93         /** 
     94          * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标 
     95          *  
     96          * @param gg_lat 
     97          * @param gg_lon 
     98          */
     99         public static PointLatLng gcj02_To_Bd09(PointLatLng Gpoint)
    100         {
    101             double x = Gpoint.Lng, y = Gpoint.Lat;
    102             double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * bd_pi);
    103             double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * bd_pi);
    104             double bd_lon = z * Math.Cos(theta) + 0.0065;
    105             double bd_lat = z * Math.Sin(theta) + 0.006;
    106             return new PointLatLng(bd_lat, bd_lon);
    107         }
    108 
    109         /** 
    110         * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param 
    111         * bd_lat * @param bd_lon * @return 
    112         */
    113         public static PointLatLng bd09_To_Gcj02(PointLatLng bdPoint)
    114         {
    115             double x = bdPoint.Lng - 0.0065, y = bdPoint.Lat - 0.006;
    116             double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * bd_pi);
    117             double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * bd_pi);
    118             double gg_lon = z * Math.Cos(theta);
    119             double gg_lat = z * Math.Sin(theta);
    120             return new PointLatLng(gg_lat, gg_lon);
    121         }
    122 
    123         /** 
    124          * (BD-09)-->84 
    125          * @param bd_lat 
    126          * @param bd_lon 
    127          * @return 
    128          */
    129         public static PointLatLng bd09_To_Gps84(PointLatLng bdPoint)
    130         {
    131 
    132             PointLatLng gcj02 = bd09_To_Gcj02(bdPoint);
    133             PointLatLng map84 = gcj02_To_Gps84(gcj02);
    134             return map84;
    135 
    136         }
    137         /** 
    138          * 84-->(BD-09)
    139          * @param bd_lat 
    140          * @param bd_lon 
    141          * @return 
    142          */
    143         public static PointLatLng Gps84_To_bd09(PointLatLng gpsPoint)
    144         {
    145             PointLatLng gcj02 = gps84_To_Gcj02(gpsPoint);
    146             PointLatLng bd09 = gcj02_To_Bd09(gcj02);
    147             return bd09;
    148 
    149         }
    150 
    151     }
    152 
    153   public class PointLatLng
    154     {
    155 
    156         public PointLatLng(double lat, double lng)
    157         {
    158             Lat = lat;
    159             Lng = lng;
    160         }
    161         public double Lat { get; set; }
    162         public double Lng { get; set; }
    163     }
  • 相关阅读:
    马的遍历 new
    锤子剪刀布 new
    npm 打包 new
    Linux 锁机制
    ubuntu virtualbox 下安装xp,识别usb
    ubuntu设置快捷键
    linux神奇的系统请求系统救命草
    linux 内核动态内存分配测试(纯属娱乐哈)
    C之绝妙(一道很NB的面试题)
    虚拟机virtualbox:Could not find an open hard disk with UUID {368441269e88468698582d1a0568f53c}.
  • 原文地址:https://www.cnblogs.com/Lxk0825/p/15465944.html
Copyright © 2020-2023  润新知