public static String getGeocoderLatitude(String address){
BufferedReader in = null;
String lng ="";
String lat ="";
try {
address = URLEncoder.encode(address, "UTF-8");
URL tirc = new URL("http://api.map.baidu.com/geocoder?address="+ address +"&output=json&key="+"7d9fbeb43e975cd1e9477a7e5d5e192a");
in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));
String res;
StringBuilder sb = new StringBuilder("");
while((res = in.readLine())!=null){
sb.append(res.trim());
}
String str = sb.toString();
if(StringUtils.isNotEmpty(str)){
int lngStart = str.indexOf("lng\":");
int lngEnd = str.indexOf(",\"lat");
int latEnd = str.indexOf("},\"precise");
if(lngStart > 0 && lngEnd > 0 && latEnd > 0){
lng = str.substring(lngStart+5, lngEnd);
lat = str.substring(lngEnd+7, latEnd);
System.out.println("=="+lng);
System.out.println("++"+lat);
}
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return lng+","+lat;
}
根据名称将其转换为经纬度坐标
最新推荐文章于 2024-10-03 07:47:13 发布