wgs2mars.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Usage: var gcjloc = transformFromWGSToGCJ(lng,lat);
  3. * Source: https://github.com/hiwanz/wgs2mars.js.git
  4. */
  5. (function (root, factory) {
  6. if (typeof define === 'function' && define.amd) {
  7. // AMD. Register as an anonymous module.
  8. define([], factory);
  9. } else if (typeof module === 'object' && module.exports) {
  10. // Node. Does not work with strict CommonJS, but
  11. // only CommonJS-like environments that support module.exports,
  12. // like Node.
  13. module.exports = factory();
  14. } else {
  15. // Browser globals (root is window)
  16. root.transformFromWGSToGCJ = factory();
  17. }
  18. }(this, function () {
  19. // const PI
  20. var PI = 3.14159265358979324;
  21. // Krasovsky 1940
  22. //
  23. // a = 6378245.0, 1/f = 298.3
  24. // b = a * (1 - f)
  25. // ee = (a^2 - b^2) / a^2;
  26. var a = 6378245.0;
  27. var ee = 0.00669342162296594323;
  28. function Rectangle(lng1, lat1, lng2, lat2) {
  29. this.west = Math.min(lng1, lng2);
  30. this.north = Math.max(lat1, lat2);
  31. this.east = Math.max(lng1, lng2);
  32. this.south = Math.min(lat1, lat2);
  33. }
  34. function isInRect(rect, lon, lat) {
  35. return rect.west <= lon && rect.east >= lon && rect.north >= lat && rect.south <= lat;
  36. }
  37. //China region - raw data
  38. var region = [
  39. new Rectangle(79.446200, 49.220400, 96.330000,42.889900),
  40. new Rectangle(109.687200, 54.141500, 135.000200, 39.374200),
  41. new Rectangle(73.124600, 42.889900, 124.143255, 29.529700),
  42. new Rectangle(82.968400, 29.529700, 97.035200, 26.718600),
  43. new Rectangle(97.025300, 29.529700, 124.367395, 20.414096),
  44. new Rectangle(107.975793, 20.414096, 111.744104, 17.871542)
  45. ];
  46. //China excluded region - raw data
  47. var exclude = [
  48. new Rectangle(119.921265, 25.398623, 122.497559, 21.785006),
  49. new Rectangle(101.865200, 22.284000, 106.665000, 20.098800),
  50. new Rectangle(106.452500, 21.542200, 108.051000, 20.487800),
  51. new Rectangle(109.032300, 55.817500, 119.127000, 50.325700),
  52. new Rectangle(127.456800, 55.817500, 137.022700, 49.557400),
  53. new Rectangle(131.266200, 44.892200, 137.022700, 42.569200),
  54. // hongkong
  55. new Rectangle(113.837108, 22.44151, 114.408397, 22.167709)
  56. ];
  57. function isInChina(lon, lat) {
  58. for (var i = 0; i < region.length; i++) {
  59. if (isInRect(region[i], lon, lat))
  60. {
  61. for (var j = 0; j < exclude.length; j++)
  62. {
  63. if (isInRect(exclude[j], lon, lat))
  64. {
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. function transformLat(x, y){
  74. 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));
  75. ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
  76. ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
  77. ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
  78. return ret;
  79. }
  80. function transformLon(x, y){
  81. var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  82. ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
  83. ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
  84. ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
  85. return ret;
  86. }
  87. // World Geodetic System ==> Mars Geodetic System
  88. function transform(wgLon,wgLat){
  89. var mgLoc = {};
  90. if (!isInChina(wgLon, wgLat)){
  91. mgLoc = {
  92. lat: wgLat,
  93. lng: wgLon
  94. };
  95. return mgLoc;
  96. }
  97. var dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
  98. var dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
  99. var radLat = wgLat / 180.0 * PI;
  100. var magic = Math.sin(radLat);
  101. magic = 1 - ee * magic * magic;
  102. var sqrtMagic = Math.sqrt(magic);
  103. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
  104. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);
  105. mgLoc = {
  106. lat: wgLat + dLat,
  107. lng: wgLon + dLon
  108. };
  109. return mgLoc;
  110. }
  111. return transform;
  112. }));