Commit 40fb794798897da0873db4bb03fe04200d874003

Authored by 648540858
1 parent fcf8c401

使用临时方案解决更新通道调用百度API问题

src/main/java/com/genersoft/iot/vmp/utils/Coordtransform.java 0 → 100644
  1 +package com.genersoft.iot.vmp.utils;
  2 +
  3 +/**
  4 + * 坐标转换
  5 + * 一个提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换的工具类
  6 + * 参考https://github.com/wandergis/coordtransform 写的Java版本
  7 + * @author Xinconan
  8 + * @date 2016-03-18
  9 + * @url https://github.com/xinconan/coordtransform
  10 + */
  11 +public class Coordtransform {
  12 +
  13 + private static double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
  14 + private static double PI = 3.1415926535897932384626;
  15 + private static double a = 6378245.0;
  16 + private static double ee = 0.00669342162296594323;
  17 +
  18 + /**
  19 + * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
  20 + * 即 百度 转 谷歌、高德
  21 + * @param bd_lon
  22 + * @param bd_lat
  23 + * @return Double[lon,lat]
  24 + */
  25 + public static Double[] BD09ToGCJ02(Double bd_lon,Double bd_lat){
  26 + double x = bd_lon - 0.0065;
  27 + double y = bd_lat - 0.006;
  28 + double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI);
  29 + double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI);
  30 + Double[] arr = new Double[2];
  31 + arr[0] = z * Math.cos(theta);
  32 + arr[1] = z * Math.sin(theta);
  33 + return arr;
  34 + }
  35 +
  36 + /**
  37 + * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
  38 + * 即谷歌、高德 转 百度
  39 + * @param gcj_lon
  40 + * @param gcj_lat
  41 + * @return Double[lon,lat]
  42 + */
  43 + public static Double[] GCJ02ToBD09(Double gcj_lon,Double gcj_lat){
  44 + double z = Math.sqrt(gcj_lon * gcj_lon + gcj_lat * gcj_lat) + 0.00002 * Math.sin(gcj_lat * x_PI);
  45 + double theta = Math.atan2(gcj_lat, gcj_lon) + 0.000003 * Math.cos(gcj_lon * x_PI);
  46 + Double[] arr = new Double[2];
  47 + arr[0] = z * Math.cos(theta) + 0.0065;
  48 + arr[1] = z * Math.sin(theta) + 0.006;
  49 + return arr;
  50 + }
  51 +
  52 + /**
  53 + * WGS84转GCJ02
  54 + * @param wgs_lon
  55 + * @param wgs_lat
  56 + * @return Double[lon,lat]
  57 + */
  58 + public static Double[] WGS84ToGCJ02(Double wgs_lon,Double wgs_lat){
  59 + if(outOfChina(wgs_lon, wgs_lat)){
  60 + return new Double[]{wgs_lon,wgs_lat};
  61 + }
  62 + double dlat = transformlat(wgs_lon - 105.0, wgs_lat - 35.0);
  63 + double dlng = transformlng(wgs_lon - 105.0, wgs_lat - 35.0);
  64 + double radlat = wgs_lat / 180.0 * PI;
  65 + double magic = Math.sin(radlat);
  66 + magic = 1 - ee * magic * magic;
  67 + double sqrtmagic = Math.sqrt(magic);
  68 + dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
  69 + dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
  70 + Double[] arr = new Double[2];
  71 + arr[0] = wgs_lon + dlng;
  72 + arr[1] = wgs_lat + dlat;
  73 + return arr;
  74 + }
  75 +
  76 + /**
  77 + * GCJ02转WGS84
  78 + * @param gcj_lon
  79 + * @param gcj_lat
  80 + * @return Double[lon,lat]
  81 + */
  82 + public static Double[] GCJ02ToWGS84(Double gcj_lon,Double gcj_lat){
  83 + if(outOfChina(gcj_lon, gcj_lat)){
  84 + return new Double[]{gcj_lon,gcj_lat};
  85 + }
  86 + double dlat = transformlat(gcj_lon - 105.0, gcj_lat - 35.0);
  87 + double dlng = transformlng(gcj_lon - 105.0, gcj_lat - 35.0);
  88 + double radlat = gcj_lat / 180.0 * PI;
  89 + double magic = Math.sin(radlat);
  90 + magic = 1 - ee * magic * magic;
  91 + double sqrtmagic = Math.sqrt(magic);
  92 + dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
  93 + dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
  94 + double mglat = gcj_lat + dlat;
  95 + double mglng = gcj_lon + dlng;
  96 + return new Double[]{gcj_lon * 2 - mglng, gcj_lat * 2 - mglat};
  97 + }
  98 +
  99 + private static Double transformlat(double lng, double lat) {
  100 + double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
  101 + ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
  102 + ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
  103 + ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
  104 + return ret;
  105 + }
  106 +
  107 + private static Double transformlng(double lng,double lat) {
  108 + double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
  109 + ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
  110 + ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
  111 + ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
  112 + return ret;
  113 + }
  114 +
  115 + /**
  116 + * outOfChina
  117 + * @描述: 判断是否在国内,不在国内则不做偏移
  118 + * @param lng
  119 + * @param lat
  120 + * @return {boolean}
  121 + */
  122 + private static boolean outOfChina(Double lng,Double lat) {
  123 + return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
  124 + };
  125 +
  126 +}
