Get a address_components

google-api, google-maps, google-places-api

Solution

Made this function for a project. It parses

- street

- number

- country

- zip

- city

from a google georesponse

function parseGoogleResponse(components) {
    _.each(components, function(component) {
      _.each(component.types, function(type) {
        if (type === 'route') {
          $("input[name=street]").val(component.long_name)
        }
        if (type === 'street_number') {
          $("input[name=nr]").val(component.long_name)
        }
        if (type === 'locality') {
          $("input[name=city]").val(component.long_name)
        }
        if (type === 'country') {
          $("input[name=country]").val(component.long_name)
        }
        if (type === 'postal_code') {
          $("input[name=zip]").val(component.long_name)
        }
      })
    })
  }

Problem

I'm using google places API with geocoding. I have a problem with address components type. I want to get information about address which user typed in autocomplete in this format: Street number/ street / City/ Province/ Country. If user autocompleted "Street 12, SomeCity, SomeProvince, SomeCountry", I want to return in alert all of this information. But when user type only "someProvince, SomeCountry", I want to have only province and country address type. Here is my code: ``` google.maps.event.addListener(autocomplete, 'place_changed', function () { var place = autocomplete.getPlace(); alert('0: ' + place.address_components[0].long_name); alert('1: ' + place.address_components[1].long_name); alert('2: ' + place.address_components[2].long_name); alert('3: ' + place.address_components[3].long_name); alert('4: ' + place.address_components[4].long_name); alert('5: ' + place.address_components[5].long_name); alert('6: ' + place.address_components[6].long_name); alert('7: ' + place.address_components[7].long_name); )}; ``` Problem is that when user autocomplete full address, it show properly all of this alerts. But when autocomplete only part of information - only country - it will show 7 times what country is typed. http://gmaps-samples-v3.googlecode.com/svn/trunk/places/autocomplete-addressform.html I want to have, that when street and city is not given, it will show alert ("street is null" etc). How to do it?

Original source