实现 将 原始的经纬坐标变成百度经纬坐标。其实中里面就是调用了百度的api:<pre name="code" class="java">"http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x="+/*经度*/xgps+"&y="+/*纬度*/ygps;
</pre><pre name="code" class="java">/**
* from: 来源坐标系 (0表示原始GPS坐标,2表示Google坐标)
to: 转换后的坐标 (4就是百度自己啦,好像这个必须是4才行)
x: 经度
y: 纬度
*/
就这么简单
package gps2bgps;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import sun.misc.BASE64Decoder;
public class ToBGps {
/**
* 接受两个参数,xgps用来接受原始经度,ygps用来接受原始纬度
* @param xgps
* @param ygps
*/
public static void getBGps(String xgps,String ygps){
String baiduUrl = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x="+/*经度*/xgps+"&y="+/*纬度*/ygps;
//目前两个URL来或去百度的坐标。但不知道哪一个是正确的
System.out.println(baiduUrl);
/**
* from: 来源坐标系 (0表示原始GPS坐标,2表示Google坐标)
to: 转换后的坐标 (4就是百度自己啦,好像这个必须是4才行)
x: 经度
y: 纬度
*/
//"http://api.map.baidu.com/ag/coord/convert?x="+xgps+"&y="+ygps+"&from=0&to=2&mode=1";
//http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=113.01930956&y=22.8370871
Map<String,String> map = null;
try {
java.net.URI uri = new URI(baiduUrl);
URL url = uri.toURL();
InputStream in = url.openStream();
byte[] b = new byte[in.available()];
in.read(b);
String result = new String(b);
System.out.println("------->"+result);
//"error":0,"x":"MTEzLjAzMTgzMzQ5NDU5","y":"MjIuODQ5NTQ4NDI3OTQ1"
String status = result.substring(result.indexOf("\"error\":")+8,result.indexOf("\"error\":")+9);
String xBaseStr = result.substring(result.indexOf("\"x\":\"")+5,result.indexOf(",\"y\":\"")-1);
String yBaseStr = result.substring(result.indexOf(",\"y\":\"")+6,result.indexOf("\"}"));
System.out.println("x-->"+xBaseStr);
System.out.println("y-->"+yBaseStr);
System.out.println(status);
if("0".equals(status)){
map = DecoredBase64(xBaseStr,yBaseStr);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(map);
}
public static Map<String,String> DecoredBase64(String x,String y){
byte[] xb = null;
byte[] yb = null;
try {
xb = new BASE64Decoder().decodeBuffer(x);
yb = new BASE64Decoder().decodeBuffer(y);
} catch (IOException e) {
System.err.println("转码出错");
}
Map<String,String> bgps = new HashMap<String,String>();
bgps.put("xbgps", new String(xb));
bgps.put("ybgps", new String(yb));
return bgps;
}
@org.junit.Test
public void test() throws IOException{
//Map m = ToBGps.DecoredBase64("MTEzLjAzMTEwNDAzMzEx", "MjIuODQwNTg1NDc4MjYz");
//System.out.print(m);
getBGps("113.02004184","22.84603575");
}
}