根据经纬度获取省市信息

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.beyond.reception.domain.vo.AreaDataVo;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

/**
 * 地图工具类
 */
@Slf4j
public class AreaUtils {

    private static final String TX_APIKEY = "";

    private static final String GD_APIKEY = "";

    private static final String BD_APIKEY = "";

    /**
     * 腾讯地图 api
     */
    public static AreaDataVo getAreaName(String latitude, String longitude) {
        String urlStr = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + latitude + "," + longitude + "&get_poi=0&key=" + TX_APIKEY;
        try {
            URL url = new URL(urlStr);
            log.info("腾讯地图坐标转换地址url开始:---------》{}", urlStr);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            log.info("腾讯地图坐标转换地址返回响应码:---------》{}", responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                log.info("腾讯地图坐标转换地址返回JSON数据:---------》{}", response);
                JSONObject jsonObject = JSONUtil.parseObj(response.toString());
                JSONObject result = JSONUtil.parseObj(jsonObject.get("result"));
                JSONObject adInfo = JSONUtil.parseObj(result.get("ad_info"));
                AreaDataVo vo = new AreaDataVo();
                vo.setProvince(adInfo.get("province").toString());
                vo.setCity(adInfo.get("city").toString());
                vo.setDistrict(adInfo.get("district").toString());
                return vo;
            } else {
                log.error("坐标转换地址有误,请联系管理员");
                return null;
            }
        } catch (Exception e) {
            log.error("坐标转换地址有误,请联系管理员:{}", e.getMessage());
        }
        return null;
    }

    /**
     * 高德地图 api
     */
    public static AreaDataVo getAreaNameByAliyun(String latitude, String longitude) {
        String urlStr = "https://restapi.amap.com/v3/geocode/regeo?location=" + latitude + "," + longitude + "&key=" + GD_APIKEY;
        try {
            URL url = new URL(urlStr);
            log.info("高德地图坐标转换地址url开始:---------》{}", urlStr);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            log.info("高德地图坐标转换地址返回响应码:---------》{}", responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                log.info("高德地图坐标转换地址返回JSON数据:---------》{}", response);
                JSONObject jsonObject = JSONUtil.parseObj(response.toString());
                JSONObject result = JSONUtil.parseObj(jsonObject.get("regeocode"));
                JSONObject adInfo = JSONUtil.parseObj(result.get("addressComponent"));
                AreaDataVo vo = new AreaDataVo();
                String[] cities = {"上海市", "重庆市", "北京市", "天津市"};
                if (Arrays.asList(cities).contains(adInfo.get("province").toString())) {
                    vo.setProvince(adInfo.get("province").toString());
                    vo.setCity(adInfo.get("province").toString());
                    vo.setDistrict(adInfo.get("district").toString());
                } else {
                    vo.setProvince(adInfo.get("province").toString());
                    vo.setCity(adInfo.get("city").toString());
                    vo.setDistrict(adInfo.get("district").toString());
                }
                return vo;
            } else {
                log.error("坐标转换地址有误,请联系管理员");
                return null;
            }
        } catch (Exception e) {
            log.error("坐标转换地址有误,请联系管理员:{}", e.getMessage());
        }
        return null;
    }

    /**
     * 百度地图 api
     */
    public static AreaDataVo getAreaNameByBaidu(String latitude, String longitude) {
        String urlStr = "https://api.map.baidu.com/reverse_geocoding/v3/?location=" + latitude + "," + longitude + "&extensions_poi=0&output=json&ak=" + BD_APIKEY;
        try {
            URL url = new URL(urlStr);
            log.info("百度地图坐标转换地址url开始:---------》{}", urlStr);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            log.info("百度地图坐标转换地址返回响应码:---------》{}", responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                log.info("百度地图坐标转换地址返回JSON数据:---------》{}", response);
                JSONObject jsonObject = JSONUtil.parseObj(response.toString());
                JSONObject result = JSONUtil.parseObj(jsonObject.get("result"));
                JSONObject adInfo = JSONUtil.parseObj(result.get("addressComponent"));
                AreaDataVo vo = new AreaDataVo();
                vo.setProvince(adInfo.get("province").toString());
                vo.setCity(adInfo.get("city").toString());
                vo.setDistrict(adInfo.get("district").toString());
                return vo;
            } else {
                log.error("坐标转换地址有误,请联系管理员");
                return null;
            }
        } catch (Exception e) {
            log.error("坐标转换地址有误,请联系管理员:{}", e.getMessage());
        }
        return null;
    }
}

import lombok.Data;
import java.io.Serializable;

/**
 * 地区数据
 */
@Data
public class AreaDataVo implements Serializable {

    /**
     * 省
     */
    private String province;

    /**
     * 市
     */
    private String city;

    /**
     * 区
     */
    private String district;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值