Java__GPS转百度
代码如下拷贝可以直接用
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.text.ParseException;
/**
- GPS转百度
- @author Administrator
*/
public class GpsChangeBaidu {
public static void main(String args[]) throws ParseException {
String lon = "112.98051";
String lat = "28.17649";
String aaa = changgeXY(lon, lat);
System.out.println(aaa);
String[] a = aaa.split(",");
if (a.length == 2) {
String lons = a[0];
String lats = a[1];
System.out.println(lons);
System.out.println(lats);
}
}
public static String[] changge(String lon, String lat) {
String[] lons = lon.split("\\.");
String[] lats = lat.split("\\.");
StringBuilder stringBuilder1 = new StringBuilder(lons[1]);
stringBuilder1.insert(2, ".");
String a = stringBuilder1.toString();
StringBuilder stringBuilder2 = new StringBuilder(lats[1]);
stringBuilder2.insert(2, ".");
String a1 = stringBuilder2.toString();
Double s1 = Double.parseDouble(lons[0]) + Double.parseDouble(a) / 60;
Double s2 = Double.parseDouble(lats[0]) + Double.parseDouble(a1) / 60;
return new String[] { String.valueOf(s1), String.valueOf(s2) };
}
public static String changge1(String lon) {
String[] lons = lon.split("\\.");
String a1 = lons[0];
String a2 = lons[1];
StringBuilder stringBuilder1 = new StringBuilder(a2);
stringBuilder1.insert(2, ".");
String a = stringBuilder1.toString();
Double s1 = Double.parseDouble(a1) + Double.parseDouble(a) / 60;
return String.valueOf(s1);
}
public static String changge2(String lat) {
String[] lats = lat.split("\\.");
String a1 = lats[0];
String a2 = lats[1];
StringBuilder stringBuilder2 = new StringBuilder(a2);
stringBuilder2.insert(2, ".");
String a = stringBuilder2.toString();
Double s2 = Double.parseDouble(a1) + Double.parseDouble(a) / 60;
return String.valueOf(s2);
}
public static String changgeXY(String xx, String yy) {
try {
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=" + xx + "&y=" + yy);
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[] point = json.split(",");
String x = point[1].split(":")[1].replace("\"", "");
String y = point[2].split(":")[1].replace("\"", "");
return (new String(decode(x)) + "," + new String(decode(y)));
} else {
System.out.println("gps坐标无效!!");
}
out.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解码
*
* @param str
* @return string
*/
public static String decode(String str) {
String bt = null;
Base64Decoder decoder = new Base64Decoder();
bt = decoder.decode(str);
// System.out.println(new String (bt));
return bt;
}
}