高德地图Api的使用

1、根据ip定位用户信息

 public static HttpEntity doGet(String url) {
        //创建一个Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //创建一个get请求
        HttpGet httpGet = new HttpGet(url);
        //响应模型
        CloseableHttpResponse response = null;
        try {
            //由客户端发送get请求
            response = httpClient.execute(httpGet);
            //从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
//                return (List) responseEntity;
                return responseEntity;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    //根据ip希望能够将IP信息转换为地理位置信息
    public String getLocationByIp(String ip) throws IOException {
        String s = "https://restapi.amap.com/v3/ip?ip="+ip+"&output=json&key=你的key值";
        HttpEntity httpEntity = doGet(s);
        return EntityUtils.toString(httpEntity);
    }
    @Test
    public void test12() throws IOException {
        String locationByIp = getLocationByIp("114.247.50.2");
        System.out.println(locationByIp);
    }

2、根据起始点和目的点进行步行路线规划

public static String doGet(String url) {
        //创建一个Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //创建一个get请求
        HttpGet httpGet = new HttpGet(url);
        //响应模型
        CloseableHttpResponse response = null;
        try {
            //由客户端发送get请求
            response = httpClient.execute(httpGet);
            //从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    @Test
    public void test1() throws IOException {
        String distanceByWalk = getDistanceByWalk("成都市xxx花园二期", "成都市xxx地铁站");
        System.out.println(distanceByWalk);
    }

    //步行路径规划
    public String getDistanceByWalk(String origin, String destination) throws IOException {
        //将出发点位置转换成经纬度
        String originLocation = getLocationByAddress(origin);
        //将目的地位置转换成经纬度
        String destinationLocation = getLocationByAddress(destination);
        String s = "https://restapi.amap.com/v3/direction/walking?origin="+originLocation+"&destination="+destinationLocation+"&key=你的key";
        String s1 = doGet(s);
       return s1;
    }

    //根据地理位置拿到经纬度
    public static String getLocationByAddress(String adress) throws IOException {
        String s = "https://restapi.amap.com/v3/geocode/geo?key=你的key&address=" + adress;
        String s1 = doGet(s);
        String location = "";
        JSONObject jsonObject = JSON.parseObject(s1);
        List geocodes = (List) jsonObject.get("geocodes");
        if (geocodes.size() != 0 && geocodes != null) {
            Map map = geocodes.get(0);
            location = String.valueOf(map.get("location"));
        }
        return location;
    }

3、根据起始点和目的点进行公交路线规划

public static String doGet(String url) {
        //创建一个Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //创建一个get请求
        HttpGet httpGet = new HttpGet(url);
        //响应模型
        CloseableHttpResponse response = null;
        try {
            //由客户端发送get请求
            response = httpClient.execute(httpGet);
            //从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    //公交路径规划
    public String getDistancByTransit(String origin, String destination,String city) throws IOException {
        //将出发点位置转换成经纬度
        String originLocation = getLocationByAddress(origin);
        //将目的地位置转换成经纬度
        String destinationLocation = getLocationByAddress(destination);
        String s = "https://restapi.amap.com/v3/direction/transit/integrated?origin="+originLocation+"&destination="+destinationLocation+"&city="+city+"&output=json&key=你的key";
        String s1 = doGet(s);
        return s1;
    }

    @Test
    public void test2() throws IOException {
        String distancByTransit = getDistancByTransit("成都市龙泉驿区银河花园二期", "成都市龙泉驿区地铁站", "成都市");
        System.out.println(distancByTransit);
    }


    //根据地理位置拿到经纬度
    public static String getLocationByAddress(String adress) throws IOException {
        String s = "https://restapi.amap.com/v3/geocode/geo?key=你的key
&address=" + adress;
        String s1 = doGet(s);
        String location = "";
        JSONObject jsonObject = JSON.parseObject(s1);
        List geocodes = (List) jsonObject.get("geocodes");
        if (geocodes.size() != 0 && geocodes != null) {
            Map map = geocodes.get(0);
            location = String.valueOf(map.get("location"));
        }
        return location;
    }

4、驾车路径规划

private static final String KEY_ = "你的key";

    public static String doGet(String url) {
        //创建一个Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //创建一个get请求
        HttpGet httpGet = new HttpGet(url);
        //响应模型
        CloseableHttpResponse response = null;
        try {
            //由客户端发送get请求
            response = httpClient.execute(httpGet);
            //从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    //驾车路径规划
    public String getDistancByCar(String origin, String destination) throws IOException {
        //将出发点位置转换成经纬度
        String originLocation = getLocationByAddress(origin);
        //将目的地位置转换成经纬度
        String destinationLocation = getLocationByAddress(destination);
        String s = "https://restapi.amap.com/v3/direction/driving?origin="+originLocation+"&destination="+destinationLocation+"&extensions=all&output=json&key="+KEY_;
        String s1 = doGet(s);
        return s1;
    }

    @Test
    public void test3() throws IOException {
        String distancByCar = getDistancByCar("xxxx花园二期", "xxxx");
        System.out.println(distancByCar);
    }

    //根据地理位置拿到经纬度
    public static String getLocationByAddress(String adress) throws IOException {
        String s = "https://restapi.amap.com/v3/geocode/geo?key=1aaa6aaa97b964b57fcc13176b307c5c&address=" + adress;
        String s1 = doGet(s);
        String location = "";
        JSONObject jsonObject = JSON.parseObject(s1);
        List geocodes = (List) jsonObject.get("geocodes");
        if (geocodes.size() != 0 && geocodes != null) {
            Map map = geocodes.get(0);
            location = String.valueOf(map.get("location"));
        }
        return location;
    }

5、骑行路径规划

private static final String KEY_ = "你的key";

    public static String doGet(String url) {
        //创建一个Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //创建一个get请求
        HttpGet httpGet = new HttpGet(url);
        //响应模型
        CloseableHttpResponse response = null;
        try {
            //由客户端发送get请求
            response = httpClient.execute(httpGet);
            //从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    //骑行路径规划
    public String getDistanceByBicycle(String origin, String destination) throws IOException {
        //将出发点位置转换成经纬度
        String originLocation = getLocationByAddress(origin);
        //将目的地位置转换成经纬度
        String destinationLocation = getLocationByAddress(destination);
        String s = "https://restapi.amap.com/v4/direction/bicycling?origin="+originLocation+"&destination="+destinationLocation+"&extensions=all&output=json&key="+KEY_;
        String s1 = doGet(s);
        return s1;
    }

    @Test
    public void test4() throws IOException {
        String distanceByBicycle = getDistanceByBicycle("成都市龙泉驿区银河花园二期", "都江堰青城山成都东软学院");
        System.out.println(distanceByBicycle);
    }

    //根据地理位置拿到经纬度
    public static String getLocationByAddress(String adress) throws IOException {
        String s = "https://restapi.amap.com/v3/geocode/geo?key=1aaa6aaa97b964b57fcc13176b307c5c&address=" + adress;
        String s1 = doGet(s);
        String location = "";
        JSONObject jsonObject = JSON.parseObject(s1);
        List geocodes = (List) jsonObject.get("geocodes");
        if (geocodes.size() != 0 && geocodes != null) {
            Map map = geocodes.get(0);
            location = String.valueOf(map.get("location"));
        }
        return location;
    }

 

其他路线规划参照高德路线规划api

路径规划-API文档-开发指南-Web服务 API|高德地图API (amap.com)

你可能感兴趣的:(JAVAWEB学习,java,定位)