1 public static class MapConvertercs
2 {
3 //WGS-84原始坐标系
4 //GCJ-02:高德、Google
5 //BD-09:百度
6
7
8 ///
9 /// 圆周率
10 ///
11 private const double PI = 3.1415926535897932384626;
12 private const double X_PI = PI 3000.0 / 180.0;
13
14 ///
15 /// 地理位置是否位于中国以外
16 ///
17 ///
WGS-84坐标纬度
18 ///
WGS-84坐标经度
19 ///
20 /// true:国外
21 /// false:国内
22 ///
23 public static bool OutOfChina(double wgLat, double wgLon)
24 {
25 if (wgLon < 72.004 || wgLon > 137.8347) return true;
26 if (wgLat < 0.8293 || wgLat > 55.8271) return true;
27
28 return false;
29 }
30
31 ///
32 /// WGS-84坐标系转火星坐标系 (GCJ-02)
33 ///
34 ///
WGS-84坐标纬度
35 ///
WGS-84坐标经度
36 ///
输出:GCJ-02坐标纬度
37 ///
输出:GCJ-02坐标经度
38 public static void WGS84ToGCJ02(double wgLat, double wgLon, out double mgLat, out double mgLon)
39 {
40 if (OutOfChina(wgLat, wgLon))
41 {
42 mgLat = wgLat;
43 mgLon = wgLon;
44 }//代码效果参考:http://www.ezhiqi.com/bx/art_6813.html
45 else
46 {
47 double dLat;
48 double dLon;
49 Delta(wgLat, wgLon, out dLat, out dLon);
50 mgLat = wgLat + dLat;
51 mgLon = wgLon + dLon;
52 }
53 }
54
55 ///
56 /// 火星坐标系 (GCJ-02)转WGS-84坐标系
57 ///
58 ///
GCJ-02坐标纬度
59 ///
GCJ-02坐标经度
60 ///
输出:WGS-84坐标纬度
61 ///
输出:WGS-84坐标经度
62 public static void GCJ02ToWGS84(double mgLat, double mgLon, out double wgLat, out double wgLon)
63 {
64 if (OutOfChina(mgLat, mgLon))
65 {
66 wgLat = mgLat;
67 wgLon = mgLon;
68 }
69 else
70 {
71 double dLat;
72 double dLon;
73 Delta(mgLat, mgLon, out dLat, out dLon);
74 wgLat = mgLat - dLat;
75 wgLon = mgLon - dLon;
76 }
77 }
78
79 ///
80 /// 火星坐标系 (GCJ-02)转WGS-84坐标系
81 ///
82 ///
GCJ-02坐标纬度
83 ///
GCJ-02坐标经度
84 ///
输出:WGS-84坐标纬度
85 ///
输出:WGS-84坐标经度
86 public static void GCJ02ToWGS84Exact(double mgLat, double mgLon, out double wgLat, out double wgLon)
87 {
88 const double InitDelta = 0.01;
89 const double Threshold = 0.000001;
90
91 double dLat = InitDelta;
92 double dLon = InitDelta;
93 double mLat = mgLat - dLat;
94 double mLon = mgLon - dLon;
95 double pLat = mgLat + dLat;
96 double pLon = mgLon + dLon;
97
98 double nLat;
99 double nLon;
100
101 int i = 0;
102 do
103 {
104 wgLat = (mLat + pLat) / 2;
105 wgLon = (mLon + pLon) / 2;
106
107 WGS84ToGCJ02(wgLat, wgLon, out nLat, out nLon);
108
109 dLat = nLat - mgLat;
110 dLon = nLon - mgLon;
111 if ((Math.Abs(dLat) < Threshold) (Math.Abs(dLon) < Threshold)) break;
112
113 if (dLat > 0) pLat = wgLat; else mLat = wgLat;
114 if (dLon > 0) pLon = wgLon; else mLon = wgLon;
115 } while (++i <= 30);
116 }//代码效果参考:http://www.ezhiqi.com/zx/art_378.html
117
118 ///
119 /// 百度坐标系 (BD-09)转火星坐标系 (GCJ-02)
120 ///
121 ///
百度坐标系纬度
122 ///
百度坐标系经度
123 ///
输出:GCJ-02坐标纬度
124 ///
输出:GCJ-02坐标经度
125 public static void BD09ToGCJ02(double bdLat, double bdLon, out double mgLat, out double mgLon)
126 {
127 double x = bdLon - 0.0065;
128 double y = bdLat - 0.006;
129 double z = Math.Sqrt(x x + y y) - 0.00002 Math.Sin(y X_PI);
130 double theta = Math.Atan2(y, x) - 0.000003 Math.Cos(x X_PI);
131 mgLat = z Math.Sin(theta);
132 mgLon = z Math.Cos(theta);
133 }
134
135 ///
136 /// 火星坐标系 (GCJ-02)转百度坐标系 (BD-09)
137 ///
138 ///
GCJ-02坐标纬度
139 ///
GCJ-02坐标经度
140 ///
输出:百度坐标系纬度
141 ///
输出:百度坐标系经度
142 public static void GCJ02ToBD09(double mgLat, double mgLon, out double bdLat, out double bdLon)
143 {
144 double x = mgLon;
145 double y = mgLat;
146 double z = Math.Sqrt(x x + y y) + 0.00002 Math.Sin(y X_PI);
147 double theta = Math.Atan2(y, x) + 0.000003 Math.Cos(x X_PI);
148 bdLat = z Math.Sin(theta) + 0.006;
149 bdLon = z * Math.Cos(theta) + 0.0065;
150 }
151
152 ///
153 /// WGS-84坐标系转百度坐标系 (BD-09)
154 ///
155 ///
WGS-84坐标纬度
156 ///
<span style="color