当前位置:   article > 正文

android 原生定位坐标转百度_android 地址转换成百度地址

android 地址转换成百度地址
  1. public class GPSUtil {
  2. public static double pi = 3.1415926535897932384626;
  3. public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  4. public static double a = 6378245.0;
  5. public static double ee = 0.00669342162296594323;
  6. public static double transformLat(double x, double y) {
  7. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
  8. + 0.2 * Math.sqrt(Math.abs(x));
  9. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  10. ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  11. ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  12. return ret;
  13. }
  14. public static double transformLon(double x, double y) {
  15. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
  16. * Math.sqrt(Math.abs(x));
  17. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  18. ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  19. ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
  20. * pi)) * 2.0 / 3.0;
  21. return ret;
  22. }
  23. public static double[] transform(double lat, double lon) {
  24. if (outOfChina(lat, lon)) {
  25. return new double[]{lat,lon};
  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 double[]{mgLat,mgLon};
  38. }
  39. public static boolean outOfChina(double lat, double lon) {
  40. if (lon < 72.004 || lon > 137.8347)
  41. return true;
  42. if (lat < 0.8293 || lat > 55.8271)
  43. return true;
  44. return false;
  45. }
  46. /**
  47. * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
  48. *
  49. * @param lat
  50. * @param lon
  51. * @return
  52. */
  53. public static double[] gps84_To_Gcj02(double lat, double lon) {
  54. if (outOfChina(lat, lon)) {
  55. return new double[]{lat,lon};
  56. }
  57. double dLat = transformLat(lon - 105.0, lat - 35.0);
  58. double dLon = transformLon(lon - 105.0, lat - 35.0);
  59. double radLat = lat / 180.0 * pi;
  60. double magic = Math.sin(radLat);
  61. magic = 1 - ee * magic * magic;
  62. double sqrtMagic = Math.sqrt(magic);
  63. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  64. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  65. double mgLat = lat + dLat;
  66. double mgLon = lon + dLon;
  67. return new double[]{mgLat, mgLon};
  68. }
  69. /**
  70. * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
  71. * */
  72. public static double[] gcj02_To_Gps84(double lat, double lon) {
  73. double[] gps = transform(lat, lon);
  74. double lontitude = lon * 2 - gps[1];
  75. double latitude = lat * 2 - gps[0];
  76. return new double[]{latitude, lontitude};
  77. }
  78. /**
  79. * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
  80. *
  81. * @param lat
  82. * @param lon
  83. */
  84. public static double[] gcj02_To_Bd09(double lat, double lon) {
  85. double x = lon, y = lat;
  86. double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  87. double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  88. double tempLon = z * Math.cos(theta) + 0.0065;
  89. double tempLat = z * Math.sin(theta) + 0.006;
  90. double[] gps = {tempLat,tempLon};
  91. return gps;
  92. }
  93. /**
  94. * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
  95. * bd_lat * @param bd_lon * @return
  96. */
  97. public static double[] bd09_To_Gcj02(double lat, double lon) {
  98. double x = lon - 0.0065, y = lat - 0.006;
  99. double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  100. double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  101. double tempLon = z * Math.cos(theta);
  102. double tempLat = z * Math.sin(theta);
  103. double[] gps = {tempLat,tempLon};
  104. return gps;
  105. }
  106. /**将gps84转为bd09
  107. * @param lat
  108. * @param lon
  109. * @return
  110. */
  111. public static double[] gps84_To_bd09(double lat,double lon){
  112. double[] gcj02 = gps84_To_Gcj02(lat,lon);
  113. double[] bd09 = gcj02_To_Bd09(gcj02[0],gcj02[1]);
  114. return bd09;
  115. }
  116. public static double[] bd09_To_gps84(double lat,double lon){
  117. double[] gcj02 = bd09_To_Gcj02(lat, lon);
  118. double[] gps84 = gcj02_To_Gps84(gcj02[0], gcj02[1]);
  119. //保留小数点后六位
  120. gps84[0] = retain6(gps84[0]);
  121. gps84[1] = retain6(gps84[1]);
  122. return gps84;
  123. }
  124. /**保留小数点后六位
  125. * @param num
  126. * @return
  127. */
  128. private static double retain6(double num){
  129. String result = String .format("%.6f", num);
  130. return Double.valueOf(result);
  131. }
  132. }
  133. public class GpsUtil
  134. {
  135. public static double pi = 3.1415926535897932384626;
  136. public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  137. public static double a = 6378245.0;
  138. public static double ee = 0.00669342162296594323;
  139. public static double TransformLat(double x, double y)
  140. {
  141. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
  142. + 0.2 * Math.Sqrt(Math.Abs(x));
  143. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
  144. ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
  145. ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
  146. return ret;
  147. }
  148. public static double TransformLon(double x, double y)
  149. {
  150. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
  151. * Math.Sqrt(Math.Abs(x));
  152. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
  153. ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
  154. ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0
  155. * pi)) * 2.0 / 3.0;
  156. return ret;
  157. }
  158. public static double[] transform(double lat, double lon)
  159. {
  160. if (OutOfChina(lat, lon))
  161. {
  162. return new double[] { lat, lon };
  163. }
  164. double dLat = TransformLat(lon - 105.0, lat - 35.0);
  165. double dLon = TransformLon(lon - 105.0, lat - 35.0);
  166. double radLat = lat / 180.0 * pi;
  167. double magic = Math.Sin(radLat);
  168. magic = 1 - ee * magic * magic;
  169. double SqrtMagic = Math.Sqrt(magic);
  170. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * SqrtMagic) * pi);
  171. dLon = (dLon * 180.0) / (a / SqrtMagic * Math.Cos(radLat) * pi);
  172. double mgLat = lat + dLat;
  173. double mgLon = lon + dLon;
  174. return new double[] { mgLat, mgLon };
  175. }
  176. public static bool OutOfChina(double lat, double lon)
  177. {
  178. if (lon < 72.004 || lon > 137.8347)
  179. return true;
  180. if (lat < 0.8293 || lat > 55.8271)
  181. return true;
  182. return false;
  183. }
  184. /**
  185. * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
  186. *
  187. * @param lat
  188. * @param lon
  189. * @return
  190. */
  191. public static double[] Gps84ToGcj02(double lat, double lon)
  192. {
  193. if (OutOfChina(lat, lon))
  194. {
  195. return new double[] { lat, lon };
  196. }
  197. double dLat = TransformLat(lon - 105.0, lat - 35.0);
  198. double dLon = TransformLon(lon - 105.0, lat - 35.0);
  199. double radLat = lat / 180.0 * pi;
  200. double magic = Math.Sin(radLat);
  201. magic = 1 - ee * magic * magic;
  202. double SqrtMagic = Math.Sqrt(magic);
  203. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * SqrtMagic) * pi);
  204. dLon = (dLon * 180.0) / (a / SqrtMagic * Math.Cos(radLat) * pi);
  205. double mgLat = lat + dLat;
  206. double mgLon = lon + dLon;
  207. return new double[] { mgLat, mgLon };
  208. }
  209. /**
  210. * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
  211. * */
  212. public static double[] Gcj02ToGps84(double lat, double lon)
  213. {
  214. double[] gps = transform(lat, lon);
  215. double lontitude = lon * 2 - gps[1];
  216. double latitude = lat * 2 - gps[0];
  217. return new double[] { latitude, lontitude };
  218. }
  219. /// <summary>
  220. /// 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
  221. /// 高德谷歌转为百度
  222. /// </summary>
  223. /// <param name="lat"></param>
  224. /// <param name="lon"></param>
  225. /// <returns></returns>
  226. public static double[] Gcj02ToBd09(double lat, double lon)
  227. {
  228. double x = lon, y = lat;
  229. double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
  230. double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
  231. double tempLon = z * Math.Cos(theta) + 0.0065;
  232. double tempLat = z * Math.Sin(theta) + 0.006;
  233. double[] gps = { tempLat, tempLon };
  234. return gps;
  235. }
  236. /// <summary>
  237. /// 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标
  238. /// 百度坐标转为高德谷歌坐标
  239. /// </summary>
  240. /// <param name="lat"></param>
  241. /// <param name="lon"></param>
  242. /// <returns></returns>
  243. public static double[] Bd09ToGcj02(double lat, double lon)
  244. {
  245. double x = lon - 0.0065, y = lat - 0.006;
  246. double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
  247. double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
  248. double tempLon = z * Math.Cos(theta);
  249. double tempLat = z * Math.Sin(theta);
  250. double[] gps = { tempLat, tempLon };
  251. return gps;
  252. }
  253. /// <summary>
  254. /// gps84转为bd09
  255. /// GPS坐标转为百度坐标
  256. /// </summary>
  257. /// <param name="lat"></param>
  258. /// <param name="lon"></param>
  259. /// <returns></returns>
  260. public static double[] Gps84ToBd09(double lat, double lon)
  261. {
  262. double[] gcj02 = Gps84ToGcj02(lat, lon);
  263. double[] bd09 = Gcj02ToBd09(gcj02[0], gcj02[1]);
  264. return bd09;
  265. }
  266. /// <summary>
  267. /// 百度坐标转成GPS坐标
  268. /// </summary>
  269. /// <param name="lat"></param>
  270. /// <param name="lon"></param>
  271. /// <returns></returns>
  272. public static double[] Bd09ToGps84(double lat, double lon)
  273. {
  274. double[] gcj02 = Bd09ToGcj02(lat, lon);
  275. double[] gps84 = Gcj02ToGps84(gcj02[0], gcj02[1]);
  276. //保留小数点后六位
  277. gps84[0] = Retain6(gps84[0]);
  278. gps84[1] = Retain6(gps84[1]);
  279. return gps84;
  280. }
  281. /// <summary>
  282. /// 保留小数点后六位
  283. /// </summary>
  284. /// <param name="num"></param>
  285. /// <returns></returns>
  286. private static double Retain6(double num)
  287. {
  288. String result = String.Format("%.6f", num);
  289. return Double.Parse(result);
  290. }
  291. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/290531
推荐阅读
相关标签
  

闽ICP备14008679号