三军可夺帅也,匹夫不可夺志也
1、高德开发平台:平台地址
2、注册高德开发平台账号
3、导入SDK
J版
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;public class main {private final static Logger logger = LoggerFactory.getLogger(main.class);public static void main(String[] args) {// lat 31.2990170 纬度//log 121.3466440 经度String add = main.getAdd("31.2990170", "121.3466440");logger.info(add);}/***根据经纬度获取省市区* @param log* @param lat* @return*/public static String getAdd(String log, String lat ){//lat 小 log 大//参数解释: 纬度,经度 采用高德API可参考高德文档https://lbs.amap.com///注意key是在高德开放平台申请的key,具体获得key的步骤请查看网址:https://developer.amap.com/api/webservice/guide/create-project/get-keyString key = "你申请的key值";String urlString = "https://restapi.amap.com/v3/geocode/regeo?location="+lat+","+log+"&extensions=base&batch=false&roadlevel=0&key="+key;String res = "";try {URL url = new URL(urlString);java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();conn.setDoOutput(true);conn.setRequestMethod("POST");java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));String line;while ((line = in.readLine()) != null) {res += line+"\n";}in.close();//解析结果JSONObject jsonObject = JSONObject.parseObject(res);logger.info(jsonObject.toJSONString());JSONObject jsonObject1 = jsonObject.getJSONObject("regeocode");res =jsonObject1.getString("formatted_address");} catch (Exception e) {logger.error("获取地址信息异常{}",e.getMessage());return null;}System.out.println("通过API获取到具体位置:"+res);return res;}
}
注意: