AmapManager.java 6.96 KB
package cn.com.glsx.entmicro.manager;

import cn.com.glsx.entmicro.config.AmapProperty;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.oreframework.util.http.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: AmapManager.java
 * @Description: 高德地图接口管理
 * @author Alvin
 * @date 2019年9月5日 下午3:11:02
 * @version V1.0
 * @Company: didihu.com.cn
 * @Copyright Copyright (c) 2018
 */
@Slf4j
@Component
public class AmapManager {

    Logger logger = Logger.getLogger(getClass());

    @Autowired
    private AmapProperty property;

    static final String STATUSTYPE = "1";
    static final String BAIDU_STATUSTYPE = "0";
    static final String RESULTE_INFO = "OK";
    static final String OUTPUT = "JSON";
    static final String BAIDU_OUTPUT = "json";
    static final String EXTENSIONS_ALL = "all";
    static final String COORDTYPE = "bd09ll";
    static final String FROM = "3";
    static final String TO = "5";

    public String getAddrFromLngLat(String lng, String lat) {
        String[] strings = convertCoordinates(lng, lat);
        if (strings == null || strings.length == 0) {
            log.info("百度坐标转换失败");
            return "";
        }
        Map<String, String> params = new HashMap<>(10);
        params.put("location", strings[0] + "," + strings[1]);
        params.put("coordtype", COORDTYPE);
        params.put("ak", property.getBaiduKey());
        params.put("output", BAIDU_OUTPUT);
        params.put("extensions_poi", "1");
        String response = null;
        try {
            response = HttpUtil.post(property.getBaiduGeocodeUrl(), params);
        } catch (IOException e) {
            log.error("通过经纬度获取详细地址异常:" + e.getMessage() + e);
        }
        if (response == null) {
            log.info("返回地址为空");
            return "";
        }
        log.info("通过经纬度获取详细地址:" + response);
        JSONObject json = JSONObject.fromObject(response);
        String status = json.getString("status");
        log.info("解析地址的状态为:" + status);
        String address = null;
        if (BAIDU_STATUSTYPE.equals(status)) {
            try {
                JSONObject result = JSONObject.fromObject(json.get("result"));
                address = result.getString("formatted_address_poi");
            } catch (Exception e) {
                log.info("解析地址数据异常:{}", json);
                return null;
            }
        }
        log.info("原始坐标:{},百度坐标:{},address:{}", lng + "," + lat, strings[0] + "," + strings[1], address);
        return address;
    }

    public String[] convertCoordinates(String lat, String lng) {
        Map<String, String> params = new HashMap<>(10);
        params.put("coords", lng + "," + lat);
        params.put("ak", property.getBaiduKey());
        params.put("from", FROM);
        params.put("to", TO);
        String response = null;
        try {
            response = HttpUtil.post(property.getGeoconvUrl(), params);
        } catch (IOException e) {
            log.error("百度坐标转换异常:" + e.getMessage() + e);
        }
        if (response == null) {
            log.info("百度坐标返回为空");
            return null;
        }
        JSONObject json = JSONObject.fromObject(response);
        String status = json.getString("status");
        log.info("解析百度坐标的状态为:" + status);
        String[] coordinates = new String[2];
        if (BAIDU_STATUSTYPE.equals(status)) {
            try {
                JSONObject result = json.getJSONArray("result").getJSONObject(0);
                coordinates[0] = result.getString("x");
                coordinates[1] = result.getString("y");
            } catch (Exception e) {
                log.info("百度坐标数据异常:{}", json);
                return null;
            }
        }
        return coordinates;
    }

    /**
     * @param lng:经度,lat:纬度
     * @return 地址
     * @description 根据经纬度查地址
     * @author leiyj
     * @date 2019年1月14日
     */
    public String getAddrFromLngLatFromGaode(String lng, String lat) {
        Map<String, String> params = new HashMap<String, String>(10);
        params.put("location", lng + "," + lat);
        params.put("output", OUTPUT);
        params.put("key", property.getKey());
        params.put("extensions", EXTENSIONS_ALL);
        String result = null;
        try {
            result = HttpUtil.post(property.getGeocodeUrl(), params);
        } catch (IOException e) {
            logger.error("通过经纬度获取详细地址异常:" + e.getMessage() + e);
        }
        if (result == null) {
            logger.info("返回地址为空");
            return "";
        }
        JSONObject json = JSONObject.fromObject(result);
        String status = json.getString("status");
        logger.info("解析地址的状态为:" + status);
        String address = null;
        if (STATUSTYPE.equals(status)) {
            JSONObject regeocode = JSONObject.fromObject(json.get("regeocode"));
            address = regeocode.getString("formatted_address");
        }
        return address;
    }


    /**
     * @param origins:起点经纬度,destination:终点经纬度
     * @return 地址
     * @description 根据起点和终点经纬度查询距离
     * @author leiyj
     * @date 2019年1月14日
     */
    public Double getDistance(String origins, String destination) {
        Map<String, String> params = new HashMap<String, String>(10);
        params.put("origins", origins);
        params.put("destination", destination);
        params.put("output", OUTPUT);
        params.put("key", property.getKey());
        params.put("type", "0");
        String result = null;
        try {
            result = HttpUtil.post(property.getDistanceUrl(), params);
        } catch (IOException e) {
            logger.error("经纬度计算距离异常:" + e.getMessage() + e);
        }
        if (result == null) {
            logger.info("返回计算距离为空");
            return 0D;
        }
        Double distance = 0D;
        logger.info(result);
        JSONObject json = JSONObject.fromObject(result);
        String status = json.getString("status");
        String info = json.getString("info");
//        logger.info("据起点和终点经纬度查询返回状态:" + status);
        if (STATUSTYPE.equals(status)) {
            JSONArray results = JSONArray.fromObject(json.get("results"));
            String r = results.getString(0);
            JSONObject json1 = JSONObject.fromObject(r);
            distance = Double.parseDouble(json1.getString("distance"));
            logger.info("俩地相隔的距离:" + distance + "米");
        }
        return distance / 1000;
    }
}