高德地图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
    评论
高德地图API是一款功能强大的地图服务,它提供了多种功能和接口供开发者使用使用高德地图API需要经过以下几个步骤: 1. 首先,你需要申请一个高德地图开发者账号,并创建一个应用。你可以访问高德地图开放平台的官方网站,在那里你可以找到相关的注册和创建应用的步骤。 2. 在创建应用后,你会得到一个API Key,这是你使用高德地图API的权限凭证。在使用API时,你需要在请求中携带这个API Key。 3. 接下来,你可以根据你的需求选择合适的API接口进行开发。高德地图API提供了地图显示、地理编码、逆地理编码、路径规划、导航等一系列功能的接口。 4. 根据你选择的接口,你需要按照API文档中的要求构造API请求,并将API Key携带在请求中。 5. 发送请求后,你会收到API的响应数据。根据响应数据的格式和内容,你可以进行相应的处理和展示。 以下是一些常用的高德地图API接口和功能: - 地理编码:将地址转换为经纬度坐标,可以使用 Geocoding API 实现。 - 逆地理编码:将经纬度坐标转换为具体地址,可以使用 Regeocoding API 实现。 - 路径规划:根据起点和终点计算出最优路径,可以使用 Directions API 实现。 - 导航:提供步行、驾车、骑行等导航功能,可以使用 Navigation API 实现。 - 地图显示:在网页或移动应用中显示地图,可以使用 JavaScript API 或 Android/iOS SDK 实现。 希望以上信息能帮助你初步了解如何使用高德地图API。如果你有其他相关问题,请随时提问。 相关问题: 1. 高德地图API有哪些常用的功能? 2. 如何在网页中使用高德地图? 3. 高德地图API的请求参数有哪些?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值