Use Java to complete the Amap development platform to solve positioning and analysis problems

The three armies can seize the commander, but an ordinary man cannot seize the will.

 

1. Amap Development Platform: Platform address

2. Register an account on the Amap development platform

3. Import SDK

J version


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-key
        String 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;
    }
}

 Notice:

Guess you like

Origin blog.csdn.net/s_sos0/article/details/134748123