8d331c5fbe0ec8f025d321b0d97f0de732b017e3.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.cockpit.util;
  2. import com.cockpit.vo.Gps;
  3. /**
  4. * 各地图API坐标系统比较与转换;
  5. * WGS84坐标系:即地球坐标系,国际上通用的坐标系。设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
  6. * 谷歌地图采用的是WGS84地理坐标系(中国范围除外);
  7. * GCJ02坐标系:即火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
  8. * 谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系; BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系;
  9. * 搜狗坐标系、图吧坐标系等,估计也是在GCJ02基础上加密而成的。 chenhua
  10. */
  11. public class PositionUtil {
  12. public static final String BAIDU_LBS_TYPE = "bd09ll";
  13. public static double pi = 3.1415926535897932384626;
  14. public static double a = 6378245.0;
  15. public static double ee = 0.00669342162296594323;
  16. /**
  17. * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
  18. *
  19. * @param lat
  20. * @param lon
  21. * @return
  22. */
  23. public static Gps gps84_To_Gcj02(double lat, double lon) {
  24. if (outOfChina(lat, lon)) {
  25. return null;
  26. }
  27. double dLat = transformLat(lon - 105.0, lat - 35.0);
  28. double dLon = transformLon(lon - 105.0, lat - 35.0);
  29. double radLat = lat / 180.0 * pi;
  30. double magic = Math.sin(radLat);
  31. magic = 1 - ee * magic * magic;
  32. double sqrtMagic = Math.sqrt(magic);
  33. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  34. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  35. double mgLat = lat + dLat;
  36. double mgLon = lon + dLon;
  37. return new Gps(mgLat, mgLon);
  38. }
  39. /**
  40. * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
  41. * */
  42. public static Gps gcj_To_Gps84(double lat, double lon) {
  43. Gps gps = transform(lat, lon);
  44. double lontitude = lon * 2 - gps.getWgLon();
  45. double latitude = lat * 2 - gps.getWgLat();
  46. return new Gps(latitude, lontitude);
  47. }
  48. /**
  49. * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
  50. *
  51. * @param gg_lat
  52. * @param gg_lon
  53. */
  54. public static Gps gcj02_To_Bd09(double gg_lat, double gg_lon) {
  55. double x = gg_lon, y = gg_lat;
  56. double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
  57. double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
  58. double bd_lon = z * Math.cos(theta) + 0.0065;
  59. double bd_lat = z * Math.sin(theta) + 0.006;
  60. return new Gps(bd_lat, bd_lon);
  61. }
  62. /**
  63. * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
  64. * bd_lat * @param bd_lon * @return
  65. */
  66. public static Gps bd09_To_Gcj02(double bd_lat, double bd_lon) {
  67. double x = bd_lon - 0.0065, y = bd_lat - 0.006;
  68. double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
  69. double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
  70. double gg_lon = z * Math.cos(theta);
  71. double gg_lat = z * Math.sin(theta);
  72. return new Gps(gg_lat, gg_lon);
  73. }
  74. /**
  75. * (BD-09)-->84
  76. * @param bd_lat
  77. * @param bd_lon
  78. * @return
  79. */
  80. public static Gps bd09_To_Gps84(double bd_lat, double bd_lon) {
  81. Gps gcj02 = PositionUtil.bd09_To_Gcj02(bd_lat, bd_lon);
  82. Gps map84 = PositionUtil.gcj_To_Gps84(gcj02.getWgLat(),
  83. gcj02.getWgLon());
  84. return map84;
  85. }
  86. public static boolean outOfChina(double lat, double lon) {
  87. if (lon < 72.004 || lon > 137.8347)
  88. return true;
  89. if (lat < 0.8293 || lat > 55.8271)
  90. return true;
  91. return false;
  92. }
  93. public static Gps transform(double lat, double lon) {
  94. if (outOfChina(lat, lon)) {
  95. return new Gps(lat, lon);
  96. }
  97. double dLat = transformLat(lon - 105.0, lat - 35.0);
  98. double dLon = transformLon(lon - 105.0, lat - 35.0);
  99. double radLat = lat / 180.0 * pi;
  100. double magic = Math.sin(radLat);
  101. magic = 1 - ee * magic * magic;
  102. double 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. double mgLat = lat + dLat;
  106. double mgLon = lon + dLon;
  107. return new Gps(mgLat, mgLon);
  108. }
  109. public static double transformLat(double x, double y) {
  110. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
  111. + 0.2 * Math.sqrt(Math.abs(x));
  112. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  113. ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  114. ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  115. return ret;
  116. }
  117. public static double transformLon(double x, double y) {
  118. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
  119. * Math.sqrt(Math.abs(x));
  120. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  121. ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  122. ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
  123. * pi)) * 2.0 / 3.0;
  124. return ret;
  125. }
  126. /**
  127. * 转换坐标度数
  128. * @param pos
  129. * @return
  130. */
  131. public double tempXY(String pos){
  132. // 22°44′52.15"″
  133. // 23°9′25.42"″
  134. // System.out.println(pos);
  135. double res = 0;
  136. try{
  137. pos = pos.substring(0,pos.length()-1);
  138. String du = pos.split("°")[0];
  139. pos = pos.split("°")[1];
  140. String feng = pos.split("′")[0];
  141. pos = pos.split("′")[1];
  142. String miao = pos.split("\"")[0];
  143. res += Double.parseDouble(du);
  144. res += Double.parseDouble(feng)/60.0;
  145. res += Double.parseDouble(miao)/3600.0;
  146. }catch(NumberFormatException e){
  147. res = 0;
  148. }catch(StringIndexOutOfBoundsException e){
  149. res = 0;
  150. }
  151. return res;
  152. };
  153. public static void main(String[] args) {
  154. // 北斗芯片获取的经纬度为WGS84地理坐标 31.426896,119.496145
  155. Gps gps = new Gps(31.426896, 119.496145);
  156. System.out.println("gps :" + gps);
  157. Gps gcj = gps84_To_Gcj02(gps.getWgLat(), gps.getWgLon());
  158. System.out.println("gcj :" + gcj);
  159. Gps star = gcj_To_Gps84(gcj.getWgLat(), gcj.getWgLon());
  160. System.out.println("star:" + star);
  161. Gps bd = gcj02_To_Bd09(gcj.getWgLat(), gcj.getWgLon());
  162. System.out.println("bd :" + bd);
  163. Gps gcj2 = bd09_To_Gcj02(bd.getWgLat(), bd.getWgLon());
  164. System.out.println("gcj :" + gcj2);
  165. }
  166. }