func.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * 判断经纬度是否超出中国境内
  3. */
  4. function isLocationOutOfChina(latitude, longitude) {
  5. if (longitude < 72.004 || longitude > 137.8347 || latitude < 0.8293 || latitude > 55.8271) { return true }
  6. return false
  7. }
  8. /**
  9. * 将WGS-84(国际标准)转为GCJ-02(火星坐标):
  10. */
  11. function transformFromWGSToGCJ(latitude, longitude) {
  12. var lat = ''
  13. var lon = ''
  14. var ee = 0.00669342162296594323
  15. var a = 6378245.0
  16. var pi = 3.14159265358979324
  17. if (isLocationOutOfChina(latitude, longitude)) {
  18. lat = latitude
  19. lon = longitude
  20. } else {
  21. var adjustLat = transformLatWithXY(longitude - 105.0, latitude - 35.0)
  22. var adjustLon = transformLonWithXY(longitude - 105.0, latitude - 35.0)
  23. var radLat = latitude / 180.0 * pi
  24. var magic = Math.sin(radLat)
  25. magic = 1 - ee * magic * magic
  26. var sqrtMagic = Math.sqrt(magic)
  27. adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
  28. adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi)
  29. latitude = latitude + adjustLat
  30. longitude = longitude + adjustLon
  31. }
  32. return { latitude: latitude, longitude: longitude }
  33. }
  34. /**
  35. * 将GCJ-02(火星坐标)转为百度坐标(DB-09):
  36. */
  37. function transformFromGCJToBaidu(latitude, longitude) {
  38. var pi = 3.14159265358979324 * 3000.0 / 180.0
  39. var z = Math.sqrt(longitude * longitude + latitude * latitude) + 0.00002 * Math.sin(latitude * pi)
  40. var theta = Math.atan2(latitude, longitude) + 0.000003 * Math.cos(longitude * pi)
  41. var a_latitude = (z * Math.sin(theta) + 0.006)
  42. var a_longitude = (z * Math.cos(theta) + 0.0065)
  43. return { latitude: a_latitude, longitude: a_longitude }
  44. }
  45. /**
  46. * 将百度坐标(DB-09)转为GCJ-02(火星坐标):
  47. */
  48. function transformFromBaiduToGCJ(latitude, longitude) {
  49. var xPi = 3.14159265358979323846264338327950288 * 3000.0 / 180.0
  50. var x = longitude - 0.0065
  51. var y = latitude - 0.006
  52. var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi)
  53. var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi)
  54. var a_latitude = z * Math.sin(theta)
  55. var a_longitude = z * Math.cos(theta)
  56. return { latitude: a_latitude, longitude: a_longitude }
  57. }
  58. /**
  59. * 将GCJ-02(火星坐标)转为WGS-84(国际标准):
  60. */
  61. function transformFromGCJToWGS(latitude, longitude) {
  62. var threshold = 0.00001
  63. // The boundary
  64. var minLat = latitude - 0.5
  65. var maxLat = latitude + 0.5
  66. var minLng = longitude - 0.5
  67. var maxLng = longitude + 0.5
  68. var delta = 1
  69. var maxIteration = 30
  70. while (true) {
  71. var leftBottom = transformFromWGSToGCJ(minLat, minLng)
  72. var rightBottom = transformFromWGSToGCJ(minLat, maxLng)
  73. var leftUp = transformFromWGSToGCJ(maxLat, minLng)
  74. var midPoint = transformFromWGSToGCJ((minLat + maxLat) / 2, (minLng + maxLng) / 2)
  75. delta = Math.abs(midPoint.latitude - latitude) + Math.abs(midPoint.longitude - longitude)
  76. if (maxIteration-- <= 0 || delta <= threshold) {
  77. return { latitude: (minLat + maxLat) / 2, longitude: (minLng + maxLng) / 2 }
  78. }
  79. if (isContains({ latitude: latitude, longitude: longitude }, leftBottom, midPoint)) {
  80. maxLat = (minLat + maxLat) / 2
  81. maxLng = (minLng + maxLng) / 2
  82. } else if (isContains({ latitude: latitude, longitude: longitude }, rightBottom, midPoint)) {
  83. maxLat = (minLat + maxLat) / 2
  84. minLng = (minLng + maxLng) / 2
  85. } else if (isContains({ latitude: latitude, longitude: longitude }, leftUp, midPoint)) {
  86. minLat = (minLat + maxLat) / 2
  87. maxLng = (minLng + maxLng) / 2
  88. } else {
  89. minLat = (minLat + maxLat) / 2
  90. minLng = (minLng + maxLng) / 2
  91. }
  92. }
  93. }
  94. function isContains(point, p1, p2) {
  95. return (point.latitude >= Math.min(p1.latitude, p2.latitude) && point.latitude <= Math.max(p1.latitude, p2.latitude)) && (point.longitude >= Math.min(p1.longitude, p2.longitude) && point.longitude <= Math.max(p1.longitude, p2.longitude))
  96. }
  97. function transformLatWithXY(x, y) {
  98. var pi = 3.14159265358979324
  99. var lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x))
  100. lat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0
  101. lat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0
  102. lat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0
  103. return lat
  104. }
  105. function transformLonWithXY(x, y) {
  106. var pi = 3.14159265358979324
  107. var lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x))
  108. lon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0
  109. lon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0
  110. lon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0
  111. return lon
  112. }
  113. export {
  114. isLocationOutOfChina,
  115. transformFromWGSToGCJ,
  116. transformFromGCJToBaidu,
  117. transformFromBaiduToGCJ,
  118. transformFromGCJToWGS
  119. }