ConvertGps.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using GMap.NET;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace HuarunTerminal.core
  7. {
  8. public static class ConvertGPS
  9. {
  10. private static double pi = 3.1415926535897932384626;
  11. private static double a = 6378245.0;
  12. private static double ee = 0.00669342162296594323;
  13. private static double bd_pi = 3.14159265358979324 * 3000.0 / 180.0;
  14. static Boolean outOfChina(double lat, double lon)
  15. {
  16. if (lon < 72.004 || lon > 137.8347)
  17. return true;
  18. if (lat < 0.8293 || lat > 55.8271)
  19. return true;
  20. return false;
  21. }
  22. static double transformLat(double x, double y)
  23. {
  24. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
  25. + 0.2 * Math.Sqrt(Math.Abs(x));
  26. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
  27. ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
  28. ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
  29. return ret;
  30. }
  31. static double transformLon(double x, double y)
  32. {
  33. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
  34. * Math.Sqrt(Math.Abs(x));
  35. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
  36. ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
  37. ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0
  38. * pi)) * 2.0 / 3.0;
  39. return ret;
  40. }
  41. /**
  42. * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
  43. *
  44. * @param lat
  45. * @param lon
  46. * @return
  47. */
  48. public static PointLatLng gps84_To_Gcj02(PointLatLng Gpoint)
  49. {
  50. if (outOfChina(Gpoint.Lat, Gpoint.Lng))
  51. {
  52. return new PointLatLng(0, 0);
  53. }
  54. double dLat = transformLat(Gpoint.Lng - 105.0, Gpoint.Lat - 35.0);
  55. double dLon = transformLon(Gpoint.Lng - 105.0, Gpoint.Lat - 35.0);
  56. double radLat = Gpoint.Lat / 180.0 * pi;
  57. double magic = Math.Sin(radLat);
  58. magic = 1 - ee * magic * magic;
  59. double sqrtMagic = Math.Sqrt(magic);
  60. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  61. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
  62. double mgLat = Gpoint.Lat + dLat;
  63. double mgLon = Gpoint.Lng + dLon;
  64. return new PointLatLng(mgLat, mgLon);
  65. }
  66. static PointLatLng transform(PointLatLng Gpoint)
  67. {
  68. if (outOfChina(Gpoint.Lat, Gpoint.Lng))
  69. {
  70. return new PointLatLng(Gpoint.Lat, Gpoint.Lng);
  71. }
  72. double dLat = transformLat(Gpoint.Lng - 105.0, Gpoint.Lat - 35.0);
  73. double dLon = transformLon(Gpoint.Lng - 105.0, Gpoint.Lat - 35.0);
  74. double radLat = Gpoint.Lat / 180.0 * pi;
  75. double magic = Math.Sin(radLat);
  76. magic = 1 - ee * magic * magic;
  77. double sqrtMagic = Math.Sqrt(magic);
  78. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  79. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
  80. double mgLat = Gpoint.Lat + dLat;
  81. double mgLon = Gpoint.Lng + dLon;
  82. return new PointLatLng(mgLat, mgLon);
  83. }
  84. /**
  85. * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
  86. * */
  87. public static PointLatLng gcj02_To_Gps84(PointLatLng Gpoint)
  88. {
  89. PointLatLng gps = transform(Gpoint);
  90. double lontitude = Gpoint.Lng * 2 - gps.Lng;
  91. double latitude = Gpoint.Lat * 2 - gps.Lat;
  92. return new PointLatLng(latitude, lontitude);
  93. }
  94. /**
  95. * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
  96. *
  97. * @param gg_lat
  98. * @param gg_lon
  99. */
  100. public static PointLatLng gcj02_To_Bd09(PointLatLng Gpoint)
  101. {
  102. double x = Gpoint.Lng, y = Gpoint.Lat;
  103. double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * bd_pi);
  104. double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * bd_pi);
  105. double bd_lon = z * Math.Cos(theta) + 0.0065;
  106. double bd_lat = z * Math.Sin(theta) + 0.006;
  107. return new PointLatLng(bd_lat, bd_lon);
  108. }
  109. /**
  110. * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
  111. * bd_lat * @param bd_lon * @return
  112. */
  113. public static PointLatLng bd09_To_Gcj02(PointLatLng bdPoint)
  114. {
  115. double x = bdPoint.Lng - 0.0065, y = bdPoint.Lat - 0.006;
  116. double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * bd_pi);
  117. double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * bd_pi);
  118. double gg_lon = z * Math.Cos(theta);
  119. double gg_lat = z * Math.Sin(theta);
  120. return new PointLatLng(gg_lat, gg_lon);
  121. }
  122. /**
  123. * (BD-09)-->84
  124. * @param bd_lat
  125. * @param bd_lon
  126. * @return
  127. */
  128. public static PointLatLng bd09_To_Gps84(PointLatLng bdPoint)
  129. {
  130. PointLatLng gcj02 = bd09_To_Gcj02(bdPoint);
  131. PointLatLng map84 = gcj02_To_Gps84(gcj02);
  132. return map84;
  133. }
  134. /**
  135. * 84-->(BD-09)
  136. * @param bd_lat
  137. * @param bd_lon
  138. * @return
  139. */
  140. public static PointLatLng Gps84_To_bd09(PointLatLng gpsPoint)
  141. {
  142. PointLatLng gcj02 = gps84_To_Gcj02(gpsPoint);
  143. PointLatLng bd09 = gcj02_To_Bd09(gcj02);
  144. return bd09;
  145. }
  146. }
  147. }