amap 高德地图

这是一个Java组件,实现了高德地图API的功能,包括关键字搜索地址、指定区域搜索、地址转经纬度以及路径规划。组件通过HttpClients发送GET请求到高德地图API接口,解析返回的JSON数据并提供相应的业务方法。
摘要由CSDN通过智能技术生成

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&region=" + 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();
            }
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值