JAVA根据经纬度获取地址

app端uni-app可直接通过自带方法获取经纬度并获取地址.部署到小程序,则只能获取经纬度 如图:
JAVA根据经纬度获取地址_第1张图片
address参数仅在app端支持.
如果需要的地址参数 在小程序端,我解决的方法是前端把经纬度传来,通过java工具类把经纬度转换成地址参数

上代码

public class GetLocation {
    private static final String key = "你申请的key";//key值我用的是高德的key
    public static void main(String[] args) {
        String add = getAdd("116.3039", "39.97646");
        JSONObject jsonObject = JSONObject.fromObject(add);
        JSONObject json = jsonObject.getJSONObject("regeocode");
        String address = json.getString("formatted_address");
        String infocode = jsonObject.getString("infocode");
        System.out.println(address);
        System.out.println(add);
    }

    public static String getAdd(String lng, String lat)
    {
        String urlString = "http://restapi.amap.com/v3/geocode/regeo?key="+key+"&location="+lng+ "," +lat;
        String res = "";
        BufferedReader in = null;
        try
        {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = null;
            while ((line = in.readLine()) != null)
            {
                res += line + "\n";
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                in.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;

    }

参考文档: 高德官方文档:地理/逆地理编码

在刚测试的时候报了一个USERKEY_PLAT_NOMATCH 10009的错
解决方案:https://blog.csdn.net/edewjeapk/article/details/89923066.
我用的是之前申请的key 好像不是WEB端的key
在这里插入图片描述
换成web服务key就行了
得到结果:
在这里插入图片描述
解析一下json获取自己想要的数据(省市区街道)即可

放一下链接链接: 高德地图错误信息表—https://lbs.amap.com/api/wx/reference/errorcode/.
高德开放平台地址-https://lbs.amap.com/api/webservice/summary.

你可能感兴趣的:(Java,java)