Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a address_components

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?

like image 898
whoah Avatar asked Nov 25 '12 13:11

whoah


People also ask

How can I get Google Lat Long ID?

getPosition(). lat(); longitude=marker. getPosition(). lng();

Is Google address lookup API free?

Even though the Google Maps API is free, it does not actually validate addresses, AND it comes with some pretty restrictive terms of service. So, if real address validation is what you need, you may want to read our review on how to choose an address validation provider.

How can I get Google Map API key?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key. Click Close.


2 Answers

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)
        }
      })
    })
  }
like image 76
Anthony De Meulemeester Avatar answered Oct 14 '22 21:10

Anthony De Meulemeester


Address Types and Address Component Types

Use the types and map them to your address fields. Keep in mind that cities, counties, states etc. can have different meanings depending on their context. For example, North Hollywood comes up with type neighborhood since it is located in Los Angeles('locality').

function placeToAddress(place){
        var address = {};
        place.address_components.forEach(function(c) {
            switch(c.types[0]){
                case 'street_number':
                    address.StreetNumber = c;
                    break;
                case 'route':
                    address.StreetName = c;
                    break;
                case 'neighborhood': case 'locality':    // North Hollywood or Los Angeles?
                    address.City = c;
                    break;
                case 'administrative_area_level_1':     //  Note some countries don't have states
                    address.State = c;
                    break;
                case 'postal_code':
                    address.Zip = c;
                    break;
                case 'country':
                    address.Country = c;
                    break;
                /*
                *   . . . 
                */
            }
        });

        return address;
    }
like image 29
Ron Avatar answered Oct 14 '22 22:10

Ron