高德地图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<Map>) 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<Map> geocodes = (List<Map>) 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<Map> geocodes = (List<Map>) 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<Map> geocodes = (List<Map>) 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<Map> geocodes = (List<Map>) 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)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值