5

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);

 }

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?

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

Thanks in advance.

Matt
  • 22,721
  • 17
  • 71
  • 112
Ramasamy
  • 111
  • 1
  • 2
  • 9

2 Answers2

5

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

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

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.

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"
    }
}

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

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Matt
  • 22,721
  • 17
  • 71
  • 112
1

TLDR

Use google place API's details request. If found, the +4 zip code will be returned as an address component of type "postal_code_suffix".

Long Version

Google place API documentation is not particularly helpful in explaining what address component type to use. Searching for the given address without the +4 code via google place API:

https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=60 MAIN ST,HUNTINGTON NY 11743&inputtype=textquery&fields=place_id&key=<KEY>

Gives place ID ChIJYWl9Uism6IkRtW81pi9SvgM.

I can then use a place details request:

https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJYWl9Uism6IkRtW81pi9SvgM&key=<KEY>

This shows a full postcode in the adr_address property: ...<span class=\"postal-code\">11743-8003</span>.... Note that the +4 zip code has likely changed since this SO question was posted (yes, +4 zip codes can change frequently!). This info is more conveniently returned as an address component with type postal_code_suffix in the response:

...
results: {
  ...
  address_components: [
    ...
    { long_name: "8003"
    , short_name: "8003"
    , types: ["postal_code_suffix"]
    }
    ...
  ]
  ...
}
...
Dalias
  • 11
  • 1