`
a575292877
  • 浏览: 9829 次
  • 性别: Icon_minigender_2
  • 来自: 哈尔滨
最近访客 更多访客>>
社区版块
存档分类
最新评论

百度地图Geocoding API坐标反转

阅读更多
[size=medium][size=x-small]package com.cjjsys.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.springframework.core.io.InputStreamResource;

import com.cjjsys.common.PropertiesUtils;
public class BaiduUtil {
public static final String KEY_1 = PropertiesUtils.getInstance().getValue("ak");   ;

// String key2 = "8ec6cd98bd56554407a207d597c0f3e5";
// String key3 = "be025dc280e1e3f7ffb95fe42a01fab2";
// String key4 = "327db7009617d6806b9c38e819ea06ac";

/**
* 返回输入地址的经纬度坐标
* key lng(经度),lat(纬度)
*/
public  String getGeocoderLatitude(String lat,String lng){
System.out.println(lat+",lng:"+lng);
BufferedReader in = null;
try {
//将地址转换成utf-8的16进制
//address = URLEncoder.encode(address, "UTF-8");
//       如果有代理,要设置代理,没代理可注释
// System.setProperty("http.proxyHost","192.168.1.188");
// System.setProperty("http.proxyPort","3128");
//http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=json&pois=1
String uri="http://api.map.baidu.com/geocoder/v2/?ak="+KEY_1+"&callback=renderReverse&" +
"location="+lat+","+lng+"&output=json&pois=0";
//"http://api.map.baidu.com/geocoder?callback=renderReverse&location="+ address +"&output=json&pois=0&key="+ KEY_1
URL tirc = new URL(uri);
HttpURLConnection conn = (HttpURLConnection)tirc.openConnection();
conn.setRequestProperty("Content-Type", "text/html;charset=utf-8");
conn.setDoOutput(true);
//尝试链接5次,如果正确的话就跳出循环没如果连接超时就重新连接
for(int i = 0; i < 5; i ++){
conn.connect();
if(conn.getResponseCode() == 200)
break;
else if(conn.getResponseCode() == 408)
Thread.sleep(5);
};
System.out.println(conn.getResponseCode());
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String res;
StringBuilder sb = new StringBuilder("");
while((res = in.readLine())!=null){
sb.append(res.trim());

}
String str = sb.toString();
System.out.println(str);
Map<String,String> map = null;
if(str!=null&&str.split("formatted_address\"\\:\"")[1]!=null&& str.split("formatted_address\"\\:\"")[1].split("\"\\,")[0]!=null){
System.out.println(str.split("formatted_address\"\\:\"")[1].split("\"\\,")[0]);
return str.split("formatted_address\"\\:\"")[1].split("\"\\,")[0];
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(null!=in){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void main(String args[]){
BaiduUtil baiduUtil= new BaiduUtil();
//43.90637886442,lng:125.30130752475
String str = baiduUtil.getGeocoderLatitude("43.90637886442","125.30130752475");

}
}




[/size][/size]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics