transform.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //转换常数
  2. const x_pi = 3.14159265358979324 * 3000.0 / 180.0
  3. const pi = 3.14159265358979324
  4. const a = 6378245.0
  5. const ee = 0.00669342162296594323
  6. function transformLon(x, y) {
  7. var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  8. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  9. ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  10. ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  11. return ret;
  12. }
  13. function transformLat(x, y) {
  14. var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  15. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  16. ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  17. ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  18. return ret;
  19. }
  20. // 判断是否在国内
  21. // function outOfChina(lat, lon) {
  22. // if (lon 137.8347)
  23. // return true;
  24. // if (lat 55.8271)
  25. // return true;
  26. // return false;
  27. // }
  28. function outOfChina(lat, lon) {
  29. if (lon < 72.004 || lon > 137.8347)
  30. return true;
  31. if (lat < 0.8293 || lat > 55.8271)
  32. return true;
  33. return false;
  34. }
  35. /*
  36. WGS-84转换GCJ-02
  37. (即 天地图转高德、腾讯地图)
  38. */
  39. export const wgs_gcj_encrypts = (latlon) => {
  40. var point = [];
  41. // for (const latlon of latlons) {
  42. if (outOfChina(latlon.lat, latlon.lng)) {
  43. // point.push({
  44. // lat: latlon.lat,
  45. // lng: latlon.lng
  46. // })
  47. return {
  48. latitude: latlon.lat,
  49. longitude: latlon.lng
  50. }
  51. }
  52. var dLat = transformLat(latlon.lng - 105.0, latlon.lat - 35.0);
  53. var dLon = transformLon(latlon.lng - 105.0, latlon.lat - 35.0);
  54. var radLat = latlon.lat / 180.0 * pi;
  55. var magic = Math.sin(radLat);
  56. magic = 1 - ee * magic * magic;
  57. var sqrtMagic = Math.sqrt(magic);
  58. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  59. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  60. var lat = latlon.lat + dLat;
  61. var lng = latlon.lng + dLon;
  62. return {
  63. latitude: lat,
  64. longitude: lng
  65. };
  66. }
  67. /*
  68. BD-09转换GCJ-02
  69. (即 百度转高德、腾讯地图)
  70. */
  71. export const bd_google_encrypt = (latlons) => {
  72. var point = [];
  73. for (const latlon of latlons) {
  74. var x = latlon.lng - 0.0065;
  75. var y = latlon.lat - 0.006;
  76. var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  77. var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  78. var gg_lon = z * Math.cos(theta);
  79. var gg_lat = z * Math.sin(theta);
  80. point.push({
  81. lat: gg_lon,
  82. lng: gg_lat
  83. })
  84. }
  85. return point;
  86. }
  87. /*
  88. GCJ-02转换BD-09
  89. (即 高德、腾讯转百度地图)
  90. */
  91. export const google_bd_encrypt = (latlons) => {
  92. var point = [];
  93. for (const latlon of latlons) {
  94. var x = latlon.lng;
  95. var y = latlon.lat;
  96. var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  97. var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  98. var bd_lon = z * Math.cos(theta) + 0.0065;
  99. var bd_lat = z * Math.sin(theta) + 0.006;
  100. point.push({
  101. lat: bd_lat,
  102. lng: bd_lon
  103. })
  104. }
  105. return point;
  106. }
  107. /*
  108. GCJ-02 到 WGS-84 的转换
  109. (即 高德、腾讯转天地图)
  110. */
  111. export const gcj_wgs_encrypts = (latlon) => {
  112. // for (const latlon of latlons) {
  113. if (outOfChina(latlon.lat, latlon.lng)) {
  114. return {
  115. latitude: latlon.lat,
  116. longitude: latlon.lng
  117. }
  118. }
  119. var dLat = transformLat(latlon.lng - 105.0, latlon.lat - 35.0);
  120. var dLon = transformLon(latlon.lng - 105.0, latlon.lat - 35.0);
  121. var radLat = latlon.lat / 180.0 * pi;
  122. var magic = Math.sin(radLat);
  123. magic = 1 - ee * magic * magic;
  124. var sqrtMagic = Math.sqrt(magic);
  125. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  126. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  127. var lat = dLat;
  128. var lng = dLon;
  129. return {
  130. latitude: lat,
  131. longitude: lng
  132. }
  133. }
  134. module.exports = {
  135. gcj_wgs_encrypts: gcj_wgs_encrypts,
  136. wgs_gcj_encrypts: wgs_gcj_encrypts
  137. }