1 #region 地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法
2
3 const double pi = 3.14159265358979324;
4 //
5 // Krasovsky 1940
6 //
7 // a = 6378245.0, 1/f = 298.3
8 // b = a * (1 - f)
9 // ee = (a^2 - b^2) / a^2;
10 const double a = 6378245.0;
11 const double ee = 0.00669342162296594323;
12
13 ///
14 /// World Geodetic System ==> Mars Geodetic System(GPS左边转换火星坐标)
15 ///
16 ///
17 ///
18 ///
19 ///
20 public static List<string> TransformGPSToBMap(double wgLat, double wgLon)
21 {
22 List<string> croods = new List<string>();
23 double mgLat;
24 double mgLon;
25 if (outOfChina(wgLat, wgLon))
26 {
27 mgLat = wgLat;
28 mgLon = wgLon;
29 return bd_encrypt(mgLat,mgLon);
30 }
31
32 double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
33 double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
34 double radLat = wgLat / 180.0 * pi;
35 double magic = Math.Sin(radLat);
36 magic = 1 - ee * magic * magic;
37 double sqrtMagic = Math.Sqrt(magic);
38 dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
39 dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
40 mgLat = wgLat + dLat;
41 mgLon = wgLon + dLon;
42
43 return bd_encrypt(mgLat, mgLon);
44 }
45
46 ///
47 /// 坐标是否出中国地区
48 ///
49 ///
50 ///
51 ///
52 static bool outOfChina(double lat, double lon)
53 {
54 if (lon <72.004 || lon > 137.8347)
55 return true;
56 if (lat <0.8293 || lat > 55.8271)
57 return true;
58 return false;
59 }
60
61 ///
62 /// 转换纬度
63 ///
64 ///
65 ///
66 ///
67 static double transformLat(double x, double y)
68 {
69 double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
70 ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
71 ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
72 ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
73 return ret;
74 }
75
76 ///
77 /// 转换经度
78 ///
79 ///
80 ///
81 ///
82 static double transformLon(double x, double y)
83 {
84 double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
85 ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
86 ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
87 ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
88 return ret;
89 }
90
91 #endregion