LngLonUtil.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.steerinfo.route.util;
  2. /**
  3. * GPS84、高德、百度、腾讯编码互转
  4. */
  5. public class LngLonUtil {
  6. public static double pi = 3.1415926535897932384626;
  7. public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  8. public static double a = 6378245.0;
  9. public static double ee = 0.00669342162296594323;
  10. public static double transformLat(double x, double y) {
  11. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  12. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  13. ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  14. ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  15. return ret;
  16. }
  17. public static double transformLon(double x, double y) {
  18. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  19. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  20. ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  21. ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  22. return ret;
  23. }
  24. public static double[] transform(double lat, double lon) {
  25. if (outOfChina(lat, lon)) {
  26. return new double[]{lat,lon};
  27. }
  28. double dLat = transformLat(lon - 105.0, lat - 35.0);
  29. double dLon = transformLon(lon - 105.0, lat - 35.0);
  30. double radLat = lat / 180.0 * pi;
  31. double magic = Math.sin(radLat);
  32. magic = 1 - ee * magic * magic;
  33. double sqrtMagic = Math.sqrt(magic);
  34. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  35. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  36. double mgLat = lat + dLat;
  37. double mgLon = lon + dLon;
  38. return new double[]{mgLat,mgLon};
  39. }
  40. /**
  41. * 判断是否在中国
  42. * @param lat
  43. * @param lon
  44. * @return
  45. */
  46. public static boolean outOfChina(double lat, double lon) {
  47. if (lon < 72.004 || lon > 137.8347)
  48. return true;
  49. if (lat < 0.8293 || lat > 55.8271)
  50. return true;
  51. return false;
  52. }
  53. /**
  54. * 84 ==》 高德/腾讯
  55. * @param lat
  56. * @param lon
  57. * @return
  58. */
  59. public static Double[] gps84_To_Gcj02(double lat, double lon) {
  60. if (outOfChina(lat, lon)) {
  61. return new Double[]{lat,lon};
  62. }
  63. double dLat = transformLat(lon - 105.0, lat - 35.0);
  64. double dLon = transformLon(lon - 105.0, lat - 35.0);
  65. double radLat = lat / 180.0 * pi;
  66. double magic = Math.sin(radLat);
  67. magic = 1 - ee * magic * magic;
  68. double sqrtMagic = Math.sqrt(magic);
  69. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  70. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  71. double mgLat = lat + dLat;
  72. double mgLon = lon + dLon;
  73. return new Double[]{mgLat, mgLon};
  74. }
  75. /**
  76. * 高德/腾讯 ==》 84
  77. * @param lon * @param lat * @return
  78. * */
  79. public static double[] gcj02_To_Gps84(double lat, double lon) {
  80. double[] gps = transform(lat, lon);
  81. double lontitude = lon * 2 - gps[1];
  82. double latitude = lat * 2 - gps[0];
  83. return new double[]{latitude, lontitude};
  84. }
  85. /**
  86. * 高德/腾讯 == 》 百度
  87. * @param lat
  88. * @param lon
  89. */
  90. public static double[] gcj02_To_Bd09(double lat, double lon) {
  91. double x = lon, y = lat;
  92. double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  93. double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  94. double tempLon = z * Math.cos(theta) + 0.0065;
  95. double tempLat = z * Math.sin(theta) + 0.006;
  96. double[] gps = {tempLat,tempLon};
  97. return gps;
  98. }
  99. /**
  100. * 百度 == 》 高德/腾讯
  101. * @param lat
  102. * @param lon
  103. */
  104. public static double[] bd09_To_Gcj02(double lat, double lon) {
  105. double x = lon - 0.0065, y = lat - 0.006;
  106. double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  107. double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  108. double tempLon = z * Math.cos(theta);
  109. double tempLat = z * Math.sin(theta);
  110. double[] gps = {tempLat,tempLon};
  111. return gps;
  112. }
  113. /**
  114. * 84 == 》 百度
  115. * @param lat
  116. * @param lon
  117. * @return
  118. */
  119. public static double[] gps84_To_bd09(double lat,double lon){
  120. Double[] gcj02 = gps84_To_Gcj02(lat,lon);
  121. double[] bd09 = gcj02_To_Bd09(gcj02[0],gcj02[1]);
  122. return bd09;
  123. }
  124. /**
  125. * 百度 == 》 84
  126. * @param lat
  127. * @param lon
  128. * @return
  129. */
  130. public static double[] bd09_To_gps84(double lat,double lon){
  131. double[] gcj02 = bd09_To_Gcj02(lat, lon);
  132. double[] gps84 = gcj02_To_Gps84(gcj02[0], gcj02[1]);
  133. //保留小数点后六位
  134. gps84[0] = retain6(gps84[0]);
  135. gps84[1] = retain6(gps84[1]);
  136. return gps84;
  137. }
  138. /*
  139. * 保留小数点后六位
  140. * @param num
  141. * @return
  142. */
  143. private static double retain6(double num){
  144. String result = String.format("%.6f", num);
  145. return Double.valueOf(result);
  146. }
  147. }