Getting street address using geocoding google maps

I am geocoding the address using google map APIs and I need to get street address, city, state and zip code in different fields. Based on the documentation of the types of address components that are returned as a 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 entered the addresses, "street_address" does not return; instead, it is returned as separate fields, most often "street_number" and "route", sometimes additional fields (for example, "subpremise" for apt number). See this sample geocoding result in documents.

How can I get an address variable that contains any fields associated with a street address? For example, in the docs I would like the 1600 Ampitheater Parkway.

+7
parsing google-maps-api-3 geocoding
source share
3 answers

Based on the street_address documents, the address type is specified, but this does not necessarily mean that it is the type of the address component - it can only be a tag used for the types array, returned with each object in the results array:

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

What you probably should do is first go through your results array and find the one that contains street_address in the types array. Then use this object to get the formatted_address attribute ( as indicated by Aperçu ). Like the doc states : " formatted_address is a string containing the human-readable address of this place." When type street_address , formatted_address is equal to a street_number + route (followed by any other address_components ). Thus, you can either split(',') formatted_address line formatted_address get the components you need, or use the address_components array of this particular object in the results array to capture the components you need (of course, this is done so you will need to combine street_number and route yourself, therefore, added logic is required).

+1
source share

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

The 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"); } }); 
+1
source share

use "route" instead of "street_address" ...

 case "route": address = this.short_name; alert(address); break; 
0
source share

All Articles