百度地圖API鏈接:http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition
百度地圖API中,有GPS坐標轉百度坐標的功能
http://dev.baidu.com/wiki/static/map/API/examples/?v=1.2&0_6#0&6
http接口是:http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=116.397428&y=39.90923&callback=BMap.Convertor.cbk_7594
返回結果坐標是通過base64加密的。
這個轉換算法百度是不會公開的,而且百度也沒有提供百度坐標轉成GPS坐標功能,這里我用了取巧的辦法。
百度坐標和GPS坐標轉換在很近的距離時偏差非常接近。
假設你有百度坐標:x1=116.397428,y1=39.90923
把這個坐標當成GPS坐標,通過接口獲得他的百度坐標:x2=116.41004950566,y2=39.916979519873
通過計算就可以得到GPS的坐標:
x = 2*x1-x2,y = 2*y1-y2
x=116.38480649434001
y=39.901480480127
在http://dev.baidu.com/wiki/static/map/API/examples/?v=1.2&0_6#0&6 將此坐標輸入GPS數據項中得到的結果是:116.39743826208,39.909194650838
實現的java代碼如下:
package com.dataprocess; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CopyOfTest { public static void main(String args[]){ String params = "coords=114.21892734521,29.575429778924&from=1&to=5&ak=ZQU8ZGeBf95nN8wY1cNL13NP&output=json"; String s=CopyOfTest.sendGet("http://api.map.baidu.com/geoconv/v1/?", params); String[] s1 = s.split("\\["); String[] s2 = s1[1].split(","); String regEx ="[^0-9.\\+\\-\\s]"; Pattern p = Pattern.compile(regEx); Matcher m1 = p.matcher(s2[0]); Matcher m2 = p.matcher(s2[1]); String[] arrs1=m1.replaceAll("").trim().split("\\s"); String[] arrs2=m2.replaceAll("").trim().split("\\s"); System.out.println(s1[1]); System.out.println(arrs1[0]+" "+arrs2[0]); } public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + param; URL realUrl = new URL(urlNameString); // 打開和URL之間的連接 URLConnection connection = realUrl.openConnection(); // 設置通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立實際的連接 connection.connect(); // 獲取所有響應頭字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍歷所有的響應頭字段 // for (String key : map.keySet()) { // System.out.println(key + "--->" + map.get(key)); // } // 定義 BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader( connection.getInputStream(),"utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送GET請求出現異常!" + e); e.printStackTrace(); } // 使用finally塊來關閉輸入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * 向指定 URL 發送POST方法的請求 * * @param url * 發送請求的 URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return 所代表遠程資源的響應結果 */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打開和URL之間的連接 URLConnection conn = realUrl.openConnection(); // 設置通用的請求屬性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 發送POST請求必須設置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection對象對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數 out.print(param); // flush輸出流的緩沖 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送 POST 請求出現異常!"+e); e.printStackTrace(); } //使用finally塊來關閉輸出流、輸入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } }
同時實現讀取文本中的百度經緯度坐標實現批量處理並生成gpx格式文檔(可上傳到openstreetmap形成軌跡)代碼如下:
package com.dataprocess; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CopyOfGenerateGpx { public void generate(){ String fileNames[]={"LocationData_2015-09-15", "LocationData_2015-09-16" }; int n = fileNames.length; for(String fileName: fileNames){ //輸入文件 File filein = new File("H:\\項目數據\\測試\\"+fileName+".txt"); //輸出文件 File fileout = new File("H:\\項目數據\\測試\\gpx\\"+fileName+".gpx"); //輸出json文件 File jsonfile = new File("H:\\項目數據\\測試\\json\\JSON_"+fileName+".json"); try { //輸入流 FileInputStream fis = new FileInputStream(filein); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); //輸出流 FileOutputStream fos = new FileOutputStream(fileout); OutputStreamWriter osw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(osw); //json輸出流 FileOutputStream fos1 = new FileOutputStream(jsonfile); OutputStreamWriter osw1 = new OutputStreamWriter(fos1); BufferedWriter bw1 = new BufferedWriter(osw1); bw1.write("[\r\n"); bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"); bw.write("<gpx version=\"1.1\" creator=\"OSMTrack\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/1\"" + " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\r\n"); bw.write("<trk>\r\n<trkseg>\r\n"); String line = null; String regEx ="[^0-9.\\+\\-\\s]"; Pattern p = Pattern.compile(regEx); while((line=br.readLine())!=null){ Matcher m = p.matcher(line); String[] arrs=m.replaceAll("").trim().split("\\s"); String lonbd = arrs[2]; String latbd = arrs[3]; String params = "coords=" + lonbd+","+latbd + "&from=1&to=5&ak=ZQU8ZGeBf95nN8wY1cNL13NP&output=json"; String s=sendGet("http://api.map.baidu.com/geoconv/v1/?", params); String[] s1 = s.split("\\["); String[] s2 = s1[1].split(","); Matcher m1 = p.matcher(s2[0]); Matcher m2 = p.matcher(s2[1]); String[] arrs1=m1.replaceAll("").trim().split("\\s"); String[] arrs2=m2.replaceAll("").trim().split("\\s"); System.out.println(s1[1]); System.out.println(arrs1[0]+" "+arrs2[0]); double lon = 2*Double.parseDouble(lonbd)-Double.parseDouble(arrs1[0]); double lat = 2*Double.parseDouble(latbd)-Double.parseDouble(arrs2[0]); String str = "<trkpt lat=\""+lat+"\" lon=\""+lon+"\"><ele>61.3</ele><time>2015-09-05T14:12:20Z</time></trkpt>"; bw.write(str+"\r\n"); String str1 = "{" +"\"longitude\": "+lon +",\"latitude\": "+lat +"},\r\n"; bw1.write(str1); System.out.println(str); } bw.write("</trkseg>\r\n</trk>\r\n</gpx>\r\n"); bw.flush(); bw1.write("]\r\n"); bw1.flush(); br.close(); fis.close(); bw.close(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + param; URL realUrl = new URL(urlNameString); // 打開和URL之間的連接 URLConnection connection = realUrl.openConnection(); // 設置通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立實際的連接 connection.connect(); // 獲取所有響應頭字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍歷所有的響應頭字段 // for (String key : map.keySet()) { // System.out.println(key + "--->" + map.get(key)); // } // 定義 BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader( connection.getInputStream(),"utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送GET請求出現異常!" + e); e.printStackTrace(); } // 使用finally塊來關閉輸入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } public static void main(String[] args){ CopyOfGenerateGpx g = new CopyOfGenerateGpx(); g.generate(); } }
