protected void runTest() throws Throwable { try {
BaiduLocation bl = new BaiduLocation();
bl.gpsx = ;//经度
bl.gpsy = ;//纬度
GetBaiduLocation(bl);
if(bl.ok) {
int baidux = (int)(bl.baidux*1E6);
int baiduy = (int)(bl.baiduy*1E6);
// 转换成功,这个坐标是百度专用的
}
else {
/// 转换失败
}
}
catch(Exception ex) {
}
} class BaiduLocation {
public float gpsx, gpsy;
public float baidux, baiduy;
public boolean ok = false;
} public static String GetBaiduLocation(float x, float y) throws MalformedURLException, IOException {
String url = String.format("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=%f&y=%f", x, y);
HttpURLConnection urlConnection = (HttpURLConnection)(new URL(url).openConnection());
urlConnection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String lines = reader.readLine();
reader.close();
urlConnection.disconnect();
return lines;
} public static boolean GetBaiduLocation(BaiduLocation bl) {
try {
bl.ok = false;
String res = GetBaiduLocation(bl.gpsx, bl.gpsy);
if(res.startsWith("{") && res.endsWith("}")) {
res = res.substring(, res.length() - ).replace("\"", "");
String[] lines = res.split(",");
for(String line : lines) {
String[] items = line.split(":");
if(items.length == ) {
if("error".equals(items[])) {
bl.ok = "".equals(items[]);
}
if("x".equals(items[])) {
bl.baidux = ConvertBase64(items[]);
}
if("y".equals(items[])) {
bl.baiduy = ConvertBase64(items[]);
}
}
}
}
} catch (Exception e) {
bl.ok = false;
}
return bl.ok;
}
private static float ConvertBase64(String str) {
byte[] bs = Base64.decode(str);
return Float.valueOf(new String(bs));
}
经纬度转换接口http://map.yanue.net/gps.html
牛人主页:http://map.yanue.net/