package com.jiuqi.mobile.lbs.intf.oth; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import com.jqmobile.core.utils.json.base64.Base64; import com.jqmobile.core.utils.json.base64.Base64DecodingException; class BaiDuGisUtil{ public static GisPoint correction_84(double dLong, double dLat) { // Map<String, String> m = new HashMap<String, String>(); GisPoint point = new GisPoint(); point.setGcType(GisCoordinateType.BaiDu); try{ String lon = "0"; String lat = "0"; Socket s = new Socket("api.map.baidu.com", 80); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8")); OutputStream out = s.getOutputStream(); StringBuffer sb = new StringBuffer("GET /ag/coord/convert?from=0&to=4"); sb.append("&x=" + dLong + "&y=" + dLat); sb.append("&callback=BMap.Convertor.cbk_3976 HTTP/1.1\r\n"); sb.append("User-Agent: Java/1.6.0_20\r\n"); sb.append("Host: api.map.baidu.com:80\r\n"); sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"); sb.append("Connection: Close\r\n"); sb.append("\r\n"); out.write(sb.toString().getBytes()); String json = ""; String tmp = ""; while ((tmp = br.readLine()) != null) { // System.out.println(tmp); json += tmp; } int start = json.indexOf("cbk_3976"); int end = json.lastIndexOf("}"); if (start != -1 && end != -1 && json.contains("\"x\":\"")) { json = json.substring(start, end); String[] p = json.split(","); String x = p[1].split(":")[1].replace("\"", ""); String y = p[2].split(":")[1].replace("\"", ""); lon = new String(decode(x)); lat = new String(decode(y)); // return (new String(decode(x)) + "," + new String(decode(y))); } // else { // lon = xx; // lat = yy; // } out.close(); br.close(); point.setLatitude(Double.parseDouble(lat)); point.setLongitude(Double.parseDouble(lon)); }catch(Exception e){ e.printStackTrace(); } // m.put("x", lon); // m.put("y", lat); return point; } public static byte[] decode(String str) { byte[] bt = null; try { return Base64.decode(str); // BASE64Decoder decoder = new BASE64Decoder(); // bt = decoder.decodeBuffer(str); } catch (Base64DecodingException e) { e.printStackTrace(); } return bt; } public static GisPoint correction_02(double dLong, double dLat) { // Map<String, String> m = new HashMap<String, String>(); GisPoint point = new GisPoint(); point.setGcType(GisCoordinateType.BaiDu); try{ String lon = "0"; String lat = "0"; Socket s = new Socket("api.map.baidu.com", 80); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8")); OutputStream out = s.getOutputStream(); StringBuffer sb = new StringBuffer("GET /ag/coord/convert?from=2&to=4"); sb.append("&x=" + dLong + "&y=" + dLat); sb.append("&callback=BMap.Convertor.cbk_3976 HTTP/1.1\r\n"); sb.append("User-Agent: Java/1.6.0_20\r\n"); sb.append("Host: api.map.baidu.com:80\r\n"); sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"); sb.append("Connection: Close\r\n"); sb.append("\r\n"); out.write(sb.toString().getBytes()); String json = ""; String tmp = ""; while ((tmp = br.readLine()) != null) { // System.out.println(tmp); json += tmp; } int start = json.indexOf("cbk_3976"); int end = json.lastIndexOf("}"); if (start != -1 && end != -1 && json.contains("\"x\":\"")) { json = json.substring(start, end); String[] p = json.split(","); String x = p[1].split(":")[1].replace("\"", ""); String y = p[2].split(":")[1].replace("\"", ""); lon = new String(decode(x)); lat = new String(decode(y)); // return (new String(decode(x)) + "," + new String(decode(y))); } // else { // lon = xx; // lat = yy; // } out.close(); br.close(); point.setLatitude(Double.parseDouble(lat)); point.setLongitude(Double.parseDouble(lon)); }catch(Exception e){ e.printStackTrace(); } // m.put("x", lon); // m.put("y", lat); return point; } }