• amap 高德地图


    package cab.bear.util;

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.URL;

    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;

    /**
     * 高德地图工具
     */
    @Component
    public class AMapUtil {
        @Value("${amap.key}")
        private String key; // 高德地图开发者 key
        
        /**
         * 关键字搜索地址
         * @param keywords
         * @return
         * @throws MalformedURLException
         * @throws URISyntaxException
         */
        public JSONArray search(String keywords) throws MalformedURLException, URISyntaxException {
            String searchUrl = "https://restapi.amap.com/v5/place/text?key=" + key + "&keywords=" + keywords;
            URL url = new URL(searchUrl);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(uri);
            
            CloseableHttpResponse response = null;

            try {
                response = httpClient.execute(httpGet);
                String content = EntityUtils.toString(response.getEntity());
                JSONObject parseObject = JSON.parseObject(content);
                String status = parseObject.getString("status");
                if("1".equals(status)) {
                    JSONArray pois = parseObject.getJSONArray("pois");
                    return pois;
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        
        /**
         * 关键字搜索地址
         * @param region
         * @param keywords
         * @return
         * @throws MalformedURLException
         * @throws URISyntaxException
         */
        public JSONArray search(String region ,String keywords) throws MalformedURLException, URISyntaxException {
            String searchUrl = "https://restapi.amap.com/v5/place/text?key=" + key + "&keywords=" + keywords + "&citylimit=true®ion=" + region; // region 搜索区域,默认全国范围;citylimit:仅返回指定城市数据,默认false
            URL url = new URL(searchUrl);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(uri);
            
            CloseableHttpResponse response = null;

            try {
                response = httpClient.execute(httpGet);
                String content = EntityUtils.toString(response.getEntity());
                JSONObject parseObject = JSON.parseObject(content);
                String status = parseObject.getString("status");
                if("1".equals(status)) {
                    JSONArray pois = parseObject.getJSONArray("pois");
                    return pois;
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        
        
        /**
         * 地址转换为经纬度
         * @param address
         * @return 经纬度
         * @throws MalformedURLException 
         * @throws URISyntaxException 
         */
        public JSONArray getLonLat(String address) throws MalformedURLException, URISyntaxException {
            String lonLatUrl = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&batch=true&address=" + address;
            URL url = new URL(lonLatUrl);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(uri);
            
            CloseableHttpResponse response = null;
            
            try {
                response = httpClient.execute(httpGet);
                String content = EntityUtils.toString(response.getEntity());
                JSONObject parseObject = JSON.parseObject(content);
                String status = parseObject.getString("status");
                if("1".equals(status)) {
                    JSONArray regeocodes = parseObject.getJSONArray("geocodes");
                    return regeocodes;
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        
        /**
         * 根据两个定位点的经纬度获取两点之间驾车路径规划
         * @throws MalformedURLException 
         * @throws URISyntaxException 
         */
        public JSONObject getDriving(String originLonLat, String destinationLonLat,int strategy) throws MalformedURLException, URISyntaxException {
            String drivingUrl = "https://restapi.amap.com/v3/direction/driving?key=" + key + "&strategy=" + strategy + "&origin=" + originLonLat + "&destination=" + destinationLonLat;
            URL url = new URL(drivingUrl);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(uri);
            
            CloseableHttpResponse response = null;
            
            try {
                response = httpClient.execute(httpGet);
                String content = EntityUtils.toString(response.getEntity());
                JSONObject parseObject = JSON.parseObject(content);
                String status = parseObject.getString("status");
                if("1".equals(status)) {
                    JSONObject jsonObject = parseObject.getJSONObject("route");
                    return jsonObject;
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        
        /**
         * 根据两个地址关键字获取两点之间驾车路径规划
         * @throws URISyntaxException 
         * @throws MalformedURLException 
         */
        public JSONObject getAddressDriving(String originAddress, String destinationAddress, int strategy) throws MalformedURLException, URISyntaxException {
            JSONArray lonLat = getLonLat(originAddress + "|" + destinationAddress);
            if(lonLat != null) {
                return getDriving(lonLat.getJSONObject(0).getString("location"), lonLat.getJSONObject(1).getString("location"), strategy);
            }
            return null;
        }
        
        /**
         * 行政区域查询
         * @throws MalformedURLException 
         * @throws URISyntaxException 
         */
        public JSONArray getDistricts(Integer subdistrict) throws MalformedURLException, URISyntaxException {
            // https://restapi.amap.com/v3/config/district?subdistrict=3 # subdistrict: 0:不返回下级行政区;1:返回下一级行政区;2:返回下两级行政区;3:返回下三级
            String districtUrl = "https://restapi.amap.com/v3/config/district?key=" + key + "&subdistrict=" + subdistrict;
            URL url = new URL(districtUrl);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(uri);
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpGet);
                if (response.getStatusLine().getStatusCode() == 200) {
                    String content = EntityUtils.toString(response.getEntity());
                    JSONObject parseObject = JSON.parseObject(content);
                    String status = parseObject.getString("status");
                    if("1".equals(status)) {
                        JSONArray array = parseObject.getJSONArray("districts");
                        for(int n = 0; n < array.size(); n++) {
                            JSONObject district = array.getJSONObject(n);
                            if("country".equals(district.getString("level"))  && "100000".equals(district.getString("adcode"))) {
                                // 中华人民共和国行政区域
                                JSONArray districts = district.getJSONArray("districts");
                                return districts;
                            }
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        
        public static void main(String[] args) throws MalformedURLException, URISyntaxException {
            String districtUrl = "https://restapi.amap.com/v3/config/district?key=6ebe75ea77cd6f178e3073de21a57a6e&subdistrict=3";
            URL url = new URL(districtUrl);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(uri);
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpGet);
                if (response.getStatusLine().getStatusCode() == 200) {
                    String content = EntityUtils.toString(response.getEntity());
                    JSONObject parseObject = JSON.parseObject(content);
                    String status = parseObject.getString("status");
                    if("1".equals(status)) {
                        JSONArray array = parseObject.getJSONArray("districts");
                        for(int n = 0; n < array.size(); n++) {
                            JSONObject district = array.getJSONObject(n);
                            if("country".equals(district.getString("level"))  && "100000".equals(district.getString("adcode"))) {
                                // 中华人民共和国行政区域
                                JSONArray districts = district.getJSONArray("districts");
                                districts.toJSONString();
                            }
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
     

  • 相关阅读:
    MySQL中的SHOW FULL PROCESSLIST命令
    LVGL学习(5):物理按键切换焦点之焦点保存和恢复
    第 5 章 数组和广义表(数组的顺序存储结构实现)
    MySQL MVCC详细介绍
    一篇博客告诉你什么是时间复杂度和空间复杂度(没有基础也能看懂)(这是学习数据结构及算法所必须掌握的基础)
    计算机视觉:基于Numpy的图像处理技术(二):图像主成分分析(PCA)
    沁恒全方位提供多种USB串口驱动第3代USB转串口产品
    Allegro软件Shape菜单下的每个命令的含义
    【React】12.路由
    C#编程模式之建造者模式
  • 原文地址:https://blog.csdn.net/phone13144830339/article/details/126226217