JAVA 获取省市区(根据字符或者根据百度API经纬度获取)

public class AddressResolutionUtil {


    public static  String SpecialCounty="仙桃市,潜江市,天门市,济源市,东方市,万宁市,文昌市,儋州市,琼海市,五指山市,石河子市,阿拉尔市,图木舒克市,五家渠市,北屯市,铁门关市,双河市";

    /**
     * 解析地址
     * @param address
     * @return
     */
    public static CityInfo addressResolution(String address){
         String regex="^(?<province>[^省]+省|.+自治区)?(?<city>[^市]+市|.+自治州)?(?<county>[^县]+县|.+区|.+市)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m=Pattern.compile(regex).matcher(address);

       
        String[] citys=SpecialCounty.split(",");
        List<String> specialCountyList = Arrays.asList(citys);


        String province,city,county=null;
        CityInfo info=new CityInfo();
        while(m.find()){
            province=m.group("province");
            info.setProvince(province==null?"":province.trim());
            city=m.group("city");
            if(StringUtil.isNotEmpty(city)&&specialCountyList.contains(city)){
                info.setCounty(city);
                continue;
            }
            info.setCity(city==null?"":city.trim());
            county=m.group("county");
            if(StringUtil.isNotEmpty(county)){
                if(county.indexOf("市")>0){
                    String  seCity=county.substring(0,county.indexOf("市")+1);
                    info.setCounty(seCity==null?"":seCity.trim());
                }else if(county.indexOf("区")>0){
                    String  seCity=county.substring(0,county.indexOf("区")+1);

                    info.setCounty(seCity==null?"":seCity.trim());
                }else {
                    String  seCity=county.substring(0,county.indexOf("县")+1);

                    info.setCounty(seCity==null?"":seCity.trim());
                }
            }


        }
        return info;

    }

    /**
     * 根据经纬度查询
     * @param log
     * @param lat
     * @return
     */
    public static String getAdd(String log, String lat ){
        //lat 小  log  大
        //参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
        String urlString = "http://api.map.baidu.com/geocoder/v2/?ak=0EXAjYp9hii1DrK3Tuda8efu9vivslcX&callback=renderReverse&location="+ lat + "," + log + "&output=json&pois=1";
        String res = "";
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line+"\n";
            }
            in.close();
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        System.out.println(res);
        return res;
    }


    public static void main(String[] args) {
      /*  System.out.println(getAdd(119.0478515625+"",31.5785354265+""));*/
        //System.out.println(addressResolution("河南省仙桃市"));
        double b=(double) 560/100;
        System.out.println(b);
    }

}

@Data
public class CityInfo {
    /**
     * 省
     */
    private String province;

    /**
     * 市
     */
    private String city;

    /**
     * 区
     */
    private String county;

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据百度API获取经纬度对应的省市区信息,可以使用Python进行实现。以下是一个示例代码: ```python import requests def get_location_info(latitude, longitude): ak = '你的百度开发者API密钥' # 需要替换为自己的百度开发者API密钥 url = f'http://api.map.baidu.com/reverse_geocoding/v3/?ak={ak}&output=json&coordtype=wgs84ll&location={latitude},{longitude}' response = requests.get(url) data = response.json() if data['status'] == 0: province = data['result']['addressComponent']['province'] city = data['result']['addressComponent']['city'] district = data['result']['addressComponent']['district'] return province, city, district else: return None # 使用示例 latitude = 39.9087 # 纬度 longitude = 116.3975 # 经度 location_info = get_location_info(latitude, longitude) if location_info: province, city, district = location_info print(f'该经纬度所在位置为:省份:{province},城市:{city},区县:{district}') else: print('获取位置信息失败') ``` 以上代码,我们使用了Python的requests库发送HTTP请求,并通过百度API的逆地理编码接口获取经纬度对应的位置信息。需要注意,你需要将`ak`变量的值替换为自己的百度开发者API密钥。 具体步骤为: 1. 引入requests库,用于发送HTTP请求。 2. 定义一个`get_location_info`函数,该函数接受经度和纬度作为参数,并返回省市区的信息。 3. 在函数构建请求URL,包括百度API密钥、经纬度等信息,并发送GET请求获取响应数据。 4. 解析API返回的JSON数据,提取省市区信息。 5. 在主程序,调用`get_location_info`函数,传入经纬度参数,获取位置信息。 6. 如果成功获取位置信息,则将其打印出来;否则输出获取位置信息失败的提示。 注意,以上代码仅为示例,具体的实现需要根据你所使用的具体百度API接口和参数进行适当的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值