... ...
src/main/java/com/genersoft/iot/vmp/utils/GpsUtil.java
... ... @@ -17,48 +17,57 @@ public class GpsUtil {
17 17 public static BaiduPoint Wgs84ToBd09(String xx, String yy) {
18 18  
19 19  
20   - try {
21   - Socket s = new Socket("api.map.baidu.com", 80);
22   - BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
23   - OutputStream out = s.getOutputStream();
24   - StringBuffer sb = new StringBuffer("GET /ag/coord/convert?from=0&to=4");
25   - sb.append("&x=" + xx + "&y=" + yy);
26   - sb.append("&callback=BMap.Convertor.cbk_3976 HTTP/1.1\r\n");
27   - sb.append("User-Agent: Java/1.6.0_20\r\n");
28   - sb.append("Host: api.map.baidu.com:80\r\n");
29   - sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
30   - sb.append("Connection: Close\r\n");
31   - sb.append("\r\n");
32   - out.write(sb.toString().getBytes());
33   - String json = "";
34   - String tmp = "";
35   - while ((tmp = br.readLine()) != null) {
36   - // logger.info(tmp);
37   - json += tmp;
38   - }
  20 +// try {
  21 +// Socket s = new Socket("api.map.baidu.com", 80);
  22 +// BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
  23 +// OutputStream out = s.getOutputStream();
  24 +// StringBuffer sb = new StringBuffer("GET /ag/coord/convert?from=0&to=4");
  25 +// sb.append("&x=" + xx + "&y=" + yy);
  26 +// sb.append("&callback=BMap.Convertor.cbk_3976 HTTP/1.1\r\n");
  27 +// sb.append("User-Agent: Java/1.6.0_20\r\n");
  28 +// sb.append("Host: api.map.baidu.com:80\r\n");
  29 +// sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
  30 +// sb.append("Connection: Close\r\n");
  31 +// sb.append("\r\n");
  32 +// out.write(sb.toString().getBytes());
  33 +// String json = "";
  34 +// String tmp = "";
  35 +// while ((tmp = br.readLine()) != null) {
  36 +// // logger.info(tmp);
  37 +// json += tmp;
  38 +// }
  39 +//
  40 +// s.close();
  41 +// int start = json.indexOf("cbk_3976");
  42 +// int end = json.lastIndexOf("}");
  43 +// if (start != -1 && end != -1 && json.contains("\"x\":\"")) {
  44 +// json = json.substring(start, end);
  45 +// String[] point = json.split(",");
  46 +// String x = point[1].split(":")[1].replace("\"", "");
  47 +// String y = point[2].split(":")[1].replace("\"", "");
  48 +// BaiduPoint bdPoint= new BaiduPoint();
  49 +// bdPoint.setBdLng(new String(decode(x)));
  50 +// bdPoint.setBdLat(new String(decode(y)));
  51 +// return bdPoint;
  52 +// //return (new String(decode(x)) + "," + new String(decode(y)));
  53 +// } else {
  54 +// logger.info("gps坐标无效!!");
  55 +// }
  56 +// out.close();
  57 +// br.close();
  58 +// } catch (Exception e) {
  59 +// e.printStackTrace();
  60 +// }
39 61  
40   - s.close();
41   - int start = json.indexOf("cbk_3976");
42   - int end = json.lastIndexOf("}");
43   - if (start != -1 && end != -1 && json.contains("\"x\":\"")) {
44   - json = json.substring(start, end);
45   - String[] point = json.split(",");
46   - String x = point[1].split(":")[1].replace("\"", "");
47   - String y = point[2].split(":")[1].replace("\"", "");
48   - BaiduPoint bdPoint= new BaiduPoint();
49   - bdPoint.setBdLng(new String(decode(x)));
50   - bdPoint.setBdLat(new String(decode(y)));
51   - return bdPoint;
52   - //return (new String(decode(x)) + "," + new String(decode(y)));
53   - } else {
54   - logger.info("gps坐标无效!!");
55   - }
56   - out.close();
57   - br.close();
58   - } catch (Exception e) {
59   - e.printStackTrace();
60   - }
61   - return null;
  62 +
  63 + double lng = Double.parseDouble(xx);
  64 + double lat = Double.parseDouble(yy);
  65 + Double[] gcj02 = Coordtransform.WGS84ToGCJ02(lng, lat);
  66 + Double[] doubles = Coordtransform.GCJ02ToBD09(gcj02[0], gcj02[1]);
  67 + BaiduPoint bdPoint= new BaiduPoint();
  68 + bdPoint.setBdLng(doubles[0] + "");
  69 + bdPoint.setBdLat(doubles[1] + "");
  70 + return bdPoint;
62 71 }
63 72  
64 73 /**
... ...