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".