高德地图 经纬度查询位置

需求:通过一组经纬度查询地址

高德api:入门指南-Web服务 API | 高德地图API (amap.com)

先申请高德key

接口调用:keyValue替换自己的keyValue

batch:是否批量查询,默认false不使用

https://restapi.amap.com/v3/geocode/regeo?key=keyValue&location=112.286928,31.548141&batch=true&extensions =all

代码调用:

   public String getAddress(double latitude, double longitude) {
        String address = "";
        CloseableHttpResponse response = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        URIBuilder uriBuilder = null;
        String location = longitude + "," + latitude;
        //调用高德地图请求拿到数据
        try {
            uriBuilder = new URIBuilder("https://restapi.amap.com/v3/geocode/regeo");
            uriBuilder.setParameter("key", mapConfiguation.getGaodeKey())
                    .setParameter("location", location);
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = response.getEntity();
                String content = EntityUtils.toString(httpEntity, "utf8");
                //解析数据
                address = GaodeLocationByLongitudeAndlatitude(content.toString());
//                System.out.println("位置信息=================="+address);
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return address;
    }

    /**
     * 输入经纬度后返回位置信息
     *
     * @param jsonData
     * @return 返回的类gaodelocation,详见类
     */
    public String GaodeLocationByLongitudeAndlatitude(String jsonData) {
        String formattedAddress = null;
        String info = null;
        String status = null;
        String address = null;
        try {
            JSONObject jsonObject = JSONObject.fromObject(jsonData);
            // 解析json
            status = jsonObject.getString("status");
            info = jsonObject.getString("info");
            JSONObject dataObj = jsonObject.getJSONObject("regeocode");
            formattedAddress = dataObj.getString("formatted_address");
            if (status.equals("1")) {
                address = formattedAddress;
            } else {
                address = info;
            }
        } catch (Exception e) {
            e.printStackTrace();
            address = e.getMessage();
        }


        return address;
    }

由于多条数据一条条查询开始使用ls.stream()查询,销量较低

后来改为parallelStream()效率大大提高

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心系代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值