Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the street address from a google maps geocode call

I am geocoding an address using google maps API, and I need to get the street address, city, state, and zip in distinct fields. Based on the documentation of the address component types that are returned in the result, this is my code:

var address = "";
var city = "";
var state = "";
var zip = "";

geocoder.geocode( { 'address': inputAddress}, function(results, status){
    if (status==google.maps.GeocoderStatus.OK){
        // loop through to get address, city, state, zip
        $.each(results[0].address_components, function(){
            switch(this.types[0]){
                case "postal_code":
                    zip = this.short_name;
                    break;
                case "street_address":
                    address = this.short_name;
                    break;
                case "administrative_area_level_1":
                    state = this.short_name;
                    break;
                case "locality":
                    city = this.short_name;
                    break;                  
            }
        });
    } else{ alert("Invalid Address"); }
});

However, it seems that when I enter addresses, "street_address" is not being returned; instead, it is returned as separate fields, most often "street_number" and "route", occasionally additional fields (such as "subpremise" for apt number). See this example of a geocoding result in the docs.

How can I get an address variable that holds any fields related to the street address? For the example in the docs, I would want "1600 Ampitheatre Parkway".

like image 627
froadie Avatar asked Dec 04 '13 09:12

froadie


Video Answer


2 Answers

Based on the docs street_address is an address type, but that does not necessarily mean it's a type of address component - it could just be a tag used for the types array - returned with each of the objects in the results array:

The types[] array within the returned result indicates the address type. These types may also be returned within address_components[] arrays to indicate the type of the particular address component.

What you probably should be doing is traversing through your results array first and find the one that includes a street_address in the types array. Then use that object to get the formatted_address attribute (as mentioned by Aperçu). As the doc states: "the formatted_address is a string containing the human-readable address of this location". When the type is street_address, the formatted_address is equal to a street_number + route (followed by whatever other address_components are available). So you can either split(',') the formatted_address string to get the components you need - or use the address_components array of that particular object in the results array to grab the components that you need (of course doing it this way you'll need to concat the street_number and route yourself - so added logic is needed).

like image 107
Suvi Vignarajah Avatar answered Oct 18 '22 03:10

Suvi Vignarajah


If you want to get all address fields in a string, use the 'formatted_address' property:

Code:

var geocoder = new google.maps.Geocoder();

var fullAdressData = '';

geocoder.geocode( { 'address': inputAddress}, function(results, status){
    if (status==google.maps.GeocoderStatus.OK){

        fullAdressData = results[0].formatted_address;

    } else{ alert("Invalid Address"); }
});
like image 22
Jamilson Junior Avatar answered Oct 18 '22 03:10

Jamilson Junior