正在进行的项目中有这样的需求:定位获得当前经纬度,再用百度Place API使用经纬度查询周边信息。这里不需要显示地图,只需要定位。看似思路很顺畅,做起来却不容易。
iPhone的GPS定位(CLLocationManager)获得的经纬坐标是基于WGS-84坐标系(世界标准),Google地图使用的是GCJ-02坐标系(中国特色的火星坐标系),这就是为什么获得的经纬坐标在google地图上会发生偏移。我项目需求是使用百度Place API,百度的经纬坐标在GCJ-02的基础上再做了次加密,就是DB-09坐标系。就想使用百度地图的IOS SDK,里面的坐标系统都是一致的而不用转换,下载2.0版的一看,没有类似CLLocationManager的定位功能,只是提供了BMapKit的地图功能,看它提供的Demo里的地图控件里也是空白一片~~ ——!好吧,放弃使用之。
在网上搜索一番,有现成的算法转换坐标,经试验 从WGS-84到GCJ-02,再到DB-09,经两次转换后,顺利获得当前正确地理位置信息和周边信息,当然这些信息是来自百度的。现显出来算法公式,原理不知。
1. // 定义经纬度结构体
2. typedef struct {
3. double lng;
4. double lat;
5. } Location;
6.
7. inline Location
8. LocationMake(double lng, double lat) {
9. Location loc; loc.lng = lng, loc.lat = lat; return loc;
10. }///
11. WGS-84 到 GCJ-02 的转换
12.
13.
14. const double pi = 3.14159265358979324;
15.
16. //
17. // Krasovsky 1940
18. //
19. // a = 6378245.0, 1/f = 298.3
20. // b = a * (1 - f)
21. // ee = (a^2 - b^2) / a^2;
22. const double a = 6378245.0;
23. const double ee = 0.00669342162296594323;
24.
25. Location transformFromWGSToGCJ(Location wgLoc)
26. {
27. Location mgLoc;
28. if (outOfChina(wgLoc.lat, wgLoc.lng))
29. {
30. mgLoc = wgLoc;
31. return mgLoc;
32. }
33. double dLat = transformLat(wgLoc.lng - 105.0, wgLoc.lat - 35.0);
34. double dLon = transformLon(wgLoc.lng - 105.0, wgLoc.lat - 35.0);
35. double radLat = wgLoc.lat / 180.0 * pi;
36. double magic = sin(radLat);
37. magic = 1 - ee * magic * magic;
38. double sqrtMagic = sqrt(magic);
39. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
40. dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
41. mgLoc.lat = wgLoc.lat + dLat;
42. mgLoc.lng = wgLoc.lng + dLon;
43.
44. return mgLoc;
45. }
46.
47. bool outOfChina(double lat, double lon)
48. {
49. if (lon 137.8347)
50. return true;
51. if (lat 55.8271)
52. return true;
53. return false;
54. }
55.
56. double transformLat(double x, double y)
57. {
58. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
59. ret += (20.0 * sin(6.0 * x * pi) + 20.0 *sin(2.0 * x * pi)) * 2.0 / 3.0;
60. ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
61. ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
62. return ret;
63. }
64.
65. double transformLon(double x, double y)
66. {
67. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
68. ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
69. ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
70. ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
71. return ret;
72. }
73.
74. ///
75. GCJ-02 坐标转换成 BD-09 坐标
76. ///
77.
78. const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
79. Location bd_encrypt(Location gcLoc)
80. {
81. double x = gcLoc.lng, y = gcLoc.lat;
82. double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
83. double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
84. return LocationMake(z * cos(theta) + 0.0065, z * sin(theta) + 0.006);
85. }
86.
87. ///
88. /// BD-09 坐标转换成 GCJ-02坐标
89. ///
90. ///
91. Location bd_decrypt(Location bdLoc)
92. {
93. double x = bdLoc.lng - 0.0065, y = bdLoc.lat - 0.006;
94. double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
95. double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
96. return LocationMake(z * cos(theta), z * sin(theta));
97. }
98. .csharpcode, .csharpcode pre
99. {
100. font-size: small;
101. color: black;
102. font-family: consolas, "Courier New", courier, monospace;
103. background-color: #ffffff;
104.
105. }
106. .csharpcode pre { margin: 0em; }
107. .csharpcode .rem { color: #008000; }
108. .csharpcode .kwrd { color: #0000ff; }
109. .csharpcode .str { color: #006080; }
110. .csharpcode .op { color: #0000c0; }
111. .csharpcode .preproc { color: #cc6633; }
112. .csharpcode .asp { background-color: #ffff00; }
113. .csharpcode .html { color: #800000; }
114. .csharpcode .attr { color: #ff0000; }
115. .csharpcode .alt
116. {
117. background-color: #f4f4f4;
118. width: 100%;
119. margin: 0em;
120. }
121. .csharpcode .lnum { color: #606060; }
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net