java后台调用高德地图根据地址获取经纬度

以下代码是调用高德地图根据地址获取经纬度,反之也是同样的道理

/** * 根据地址获取经纬度 *

public static Map getAreaLongAndDimen(String addr){

try {

addr = new String(addr.getBytes("UTF-8"),"GBK");}//因为高德地图用的是linux系统,所以其使用的是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;

}

你可能感兴趣的:(java后台调用高德地图根据地址获取经纬度)