2025年整理了一份谷歌地址校验查询的案例。

整理了一份谷歌地址校验查询的案例。都知道谷歌很牛逼 自从用了他的地址校验 别的我都不想用了 代码 package com yiexpress core utils aupost import java io IOException import java util HashMap import java util List import java util Map

大家好,我是讯享网,很高兴认识大家。

都知道谷歌很牛逼。自从用了他的地址校验,别的我都不想用了。

代码

package com.yiexpress.core.utils.aupost; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.amazonaws.util.json.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import com.yiexpress.aupost.dto.googlebean.AddressComponents; import com.yiexpress.aupost.dto.googlebean.GoogleGeocodeJSONBean; import com.yiexpress.aupost.dto.googlebean.Results; import com.yiexpress.cainiao.config.CainiaoConstants; import com.yiexpress.core.utils.AnJieUtils; import com.yiexpress.core.utils.DictUtils; import com.yiexpress.core.utils.FormatUtils; import com.yiexpress.core.utils.HttpUtils; / * * @author Martin * */ public final class GoogleGeocoderUtil { public static final int ADDRESS = 1; public static final int LATLNG = 2; private final static String GOOGLEAPIURL="https://ditu.google.cn"; private static final Logger LOGGER=LoggerFactory.getLogger(GoogleGeocoderUtil.class); private static final ObjectMapper jsonMapper=new ObjectMapper(); / * * @param address 坐标(,12312)这种方式,)或者直接地址address="12321,2312" address="上海市普陀区" * @param lang null时以en为准 语言 * @return * @throws Exception */ public static GoogleGeocodeJSONBean geocodeByAddress(String address,String lang) throws Exception{ if(StringUtils.isBlank(lang)){ lang="en"; } LOGGER.info("谷歌地址解析开始:"); String path="/maps/api/geocode/json"; Map<String, String> body = new HashMap<String, String>(); body.put("language", lang); body.put("sensor", "false"); body.put("address", address); body.put("key", CainiaoConstants.GOOGLE_API_KEY); JSONObject object=null; try { HttpResponse response =HttpUtils.doPost(GOOGLEAPIURL, path, DictUtils.GET_METHOD, new HashMap<String, String>() ,body, new HashMap<String, String>()); object = ChEnUtils.getJsonByRes(response); GoogleGeocodeJSONBean bean = jsonMapper.readValue(object.toString(), GoogleGeocodeJSONBean.class); //LOGGER.info(object.toString()); return bean; } catch (Exception e) { LOGGER.info("请求发生了错误!==>{}",e); } return null; } public static void main(String[] args) throws Exception { try { // NORTH LAKES QLD LJ Hooker North Lakes // String info=GoogleGeocoderUtil.checkAddrIsCorrect("CN","湖南省","株洲","中心广场百货大楼","","CN",true); // System.out.println(info); GoogleGeocoderUtil.geocodeByAddress("上海三门路青新公寓", "CN"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(checkAddrIsCorrect("AU", "NSW", "Greenacre", null,"2190", "en")); //System.out.println("AU".contains("A")); } / * 校验地址的准确性 * @param country 国家 * @param state 省/州 * @param suburb 城市 * @param stream 地址 * @param postCode 邮编 * @param lang 语言 * @param isValidStream 是否校验地址 * @return */ public static String checkAddrIsCorrect(String country,String state,String suburb,String stream,String postCode,String lang,boolean isValidStream){ String result = DictUtils.SUCCESS; String address=stream+","+suburb+","+state+","+country; LOGGER.info("您的地址是:{}",address); GoogleGeocodeJSONBean info; try { info = GoogleGeocoderUtil.geocodeByAddress(address, lang); } catch (Exception e) { LOGGER.info("系统发生错误,请联系管理员(网络错误)或者解析javabean错误)!==>{}",e); return "系统发生错误,请联系管理员!"; } if("OK".equalsIgnoreCase(info.getStatus())){ if(info.getResults()==null||info.getResults().size()==0){ return "请填写正确的地址"; } Results googleResult = info.getResults().get(0); List<AddressComponents> addressCom = googleResult.getAddress_components(); // 遍历返回的地址信息 String googleShortCountry=null; String googleShortState=null; String googleShortsuburb=null; String googleShortTown=null; String googleShortStream=null; String googleShortStreamNumber=null; String googleLongCountry=null; String googleLongState=null; String googleLongsuburb=null; String googleLongTown=null; String googleLongStream=null; String googleLongStreamNumber=null; String googlePostCode=null; for(AddressComponents addressComponent:addressCom){ if(addressComponent.getTypes()[0].equalsIgnoreCase("country")){ googleShortCountry = addressComponent.getShort_name(); googleLongCountry = addressComponent.getLong_name(); } else if(addressComponent.getTypes()[0].equalsIgnoreCase("administrative_area_level_2")){ googleShortState = addressComponent.getShort_name(); googleLongState = addressComponent.getLong_name(); } else if(addressComponent.getTypes()[0].equalsIgnoreCase("postal_town")){ googleShortTown = addressComponent.getShort_name(); googleLongTown = addressComponent.getLong_name(); } else if(addressComponent.getTypes()[0].equalsIgnoreCase("locality")){ googleShortsuburb = addressComponent.getShort_name(); googleLongsuburb = addressComponent.getLong_name(); } else if(addressComponent.getTypes()[0].equalsIgnoreCase("route")){ googleShortStream = addressComponent.getShort_name(); googleLongStream = addressComponent.getLong_name(); } else if(addressComponent.getTypes()[0].equalsIgnoreCase("street_number")){ googleShortStreamNumber = addressComponent.getShort_name(); googleLongStreamNumber = addressComponent.getLong_name(); } else if(addressComponent.getTypes()[0].equalsIgnoreCase("postal_code")){ googlePostCode = addressComponent.getShort_name(); } } // 定制 为英国GB 校验邮编和城市 没有州 无法校验 // if("GB".equalsIgnoreCase(validCountry)){ // 首先校验国家,省州,城市是否匹配 if(StringUtils.isNotBlank(country)&&StringUtils.isNotBlank(googleLongCountry)&&!(country.equalsIgnoreCase(googleLongCountry)||country.equalsIgnoreCase(googleShortCountry))){ return "国家不匹配,结果:全名("+googleLongCountry +" )或者缩写名("+googleShortCountry+")。您的:"+country; } /*if(StringUtils.isNotBlank(state)&&StringUtils.isNotBlank(googleLongState)&&!(state.equalsIgnoreCase(googleLongState)||state.equalsIgnoreCase(googleShortState))){ return "省/州(地区)不匹配,结果:全名("+googleLongState +" )或者缩写名("+googleShortState+")。您的:"+state; }*/ if(StringUtils.isNotBlank(suburb)&&StringUtils.isNotBlank(googleLongTown)&&!(suburb.equalsIgnoreCase(googleLongsuburb)||suburb.equalsIgnoreCase(googleShortsuburb))){ // 如果连镇都不匹配 if(googleLongsuburb==null&&googleShortsuburb==null){ if(!(suburb.equalsIgnoreCase(googleLongTown)||suburb.equalsIgnoreCase(googleShortTown))){ return "城市(地市)不匹配,结果:全名("+googleLongTown +" )或者缩写名("+googleShortTown+")。您的:"+suburb; } }else{ return "城市(地市)不匹配,结果:全名("+googleLongsuburb +" )或者缩写名("+googleShortsuburb+")。您的:"+suburb; } } String shortStream = googleShortStreamNumber+" "+googleShortStream; String longStream = googleLongStreamNumber+" "+googleLongStream; if(isValidStream){ if(googleShortStreamNumber==null||googleLongStream==null){ return "请填写正确的地址!"; } if(StringUtils.isNotBlank(shortStream)&& !( AnJieUtils.trim(stream.toUpperCase()).contains(AnJieUtils.trim(shortStream.toUpperCase())) ||AnJieUtils.trim(stream.toUpperCase()).contains(AnJieUtils.trim(longStream.toUpperCase()))) ){ return "街道地址不包含,结果:全名("+longStream +" )或者缩写名("+shortStream+")。您的:"+stream; } } if(StringUtils.isNotBlank(postCode)&&!AnJieUtils.trim(postCode).equalsIgnoreCase(AnJieUtils.trim(googlePostCode))){ if(googlePostCode==null){ return "请填写正确的地址!"; } return "邮编不匹配,结果:"+googlePostCode+" 。您的:"+postCode; } // } }else{ return "地址不存在,请检查您输入的地址"; } return result; } }

讯享网

以上是一份案例。对英国地址进行校验的案例。

如果你刚好,需要,也不需要吝啬。

地址自己去下 :https://download.csdn.net/download/_/


讯享网

对于谷歌返回参数:

 {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara",
               "short_name" : "Santa Clara",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.,
               "lng" : -122.0
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.149,
                  "lng" : -122.05
               },
               "southwest" : {
                  "lat" : 37.850,
                  "lng" : -122.05
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
} 
请参考链接 说明这是参数说明

https://blog.csdn.net/_/article/details/

 

小讯
上一篇 2025-01-27 23:30
下一篇 2025-03-21 20:47

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/115184.html