public static Map<String,String> getGeocoderLatitude(String address){
BufferedReader in = null;
try {
//将地址转换成utf-8的16进制
address = URLEncoder.encode(address, "UTF-8");
String key ="xxxxxxx";//这个key值要自己申请,具体自行百度
URL tirc = new URL("https://apis.map.qq.com/ws/geocoder/v1/?address="+ address +"&key="+ key);
HttpURLConnection conn = (HttpURLConnection) tirc.openConnection();
conn.setDoOutput(true);
// 腾讯地图使用GET
conn.setRequestMethod("GET");
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();
Map<String,String> map = null;
if(StringUtils.isNotEmpty(str)){
int lngStart = str.indexOf("lng\":");
int lngEnd = str.indexOf(",\"lat");
int latEnd = str.indexOf("},\"ad_info");
if(lngStart > 0 && lngEnd > 0 && latEnd > 0){
String lng = str.substring(lngStart+5, lngEnd);
String lat = str.substring(lngEnd+7, latEnd);
map = new HashMap<String,String>();
map.put("lng", lng);
map.put("lat", lat);
return map;
}
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}