CLLocation+Sino.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // CLLocation+Sino.h
  3. //
  4. // Created by i0xbean@gmail.com on 13-4-26.
  5. // 火星坐标系转换扩展
  6. #import "CLLocation+Sino.h"
  7. void transform_earth_2_mars(double lat, double lng, double* tarLat, double* tarLng);
  8. void transform_mars_2_bear_paw(double lat, double lng, double* tarLat, double* tarLng);
  9. void transform_bear_paw_2_mars(double lat, double lng, double* tarLat, double* tarLng);
  10. @implementation CLLocation (Sino)
  11. - (CLLocation*)locationMarsFromEarth;
  12. {
  13. double lat = 0.0;
  14. double lng = 0.0;
  15. transform_earth_2_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
  16. return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
  17. altitude:self.altitude
  18. horizontalAccuracy:self.horizontalAccuracy
  19. verticalAccuracy:self.verticalAccuracy
  20. course:self.course
  21. speed:self.speed
  22. timestamp:self.timestamp];
  23. }
  24. +(CLLocationCoordinate2D) CoordMars:(double)lat :(double)lng
  25. {
  26. double marslat = 0.0;
  27. double marslng = 0.0;
  28. transform_earth_2_mars( lat, lng, &marslat, &marslng);
  29. CLLocationCoordinate2D cMars = CLLocationCoordinate2DMake(marslat, marslng);
  30. return cMars;
  31. }
  32. - (CLLocation*)locationEarthFromMars;
  33. {
  34. // 二分法查纠偏文件
  35. // http://xcodev.com/131.html
  36. return nil;
  37. }
  38. - (CLLocation*)locationBearPawFromMars;
  39. {
  40. double lat = 0.0;
  41. double lng = 0.0;
  42. transform_mars_2_bear_paw(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
  43. return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
  44. altitude:self.altitude
  45. horizontalAccuracy:self.horizontalAccuracy
  46. verticalAccuracy:self.verticalAccuracy
  47. course:self.course
  48. speed:self.speed
  49. timestamp:self.timestamp];
  50. }
  51. - (CLLocation*)locationMarsFromBearPaw;
  52. {
  53. double lat = 0.0;
  54. double lng = 0.0;
  55. transform_bear_paw_2_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
  56. return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
  57. altitude:self.altitude
  58. horizontalAccuracy:self.horizontalAccuracy
  59. verticalAccuracy:self.verticalAccuracy
  60. course:self.course
  61. speed:self.speed
  62. timestamp:self.timestamp];
  63. }
  64. @end
  65. // --- transform_earth_2_mars ---
  66. // 参考来源:https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936
  67. // Krasovsky 1940
  68. //
  69. // a = 6378245.0, 1/f = 298.3
  70. // b = a * (1 - f)
  71. // ee = (a^2 - b^2) / a^2;
  72. const double a = 6378245.0;
  73. const double ee = 0.00669342162296594323;
  74. bool transform_sino_out_china(double lat, double lon)
  75. {
  76. if (lon < 72.004 || lon > 137.8347)
  77. return true;
  78. if (lat < 0.8293 || lat > 55.8271)
  79. return true;
  80. return false;
  81. }
  82. double transform_earth_2_mars_lat(double x, double y)
  83. {
  84. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
  85. ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
  86. ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
  87. ret += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
  88. return ret;
  89. }
  90. double transform_earth_2_mars_lng(double x, double y)
  91. {
  92. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
  93. ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
  94. ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
  95. ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
  96. return ret;
  97. }
  98. void transform_earth_2_mars(double lat, double lng, double* tarLat, double* tarLng)
  99. {
  100. if (transform_sino_out_china(lat, lng))
  101. {
  102. *tarLat = lat;
  103. *tarLng = lng;
  104. return;
  105. }
  106. double dLat = transform_earth_2_mars_lat(lng - 105.0, lat - 35.0);
  107. double dLon = transform_earth_2_mars_lng(lng - 105.0, lat - 35.0);
  108. double radLat = lat / 180.0 * M_PI;
  109. double magic = sin(radLat);
  110. magic = 1 - ee * magic * magic;
  111. double sqrtMagic = sqrt(magic);
  112. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
  113. dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
  114. *tarLat = lat + dLat;
  115. *tarLng = lng + dLon;
  116. }
  117. // --- transform_earth_2_mars end ---
  118. // --- transform_mars_vs_bear_paw ---
  119. // 参考来源:http://blog.woodbunny.com/post-68.html
  120. const double x_pi = M_PI * 3000.0 / 180.0;
  121. void transform_mars_2_bear_paw(double gg_lat, double gg_lon, double *bd_lat, double *bd_lon)
  122. {
  123. double x = gg_lon, y = gg_lat;
  124. double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
  125. double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
  126. *bd_lon = z * cos(theta) + 0.0065;
  127. *bd_lat = z * sin(theta) + 0.006;
  128. }
  129. void transform_bear_paw_2_mars(double bd_lat, double bd_lon, double *gg_lat, double *gg_lon)
  130. {
  131. double x = bd_lon - 0.0065, y = bd_lat - 0.006;
  132. double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
  133. double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
  134. *gg_lon = z * cos(theta);
  135. *gg_lat = z * sin(theta);
  136. }
  137. // --- transform_mars_vs_bear_paw end ---