EMCoordinateTransform.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. //
  2. // EMCoordinateTransform.m
  3. // SolarLamp
  4. //
  5. // Created by weclouds on 2018/11/14.
  6. // Copyright © 2018 SolarLamp. All rights reserved.
  7. //
  8. //这个自己写
  9. #import "EMCoordinateTransform.h"
  10. #import<math.h>
  11. #import<UIKit/UIGeometry.h>
  12. static const double a = 6378245.0;
  13. static const double ee = 0.00669342162296594323;
  14. static const double pi = 3.14159265358979324;
  15. static const double xPi = M_PI  * 3000.0 / 180.0;
  16. @implementation EMCoordinateTransform
  17. +(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc
  18. {
  19. CLLocationCoordinate2D adjustLoc;
  20. double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
  21. double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
  22. long double radLat = wgsLoc.latitude / 180.0 * pi;
  23. long double magic = sin(radLat);
  24. magic = 1 - ee * magic * magic;
  25. long double sqrtMagic = sqrt(magic);
  26. adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  27. adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
  28. adjustLoc.latitude = wgsLoc.latitude + adjustLat;
  29. adjustLoc.longitude = wgsLoc.longitude + adjustLon;
  30. return adjustLoc;
  31. }
  32. + (double)transformLatWithX:(double)x withY:(double)y
  33. {
  34. double lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x));
  35. lat += (20.0 * sin(6.0 * x * pi) + 20.0 *sin(2.0 * x * pi)) * 2.0 / 3.0;
  36. lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
  37. lat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
  38. return lat;
  39. }
  40. + (double)transformLonWithX:(double)x withY:(double)y
  41. {
  42. double lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x));
  43. lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
  44. lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
  45. lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
  46. return lon;
  47. }
  48. +(CLLocationCoordinate2D)transformFromGCJToBaidu:(CLLocationCoordinate2D)p
  49. {
  50. long double z = sqrt(p.longitude * p.longitude + p.latitude * p.latitude) + 0.00002 * sin(p.latitude * xPi);
  51. long double theta = atan2(p.latitude, p.longitude) + 0.000003 * cos(p.longitude * xPi);
  52. CLLocationCoordinate2D geoPoint;
  53. geoPoint.latitude = (z * sin(theta) + 0.006);
  54. geoPoint.longitude = (z * cos(theta) + 0.0065);
  55. return geoPoint;
  56. }
  57. +(CLLocationCoordinate2D)transformFromBaiduToGCJ:(CLLocationCoordinate2D)p
  58. {
  59. double x = p.longitude - 0.0065, y = p.latitude - 0.006;
  60. double z = sqrt(x * x + y * y) - 0.00002 * sin(y * xPi);
  61. double theta = atan2(y, x) - 0.000003 * cos(x * xPi);
  62. CLLocationCoordinate2D geoPoint;
  63. geoPoint.latitude  = z * sin(theta);
  64. geoPoint.longitude = z * cos(theta);
  65. return geoPoint;
  66. }
  67. +(CLLocationCoordinate2D)transformFromGCJToWGS:(CLLocationCoordinate2D)p
  68. {
  69. double threshold = 0.00001;
  70. // The boundary
  71. double minLat = p.latitude - 0.5;
  72. double maxLat = p.latitude + 0.5;
  73. double minLng = p.longitude - 0.5;
  74. double maxLng = p.longitude + 0.5;
  75. double delta = 1;
  76. int maxIteration = 30;
  77. // Binary search
  78. while(true)
  79. {
  80. CLLocationCoordinate2D leftBottom  = [[self class] transformFromWGSToGCJ:(CLLocationCoordinate2D){.latitude = minLat,.longitude = minLng}];
  81. CLLocationCoordinate2D rightBottom = [[self class] transformFromWGSToGCJ:(CLLocationCoordinate2D){.latitude = minLat,.longitude = maxLng}];
  82. CLLocationCoordinate2D leftUp      = [[self class] transformFromWGSToGCJ:(CLLocationCoordinate2D){.latitude = maxLat,.longitude = minLng}];
  83. CLLocationCoordinate2D midPoint    = [[self class] transformFromWGSToGCJ:(CLLocationCoordinate2D){.latitude = ((minLat + maxLat) / 2),.longitude = ((minLng + maxLng) / 2)}];
  84. delta = fabs(midPoint.latitude - p.latitude) + fabs(midPoint.longitude - p.longitude);
  85. if(maxIteration-- <= 0 || delta <= threshold)
  86. {
  87. return (CLLocationCoordinate2D){.latitude = ((minLat + maxLat) / 2),.longitude = ((minLng + maxLng) / 2)};
  88. }
  89. if(isContains(p, leftBottom, midPoint))
  90. {
  91. maxLat = (minLat + maxLat) / 2;
  92. maxLng = (minLng + maxLng) / 2;
  93. }
  94. else if(isContains(p, rightBottom, midPoint))
  95. {
  96. maxLat = (minLat + maxLat) / 2;
  97. minLng = (minLng + maxLng) / 2;
  98. }
  99. else if(isContains(p, leftUp, midPoint))
  100. {
  101. minLat = (minLat + maxLat) / 2;
  102. maxLng = (minLng + maxLng) / 2;
  103. }
  104. else
  105. {
  106. minLat = (minLat + maxLat) / 2;
  107. minLng = (minLng + maxLng) / 2;
  108. }
  109. }
  110. }
  111. static bool isContains(CLLocationCoordinate2D point, CLLocationCoordinate2D p1, CLLocationCoordinate2D p2)
  112. {
  113. return (point.latitude >= MIN(p1.latitude, p2.latitude) && point.latitude <= MAX(p1.latitude, p2.latitude)) && (point.longitude >= MIN(p1.longitude,p2.longitude) && point.longitude <= MAX(p1.longitude, p2.longitude));
  114. }
  115. /**
  116. *  判断是不是在中国
  117. *  用引射线法判断 点是否在多边形内部
  118. *  算法参考:http://www.cnblogs.com/luxiaoxun/p/3722358.html
  119. */
  120. + (BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location {
  121. CGPoint point = CGPointMake(location.latitude, location.longitude);
  122. BOOL oddFlag = NO;
  123. NSInteger j = [self polygonOfChina].count - 1;
  124. for (NSInteger i = 0; i < [self polygonOfChina].count; i++) {
  125. CGPoint polygonPointi = [[self polygonOfChina][i] CGPointValue];
  126. CGPoint polygonPointj = [[self polygonOfChina][j] CGPointValue];
  127. if (((polygonPointi.y < point.y && polygonPointj.y >= point.y) ||
  128. (polygonPointj.y < point.y && polygonPointi.y >= point.y)) &&
  129. (polygonPointi.x <= point.x || polygonPointj.x <= point.x)) {
  130. oddFlag ^= (polygonPointi.x +
  131. (point.y - polygonPointi.y) /
  132. (polygonPointj.y - polygonPointi.y) *
  133. (polygonPointj.x - polygonPointi.x) <
  134. point.x);
  135. }
  136. j = i;
  137. }
  138. return !oddFlag;
  139. }
  140. //  中国大陆多边形,用于判断坐标是否在中国
  141. //  因为港澳台地区使用WGS坐标,所以多边形不包含港澳台地区
  142. + (NSMutableArray *)polygonOfChina {
  143. static NSMutableArray *polygonOfChina = nil;
  144. static dispatch_once_t onceToken;
  145. dispatch_once(&onceToken, ^{
  146. polygonOfChina = [[NSMutableArray alloc] init];
  147. [polygonOfChina
  148. addObject:[NSValue valueWithCGPoint:CGPointMake(49.1506690000,
  149. 87.4150810000)]];
  150. [polygonOfChina
  151. addObject:[NSValue valueWithCGPoint:CGPointMake(48.3664501790,
  152. 85.7527085300)]];
  153. [polygonOfChina
  154. addObject:[NSValue valueWithCGPoint:CGPointMake(47.0253058185,
  155. 85.3847443554)]];
  156. [polygonOfChina
  157. addObject:[NSValue valueWithCGPoint:CGPointMake(45.2406550000,
  158. 82.5214000000)]];
  159. [polygonOfChina
  160. addObject:[NSValue valueWithCGPoint:CGPointMake(44.8957121295,
  161. 79.9392351487)]];
  162. [polygonOfChina
  163. addObject:[NSValue valueWithCGPoint:CGPointMake(43.1166843846,
  164. 80.6751253982)]];
  165. [polygonOfChina
  166. addObject:[NSValue valueWithCGPoint:CGPointMake(41.8701690000,
  167. 79.6882160000)]];
  168. [polygonOfChina
  169. addObject:[NSValue valueWithCGPoint:CGPointMake(39.2896190000,
  170. 73.6171080000)]];
  171. [polygonOfChina
  172. addObject:[NSValue valueWithCGPoint:CGPointMake(34.2303430000,
  173. 78.9155300000)]];
  174. [polygonOfChina
  175. addObject:[NSValue valueWithCGPoint:CGPointMake(31.0238860000,
  176. 79.0627080000)]];
  177. [polygonOfChina
  178. addObject:[NSValue valueWithCGPoint:CGPointMake(27.9989800000,
  179. 88.7028920000)]];
  180. [polygonOfChina
  181. addObject:[NSValue valueWithCGPoint:CGPointMake(27.1793590000,
  182. 88.9972480000)]];
  183. [polygonOfChina
  184. addObject:[NSValue valueWithCGPoint:CGPointMake(28.0969170000,
  185. 89.7331400000)]];
  186. [polygonOfChina
  187. addObject:[NSValue valueWithCGPoint:CGPointMake(26.9157800000,
  188. 92.1615830000)]];
  189. [polygonOfChina
  190. addObject:[NSValue valueWithCGPoint:CGPointMake(28.1947640000,
  191. 96.0986050000)]];
  192. [polygonOfChina
  193. addObject:[NSValue valueWithCGPoint:CGPointMake(27.4094760000,
  194. 98.6742270000)]];
  195. [polygonOfChina
  196. addObject:[NSValue valueWithCGPoint:CGPointMake(23.9085500000,
  197. 97.5703890000)]];
  198. [polygonOfChina
  199. addObject:[NSValue valueWithCGPoint:CGPointMake(24.0775830000,
  200. 98.7846100000)]];
  201. [polygonOfChina
  202. addObject:[NSValue valueWithCGPoint:CGPointMake(22.1375640000,
  203. 99.1893510000)]];
  204. [polygonOfChina
  205. addObject:[NSValue valueWithCGPoint:CGPointMake(21.1398950000,
  206. 101.7649720000)]];
  207. [polygonOfChina
  208. addObject:[NSValue valueWithCGPoint:CGPointMake(22.2746220000,
  209. 101.7281780000)]];
  210. [polygonOfChina
  211. addObject:[NSValue valueWithCGPoint:CGPointMake(23.2641940000,
  212. 105.3708430000)]];
  213. [polygonOfChina
  214. addObject:[NSValue valueWithCGPoint:CGPointMake(22.7191200000,
  215. 106.6954480000)]];
  216. [polygonOfChina
  217. addObject:[NSValue valueWithCGPoint:CGPointMake(21.9945711661,
  218. 106.7256731791)]];
  219. [polygonOfChina
  220. addObject:[NSValue valueWithCGPoint:CGPointMake(21.4847050000,
  221. 108.0200530000)]];
  222. [polygonOfChina
  223. addObject:[NSValue valueWithCGPoint:CGPointMake(20.4478440000,
  224. 109.3814530000)]];
  225. [polygonOfChina
  226. addObject:[NSValue valueWithCGPoint:CGPointMake(18.6689850000,
  227. 108.2408210000)]];
  228. [polygonOfChina
  229. addObject:[NSValue valueWithCGPoint:CGPointMake(17.4017340000,
  230. 109.9333720000)]];
  231. [polygonOfChina
  232. addObject:[NSValue valueWithCGPoint:CGPointMake(19.5085670000,
  233. 111.4051560000)]];
  234. [polygonOfChina
  235. addObject:[NSValue valueWithCGPoint:CGPointMake(21.2716775175,
  236. 111.2514995205)]];
  237. [polygonOfChina
  238. addObject:[NSValue valueWithCGPoint:CGPointMake(21.9936323233,
  239. 113.4625292629)]];
  240. [polygonOfChina
  241. addObject:[NSValue valueWithCGPoint:CGPointMake(22.1818312942,
  242. 113.4258358111)]];
  243. [polygonOfChina
  244. addObject:[NSValue valueWithCGPoint:CGPointMake(22.2249729295,
  245. 113.5913115000)]];
  246. [polygonOfChina
  247. addObject:[NSValue valueWithCGPoint:CGPointMake(22.4501912753,
  248. 113.8946844490)]];
  249. [polygonOfChina
  250. addObject:[NSValue valueWithCGPoint:CGPointMake(22.5959159322,
  251. 114.3623797842)]];
  252. [polygonOfChina
  253. addObject:[NSValue valueWithCGPoint:CGPointMake(22.4334610000,
  254. 114.5194740000)]];
  255. [polygonOfChina
  256. addObject:[NSValue valueWithCGPoint:CGPointMake(22.9680954377,
  257. 116.8326939975)]];
  258. [polygonOfChina
  259. addObject:[NSValue valueWithCGPoint:CGPointMake(25.3788220000,
  260. 119.9667980000)]];
  261. [polygonOfChina
  262. addObject:[NSValue valueWithCGPoint:CGPointMake(28.3261276204,
  263. 121.7724402562)]];
  264. [polygonOfChina
  265. addObject:[NSValue valueWithCGPoint:CGPointMake(31.9883610000,
  266. 123.8808230000)]];
  267. [polygonOfChina
  268. addObject:[NSValue valueWithCGPoint:CGPointMake(39.8759700000,
  269. 124.4695370000)]];
  270. [polygonOfChina
  271. addObject:[NSValue valueWithCGPoint:CGPointMake(41.7350890000,
  272. 126.9531720000)]];
  273. [polygonOfChina
  274. addObject:[NSValue valueWithCGPoint:CGPointMake(41.5142160000,
  275. 128.3145720000)]];
  276. [polygonOfChina
  277. addObject:[NSValue valueWithCGPoint:CGPointMake(42.9842081790,
  278. 131.0676468344)]];
  279. [polygonOfChina
  280. addObject:[NSValue valueWithCGPoint:CGPointMake(45.2690810000,
  281. 131.8468530000)]];
  282. [polygonOfChina
  283. addObject:[NSValue valueWithCGPoint:CGPointMake(45.0608370000,
  284. 133.0610740000)]];
  285. [polygonOfChina
  286. addObject:[NSValue valueWithCGPoint:CGPointMake(48.4480260000,
  287. 135.0111880000)]];
  288. [polygonOfChina
  289. addObject:[NSValue valueWithCGPoint:CGPointMake(48.0054800000,
  290. 131.6628800000)]];
  291. [polygonOfChina
  292. addObject:[NSValue valueWithCGPoint:CGPointMake(50.2270740000,
  293. 127.6890640000)]];
  294. [polygonOfChina
  295. addObject:[NSValue valueWithCGPoint:CGPointMake(53.3516070000,
  296. 125.3710040000)]];
  297. [polygonOfChina
  298. addObject:[NSValue valueWithCGPoint:CGPointMake(53.4176040000,
  299. 119.9254040000)]];
  300. [polygonOfChina
  301. addObject:[NSValue valueWithCGPoint:CGPointMake(47.5590810000,
  302. 115.1421070000)]];
  303. [polygonOfChina
  304. addObject:[NSValue valueWithCGPoint:CGPointMake(47.1339370000,
  305. 119.1159230000)]];
  306. [polygonOfChina
  307. addObject:[NSValue valueWithCGPoint:CGPointMake(44.8256460000,
  308. 111.2786750000)]];
  309. [polygonOfChina
  310. addObject:[NSValue valueWithCGPoint:CGPointMake(42.5293560000,
  311. 109.2549720000)]];
  312. [polygonOfChina
  313. addObject:[NSValue valueWithCGPoint:CGPointMake(43.2598160000,
  314. 97.2967290000)]];
  315. [polygonOfChina
  316. addObject:[NSValue valueWithCGPoint:CGPointMake(45.4247620000,
  317. 90.9680590000)]];
  318. [polygonOfChina
  319. addObject:[NSValue valueWithCGPoint:CGPointMake(47.8075570000,
  320. 90.6737020000)]];
  321. [polygonOfChina
  322. addObject:[NSValue valueWithCGPoint:CGPointMake(49.1506690000,
  323. 87.4150810000)]];
  324. });
  325. return polygonOfChina;
  326. }
  327. @end