使用Google API,jQuery从地址解析中提取加4个邮政编码 [英] Extracting plus 4 zip code from geocoding using google API, jQuery

查看:84
本文介绍了使用Google API,jQuery从地址解析中提取加4个邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery的帮助下使用Google API进行地理编码.以下是我尝试过的代码.

I am using Geo coding using google API with the help of jQuery. The following is the code i tried.

jQuery(document).ready(function () {
var geocoder = new google.maps.Geocoder();
jQuery('AddressPicker1').autocomplete({
    source: function (request, response) {
        geocoder.geocode({ address: request.term}, function (results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
          response(jQuery.map(results, function(item) {
          var streetNumber = "";
          var streetName = "";
          var cityName = "";
          var zip = "";
          var country = "";
          var countyName = "";
          var state = "";  

          for ( var component =0; component < item.address_components.length; component++ ) {
             if (item.address_components[component] != undefined && item.address_components[component] != null ) {
                  if ( jQuery.inArray("street_number", item.address_components[component].types) > -1 ){
                      streetNumber = item.address_components[component].long_name;
                  }

                  if ( jQuery.inArray("route", item.address_components[component].types) > -1){
                      streetName = item.address_components[component].long_name;
                  }

                  if ( jQuery.inArray("locality", item.address_components[component].types) > -1){
                      cityName = item.address_components[component].long_name;
                  }

                  if ( jQuery.inArray("postal_code", item.address_components[component].types) > -1){
                      zip = item.address_components[component].long_name;
                  }

                  if ( jQuery.inArray("country", item.address_components[component].types) > -1){
                      country = item.address_components[component].long_name;
                  }

                  if ( jQuery.inArray("administrative_area_level_2", item.address_components[component].types) > -1){
                        countyName = item.address_components[component].long_name;
                  }

                  if ( jQuery.inArray("administrative_area_level_1", item.address_components[component].types) > -1){
                        state = item.address_components[component].short_name; 
                  }
            }
          }
    return {
        label : item.formatted_address,
        value : item.formatted_address,
        latitude : item.geometry.location.lat(),
        longitude : item.geometry.location.lng(),
        streetNo : streetNumber,
        streetName : streetName,
        city : cityName.toUpperCase() ,
        zip : zip,
        country : country,
        county : countyName.toUpperCase(),
        state : state
    }

     }));
   }
 });
 },
 select: function (event, ui) {
      jQuery('StreetNumber').val(ui.item.streetNo);
      jQuery('StreetName').val(ui.item.streetName);
      jQuery('City').val(ui.item.city);
      jQuery('Zip').val(ui.item.zip);
      jQuery('Country').val(ui.item.country);
      jQuery('CountyName').val(ui.item.county);
      jQuery('StateCode').val(ui.item.state);
      jQuery('GeocodedLatitude').val(ui.item.latitude);
      jQuery('GeocodedLongitude').val(ui.item.longitude);

 }

我在搜索时遇到了问题,外加4个邮政编码.我没有在响应中得到额外的4位数字. 例如,对于搜索文本"60 MAIN ST,HUNTINGTON NY 11743-6961". 我在响应中通过"postal_code"属性检索了"11743".但是我无法从响应中检索"6961".

I faced the problem during searching plus 4 zip codes. i didn't get additional 4 digits in the response. Example ,For the search text, "60 MAIN ST,HUNTINGTON NY 11743-6961". I retrieved the '11743' through 'postal_code' attribute in the response. But i cant able to retrieve '6961' from the response.

在这种情况下,是否有任何选项来获取完整的邮政编码?

Is there any options to get the full zip code in this scenario?

或者我需要尝试其他搜索方式以及4个邮政编码.

or I need to try for some other way to search plus 4 zip code.

谢谢.

推荐答案

据我所知,Google的地址解析器未附加邮政编码+4(附加)代码.

As far as I know, Google's geocoder doesn't append ZIP +4 (addon) codes.

Google不验证地址,这包括标准化其格式,而地址格式又包括附加ZIP + 4代码.

Google doesn't verify addresses, which includes standardizing its format, which in turn includes appending the ZIP+4 code.

您可能需要的是一个实际上可以标准化和验证地址的API.像 LiveAddress 这样的API可能会做您想要的事情.它将返回所有地址组成部分,包括+4附加代码.

What you probably need is an API that actually standardizes and validates addresses. An API like LiveAddress will probably do what you want. It will return all the address components, including the +4 addon code.

我是SmartyStreets的一名开发人员.这是一个针对您在问题中给出的地址的答复示例,其中使用了+4代码-在此交货点实际上有两个+4代码,因为其中一个是公司YMCA,我只显示这里的一个:

I'm a developer at SmartyStreets. Here's an example of a response for the address you give in your question, with the +4 code you use -- there are actually two +4 codes at this delivery point because one is the firm, the YMCA, and I'll just show the one here:

{
    "input_index": 0,
    "candidate_index": 1,
    "addressee": "Ymca",
    "delivery_line_1": "60 Main St",
    "last_line": "Huntington NY 11743-6961",
    "delivery_point_barcode": "117436961606",
    "components": {
        "primary_number": "60",
        "street_name": "Main",
        "street_suffix": "St",
        "city_name": "Huntington",
        "state_abbreviation": "NY",
        "zipcode": "11743",
        "plus4_code": "6961",
        "delivery_point": "60",
        "delivery_point_check_digit": "6"
    },
    "metadata": {
        "record_type": "F",
        "county_fips": "36103",
        "county_name": "Suffolk",
        "carrier_route": "C037",
        "congressional_district": "03",
        "rdi": "Commercial",
        "latitude": 40.87179,
        "longitude": -73.42655,
        "precision": "Zip7"
    },
    "analysis": {
        "dpv_match_code": "Y",
        "dpv_footnotes": "AABB",
        "dpv_cmra": "N",
        "dpv_vacant": "N",
        "active": "Y"
    }
}

以下是在SmartyStreets主页上进行查找的完整结果: http://smartystreets.com/?street=60%20Main%20St&city=Huntington&state=NY&zipcode=11743

Here's the lookup on the SmartyStreets homepage with full results: http://smartystreets.com/?street=60%20Main%20St&city=Huntington&state=NY&zipcode=11743

无论如何,这是地址验证和地址编码的区别.地理编码只是将地址转换为坐标,但是如果您需要+4代码,则需要地址验证API. LiveAddress是您可以使用的几种,但应该可以为您提供一个更好的主意,您可以在此处开始搜索.

Anyway, this is the difference been address validation and geocoding. Geocoding merely turns an address into coordinates, but if you need the +4 code, you need an address validation API. LiveAddress is one of a few you can use, but this should give you a better idea and you can start your search there.

这篇关于使用Google API,jQuery从地址解析中提取加4个邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