zoukankan      html  css  js  c++  java
  • 百度地图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

  • 相关阅读:
    DataItem,gridview,repeater数据控件数据绑定
    repeater没有数据显示暂无数据,无记录
    Javascript操作Cookie的脚本 — CookieHelper
    <script>标签应该放到</body>标签之前
    IEnumerable 和 IQueryable 区别
    【three.js练习程序】鼠标滚轮缩放
    【three.js练习程序】动画效果,100个方块随机运动
    【three.js练习程序】随机生成100个方块
    树莓派练习程序(寻迹模块)
    树莓派练习程序(声音检测)
  • 原文地址:https://www.cnblogs.com/mxh-java/p/15023531.html
Copyright © 2011-2022 走看看