栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

amap 高德地图

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
    
    
    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;
    }
    
    
    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;
    }
    
    
    
    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;
    }
    
    
    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;
    }
    
    
    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;
    }
    
    
    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();
            }
        }
    }
}
 

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040457.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号