高德猎鹰轨迹查询相关接口

高德猎鹰轨迹官网:服务管理-API文档-开发指南-猎鹰轨迹服务 | 高德地图API

轨迹查询 httpclient的post 

// post方法请求  创建轨迹
private static void createTrace() {
        String key = "高德注册的key";
        String sid = "服务id"; // 服务id
        String tid = "轨迹id"; // 轨迹id
        String tName = "第一个轨迹"; // 轨迹id

        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建HttpPost对象
        HttpPost httpPost = new HttpPost(LieYingConstant.trace.ADD + 
                "?key=" + key + "&sid=" + sid + "&tid=" + tid + "&tname=" + tName);

        //设置请求头信息
        httpPost.setHeader("Content-Type", "application/json");

        //执行请求
        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            //获取响应结果
            HttpEntity responseEntity = response.getEntity();
            String responseString = EntityUtils.toString(responseEntity, "UTF-8");
            System.out.println(responseString);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

轨迹查询 httpclient的get,猎鹰轨迹纠偏查询有一个比较坑的地方,就是纠偏时,他默认只查询第一页的,如果要查询整个轨迹,后端需要拿到返回的点的数量,计算总的页数,再循环查询所有的点位,

 private static void tranceSearch(LieYingDTO lieYingDTO) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建HttpPost对象
//        HttpGet httpGet = new HttpGet(LieYingConstant.trace.LIST + "?key=" + key + "&sid=" + sid +
//                "&tid=" + tid + "&trid=" + trid + "&correction=denoise=1,mapmatch=1,attribute=1,threshold=100,mode=driving&recoup=1");
        String requestUrl = LieYingConstant.trace.LIST + "?key=" + lieYingDTO.getKey() + "&sid=" + lieYingDTO.getSid() +
                "&tid=" + lieYingDTO.getTid() + "&trid=" + lieYingDTO.getTrid()
                + "&pagesize=999" ;
        HttpGet httpGet = new HttpGet(requestUrl);
        System.out.println("url---->" + requestUrl);
        //设置请求头信息
        httpGet.setHeader("Content-Type", "application/json");

        //执行请求
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            //获取响应结果
            HttpEntity responseEntity = response.getEntity();
            String responseString = EntityUtils.toString(responseEntity, "UTF-8");
            JSONObject parse = (JSONObject)JSONObject.parse(responseString);
            JSONObject data = (JSONObject)parse.get("data");
            JSONArray tracks = (JSONArray) data.get("tracks");
            JSONObject obj = (JSONObject)tracks.get(0);
            JSONArray jsonArray = (JSONArray)obj.get("points");
            List list = new ArrayList();
            for (Object o : jsonArray) {
                JSONObject object = (JSONObject) o;
                String location = object.get("location").toString();
                String[] split = location.split(",");
                list.add(split);
            }
            System.out.println(responseString);
            System.out.println(JSON.toJSON(list));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

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