百度地图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();
}
}

通过百度地图API将百度坐标转换成GPS经纬度的更多相关文章

  1. ***微信LBS地理位置开发+百度地图API(地理位置和坐标转换)

    微信公众平台开发 - 获取用户地理位置 本文介绍在微信公众平台上如何使用高级接口开发获取用户地理位置的功能. 一.获取用户地理位置接口 开通了上报地理位置接口的公众号,用户在关注后进入公众号会话时,会 ...

  2. 【百度地图API】百度API卫星图使用方法和卫星图对比工具

    原文:[百度地图API]百度API卫星图使用方法和卫星图对比工具 百度地图API推出卫星图接口也有一个月啦~ 本文除了介绍如何使用百度地图API来操作卫星图外,还顺带制作了个卫星图对比工具. 一.百度 ...

  3. 用户Ip地址和百度地图api接口获取用户地理位置(经纬度坐标,城市)

    <?php   //获取用户ip(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获取127.0.0.1) function getip(){     if(!empty($_SERVE ...

  4. php用百度地图API进行IP定位和GPS定位

    <?php /** * 根据地理坐标获取国家.省份.城市,及周边数据类(利用百度Geocoding API实现) * 百度密钥获取方法:http://lbsyun.baidu.com/apico ...

  5. 百度地图API地理位置和坐标转换

    1.由地名(省份.城市.街道等)得到其对应的百度地图坐标: http://api.map.baidu.com/geocoder/v2/?output=json&ak=你从百度申请到的Key&a ...

  6. 【Android接百度地图API】百度地图Demo点击按钮闪退

    运行百度地图自带的BaiduMap_AndroidSDK_v4.1.0_Sample里面的BaiduMapsApiASDemo发现点击上面的按钮会闪退,控制台报的是xml的问题 查了一下,官方文档特别 ...

  7. 百度地图API开发指南

    简介什么是百度地图API? 百度地图API是一套由JavaScript语言编写的应用程序接口,它能够帮助您在网站中构建功能丰富.交互性强的地图应用.百度地图API包含了构建地图基本功能的各种接口,提供 ...

  8. 百度地图API调用实例之地址标注与位置显示

    之前弄了个谷歌地图API标注的调用实例,后来要求改成百度地图. 感谢主,通过网上资料(百度地图API,百度地图API详解之地图标注)收集及研究, 终于把百度地图标注和显示功能实现出来了,具体实现方法如 ...

  9. 百度地图API和高德地图API资料集锦

    [高德地图API]从零开始学高德JS API(五)路线规划——驾车|公交|步行   [高德地图API]从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自 ...

随机推荐

  1. 【一段日子荟萃】where should I go.

    当<UNIX环境高级编程>和<鸟哥的私房菜>到我的桌头的时候,我忽然产生了厌倦的心. NO,我不是想做这个,我不是想学习这个操作系统的结构和接口. 我想些一个操作系统,更一般的 ...

  2. linux命令 screen的简单使用

    在远程命令行下某些长时间的操作,一旦网络出现故障,后果可能会很严重,在这种情况下可以使用screen命令来解决.screen可以创建一个session,在不小心断开以后还可以继续恢复session保存 ...

  3. python添加windows域验证

    1.安装python-ldap https://pypi.python.org/pypi/python-ldap/ 在 Ubuntu/Debian 下安装 python-ldap 模块: $ sudo ...

  4. html5 canvas防微博旋转

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. HDU2033 人见人爱A+B 分类: ACM 2015-06-21 23:05 13人阅读 评论(0) 收藏

    人见人爱A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  6. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...

  7. C++11空指针

    [C++11空指针] 早在 1972 年,C语言诞生的初期,常数 0 带有常数及空指针的双重身分. C 使用 preprocessor macro NULL 表示空指针, 让 NULL 及 0 分别代 ...

  8. 【全面完美方案】iPhone 4S WiFi变灰 DIY修复方式

    这是我在一位台湾网友usaretama发表的一篇帖子中看到的,原帖我发表在维维网 如果你有WiFi开关变灰不能切换.WiFi遇到搜不到AP或搜到了连不上,那您就要注意这篇了. 家人的 iPhone 4 ...

  9. flask中的session对象方法

    'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys ...

  10. ASP.NET MVC- UrlHelper的用法

    UrlHelper提供了四个非常常用的四个方法 1.Action方法通过提供Controller,Action和各种参数生成一个URL, 2.Content方法是将一个虚拟的,相对的路径转换到应用程序 ...