All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.AmapUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
package com.github.dennisit.vplus.data.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.List;

// http://restapi.amap.com/v3/geocode/geo?key=389880a06e3f893ea46036f030c94700&s=rsv3&city=35&address=%E6%9C%9B%E4%BA%AC%E5%9B%BD%E9%99%85%E7%A0%94%E5%8F%91%E5%9B%AD
@Slf4j
public class AmapUtils {

    // 地理编码地址
    private static String AMAP_RESTAPI = "http://restapi.amap.com/v3/geocode/geo";

    private static String AMAP_APP_KEY = "389880a06e3f893ea46036f030c94700";

    private static RestTemplate restTemplate = new RestTemplate();

    @Data
    @NoArgsConstructor
    public static class AmapResult implements Serializable {

        // 结果状态0,表示失败,1:表示成功
        private String status;

        // 返回结果的数目
        private String count;

        // 返回状态说明
        private String info;

        // 识别的结果列表
        private List geocodes;
    }

    @Data
    @NoArgsConstructor
    public static class Geocodes implements Serializable {
        // 结构化地址信息
        private String formatted_address;
        // 所在省
        private String province;
        // 城市
        private String city;
        // 城市编码
        private String citycode;
        // 地址所在的区
        private String district;
        // 地址所在的乡镇
        private String township;
        // 街道
        private String street;
        // 门牌
        private String number;
        // 区域编码
        private String adcode;
        // 坐标点
        private String location;
        // 匹配级别
        private String level;
    }

    /**
     * @param address 待查询地址
     * @return 返回结果
     */
    public static AmapResult getLocation(String address) {
        return getLocation(AMAP_APP_KEY, address);
    }

    /**
     * @param appKey  应用Key
     * @param address 待查询地址
     * @return 返回结果
     */
    public static AmapResult getLocation(String appKey, String address) {
        return getLocation(AMAP_RESTAPI, appKey, address);
    }

    /**
     * @param restApi 服务地址
     * @param appKey  应用Key
     * @param address 待查询地址
     * @return 返回结果
     */
    public static AmapResult getLocation(String restApi, String appKey, String address) {
        AmapResult location = null;
        if (address != null) {
            try {
                location = new AmapResult();
                String url = restApi + "?key=" + appKey + "&address=" + address;
                log.info("高德地图params:" + url);
                String result = restTemplate.getForObject(url, String.class);// OKHttpUtil.httpPost(url, params);
                System.out.println("高德地图返回结果:" + result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                location.setStatus(jsonObject.getString("status"));
                location.setInfo(jsonObject.getString("info"));
                location.setCount(jsonObject.getString("count"));
                List geocodes = Lists.newArrayList();
                JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
                if ((jsonArray != null) && (jsonArray.size() > 0)) {
                    JSONObject jo = (JSONObject) jsonArray.get(0);
                    Geocodes go = new Geocodes();
                    go.setFormatted_address(jo.getString("formatted_address"));
                    go.setProvince(jo.getString("province"));
                    go.setCitycode(jo.getString("citycode"));
                    go.setCity(jo.getString("city"));
                    go.setDistrict(jo.getString("district"));
                    // 地址所在的乡镇
                    JSONArray ts = jo.getJSONArray("township");
                    if (ts != null && ts.size() > 0) {
                        go.setTownship(ts.getString(0));
                    }
                    // 地址编号
                    go.setAdcode(jo.getString("adcode"));
                    // 街道
                    JSONArray jd = jo.getJSONArray("street");
                    if (jd != null && jd.size() > 0) {
                        go.setStreet(jd.getString(0));
                    }
                    // 号码
                    JSONArray hm = jo.getJSONArray("number");
                    if (hm != null && hm.size() > 0) {
                        go.setStreet(hm.getString(0));
                    }
                    go.setLocation(jo.getString("location"));
                    go.setLevel(jo.getString("level"));
                    geocodes.add(go);
                }
                location.setGeocodes(geocodes);
            } catch (Exception e) {
                log.error(e.getLocalizedMessage(), e);
            }
        }
        return location;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy