• java的调用百度接口工具方法(两个之间的距离)


    涉及到 经纬度的查询 和路线规划 以下使用的@Slf4j注解

        private void checkOutOfRange(String address){
            Map map=new HashMap();
            map.put("address",shopAddress);
            map.put("output","json");
            map.put("ak",ak);
            //获取店铺的经纬度坐标
            String shopCoordinate = HttpClientUtil.doGet("https://api.map.baidu.com/geocoding/v3", map);
    
            JSONObject jsonObject = JSON.parseObject(shopCoordinate);
            if(!jsonObject.getString("status").equals("0")){//0代表着地址解析成功
                throw new OrderBusinessException("店铺地址解析失败");
            }
    //        { 返回的结果集举例
    //            "status": 0,
    //                "result": {
    //            "location": {
    //                "lng": 113.923201,
    //                "lat": 23.105796
    //            }
    //        }
    //        }
            //查看经纬度
            JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
    
            String lat = location.getString("lat");
            String lng = location.getString("lng");
    
            //获取店铺的经纬度
            String shopLngLat=lat+","+lng;
            log.info("获取店铺的经纬度{}",shopLngLat);
    
            map.put("address",address);
            //获取用户收货地址的经纬度坐标
            String userCoordinate = HttpClientUtil.doGet("https://api.map.baidu.com/geocoding/v3", map);
    
            jsonObject = JSON.parseObject(userCoordinate);
            if(!jsonObject.getString("status").equals("0")){//0代表着地址解析成功
                throw new OrderBusinessException("收获地址解析失败");
            }
            //数据解析
            location = jsonObject.getJSONObject("result").getJSONObject("location");
            lat=location.getString("lat");
            lng=location.getString("lng");
            //用户收货地址经纬度坐标
            String userLngLat=lat+","+lng;
    
            map.put("origin",shopLngLat);
            map.put("destination",userLngLat);
            map.put("steps_info","0");
    
            String json=HttpClientUtil.doGet("https://api.map.baidu.com/directionlite/v1/driving",map);
    
            jsonObject = JSON.parseObject(json);
            if(!jsonObject.getString("status").equals("0")){
                throw new OrderBusinessException("配送路线规划失败");
            }
            /*
            {
      "status": 0,
      "result": {
        "routes": [
          {
            "distance": "1449",
            "duration": "245",
            "real_time_traffic": {
              "road_id": "11607050001",
              "speed": "50",
              "delay": "0",
              "jing_jie": "100",
              "traffic_state": "normal"
            },
           .....
           */
            //数据解析
            JSONObject result = jsonObject.getJSONObject("result");
            JSONArray jsonArray = (JSONArray) result.get("routes");
            Integer distance = (Integer) ((JSONObject) jsonArray.get(0)).get("distance");//获取举例米
            log.info("距离为:{}",distance);
            if (distance>5000){
                //配送距离超过5000米
                throw new OrderBusinessException("超出配送范围");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    请求第三方的客户端工具类

    public class HttpClientUtil {
    
        static final  int TIMEOUT_MSEC = 5 * 1000;
    
        /**
         * 发送GET方式请求
         * @param url
         * @param paramMap
         * @return
         */
        public static String doGet(String url,Map<String,String> paramMap){
            // 创建Httpclient对象
            CloseableHttpClient httpClient = HttpClients.createDefault();
    
            String result = "";
            CloseableHttpResponse response = null;
    
            try{
                URIBuilder builder = new URIBuilder(url);
                if(paramMap != null){
                    for (String key : paramMap.keySet()) {
                        builder.addParameter(key,paramMap.get(key));
                    }
                }
                URI uri = builder.build();
    
                //创建GET请求
                HttpGet httpGet = new HttpGet(uri);
    
                //发送请求
                response = httpClient.execute(httpGet);
    
                //判断响应状态
                if(response.getStatusLine().getStatusCode() == 200){
                    result = EntityUtils.toString(response.getEntity(),"UTF-8");
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    response.close();
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            return result;
        }
    
        /**
         * 发送POST方式请求
         * @param url
         * @param paramMap
         * @return
         * @throws IOException
         */
        public static String doPost(String url, Map<String, String> paramMap) throws IOException {
            // 创建Httpclient对象
            CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = null;
            String resultString = "";
    
            try {
                // 创建Http Post请求
                HttpPost httpPost = new HttpPost(url);
    
                // 创建参数列表
                if (paramMap != null) {
                    List<NameValuePair> paramList = new ArrayList();
                    for (Map.Entry<String, String> param : paramMap.entrySet()) {
                        paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
                    }
                    // 模拟表单
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
                    httpPost.setEntity(entity);
                }
    
                httpPost.setConfig(builderRequestConfig());
    
                // 执行http请求
                response = httpClient.execute(httpPost);
    
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            } catch (Exception e) {
                throw e;
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            return resultString;
        }
    
        /**
         * 发送POST方式请求
         * @param url
         * @param paramMap
         * @return
         * @throws IOException
         */
        public static String doPost4Json(String url, Map<String, String> paramMap) throws IOException {
            // 创建Httpclient对象
            CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = null;
            String resultString = "";
    
            try {
                // 创建Http Post请求
                HttpPost httpPost = new HttpPost(url);
    
                if (paramMap != null) {
                    //构造json格式数据
                    JSONObject jsonObject = new JSONObject();
                    for (Map.Entry<String, String> param : paramMap.entrySet()) {
                        jsonObject.put(param.getKey(),param.getValue());
                    }
                    StringEntity entity = new StringEntity(jsonObject.toString(),"utf-8");
                    //设置请求编码
                    entity.setContentEncoding("utf-8");
                    //设置数据类型
                    entity.setContentType("application/json");
                    httpPost.setEntity(entity);
                }
    
                httpPost.setConfig(builderRequestConfig());
    
                // 执行http请求
                response = httpClient.execute(httpPost);
    
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            } catch (Exception e) {
                throw e;
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            return resultString;
        }
        //设置连接超时时间
        private static RequestConfig builderRequestConfig() {
            return RequestConfig.custom()
                    .setConnectTimeout(TIMEOUT_MSEC)
                    .setConnectionRequestTimeout(TIMEOUT_MSEC)
                    .setSocketTimeout(TIMEOUT_MSEC).build();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155

    打个标记.

  • 相关阅读:
    【C++】栈和队列的模拟实现 & 经典题目解析
    android系统硬件输入和软键盘输入属性
    【pytorch】内容链接汇总
    【C++初阶(三)引用与内联函数】
    python标准模块----logging
    初次远程面试阿里团队,4面以为没结果了,真没想到收到录取通知
    进程和线程的主要区别
    Xline 源码解读(四)—— CURP 状态机引擎
    什么是软件EV代码签名证书
    【小程序自动化Minium】一、框架介绍和环境搭建
  • 原文地址:https://blog.csdn.net/Takeit_easy/article/details/133244072