Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting plus 4 zip code from geocoding using google API, jQuery

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.

like image 912
Ramasamy Avatar asked Mar 13 '13 13:03

Ramasamy


1 Answers

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.

like image 153
Matt Avatar answered Oct 01 '22 00:10

Matt