以下代碼是調(diào)用高德地圖根據(jù)地址獲取經(jīng)緯度缝左,反之也是同樣的道理
/** * 根據(jù)地址獲取經(jīng)緯度 *
public static Map getAreaLongAndDimen(String addr){
try {
addr = new String(addr.getBytes("UTF-8"),"GBK");}//因為高德地圖用的是linux系統(tǒng),所以其使用的是gbk的編碼,所以在這里你用的是utf-8的話欲间,就得轉換成gbk的編碼格式代乃。
catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String str = "http://restapi.amap.com/v3/geocode/geo?address="+addr+"&output=JSON&key=key值";HashMapparam = new HashMap();
param.put("info", "erro");
InputStream inputStream = null;
try {
URL url = new URL(str);
HttpURLConnection urlConnection =(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5*1000);//超時時間
urlConnection.setRequestProperty("contentType", "utf-8");//字符集
urlConnection.connect();
inputStream = urlConnection.getInputStream();
JsonNode jsonNode = new ObjectMapper().readTree(inputStream);//jackson
? ? if(StringUtil.equals(jsonNode.findValue("status").textValue(),"1") && jsonNode.findValue("geocodes").size()>0){
? ? String[] degree = jsonNode.findValue("geocodes").findValue("location").textValue().split(",");
? ? param.put("longitude", degree[0]);
? ? param.put("dimension", degree[1]);
? ? param.put("info", "success");
? ? }
} catch (MalformedURLException e) {
param.put("info", "erro");
} catch (IOException e) {
param.put("info", "erro");
}finally{
try {
if(null != inputStream ){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return param;
}