其他分享
首页 > 其他分享> > 百度地图api获取坐标计算距离

百度地图api获取坐标计算距离

作者:互联网

调用百度地图API获取经纬坐标

    /**
     * 获取地址经纬度
     */
    @Test
    public void getLatAndLon() {
        String url = "http://api.map.baidu.com/geocoding/v3/";
        Map<String, String> params = new HashMap<>();
        params.put("ak", "your ak");
        params.put("address", "浙江省杭州市下城区石桥街道洄龙湖邸10号楼"); // 地址
        params.put("callback", "showLocation");
        params.put("output", "json");

        //创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            // 创建URI对象,并且设置请求参数
            URI uri = new URIBuilder(url).setParameter("ak", params.get("ak"))
                    .setParameter("address", params.get("address"))
                    .setParameter("callback", params.get("callback"))
                    .setParameter("output", params.get("output"))
                    //.setParameter("output", params.get("output"))
                    .build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 解析响应数据
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

根据两个经纬坐标计算距离

    /**
     * 百度地图两个经纬度计算距离时间
     * {"status":0,"result":[{"distance":{"text":"289.6公里","value":289578},"duration":{"text":"3.6小时","value":12828}}],"message":"成功"}
     */
    @Test
    public void mapGet() {
        String url = "http://api.map.baidu.com/routematrix/v2/driving"; // 算路
        String url1 = "http://api.map.baidu.com/direction/v2/driving"; // 路线规划
        Map<String, String> params = new HashMap<>();
        params.put("ak", "your ak");
        params.put("origins", "30.350216,120.206120"); // 起始位置(维度,经度)
        params.put("destinations", "31.953043,118.544131"); // 目的地(维度,经度)
        params.put("tactics", "11");
        //params.put("output", "json");

        //创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            // 创建URI对象,并且设置请求参数
            URI uri = new URIBuilder(url).setParameter("ak", params.get("ak"))
                    .setParameter("origins", params.get("origins"))
                    .setParameter("destinations", params.get("destinations"))
                    .setParameter("tactics", params.get("tactics"))
                    //.setParameter("output", params.get("output"))
                    .build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 解析响应数据
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

使用百度地图api前先创建应用,就能获取ak码
百度地图api文档:https://lbsyun.baidu.com/index.php?title=webapi

标签:get,ak,api,params,坐标,put,setParameter,response,百度
来源: https://www.cnblogs.com/mxh-java/p/15023531.html