notyou61
notyou61

Reputation: 49

Google API Address Components

I am attempting to obtain the address components using the Google Maps API however am unable to properly parse results. My code is as follows:

    // Ajax Call
$.ajax({
    url: 'queryPage.php?',
    data: 'varObtainGoogleAddress=y&' +
          'varAPILink=' + encodeURIComponent(varAPILink),
    dataType: 'json',
    success: function(data) {   
        // Formatted Address
        varFormattedAddress = data.result['formatted_address'];  // Works!
    }
});

What I would like is the city, state and postal code items. Any help in this regard is appreciated. I am a self taught amateur web developer. :)

Upvotes: 0

Views: 1739

Answers (3)

David Rearte
David Rearte

Reputation: 874

a little late but this is that i have

calle === street

nro === street_number

extraerCalle(resultado: IResult): string | undefined {
    const route = resultado.address_components.find(ac => !!ac.types.find(type => type === 'route'));
    return route && route.long_name;
  }

  extraerNro(resultado: IResult): string | undefined {
    const streetNumber = resultado.address_components.find(ac => !!ac.types.find(type => type === 'street_number'));
    return streetNumber && streetNumber.long_name;
  }

  extraerCalleNro(resultado: IResult): string {
    const calle = this.extraerCalle(resultado);
    const nro = this.extraerNro(resultado);
    return (calle || '') + (calle && nro ? ` ${nro}` : '');
  }

Upvotes: 0

Emmanuel Delay
Emmanuel Delay

Reputation: 3679

I wrote a function.

/**
*   geocodeResponse is an object full of address data.  
*   This function will "fish" for the right value
*   
*   example: type = 'postal_code' => 
*   geocodeResponse.address_components[5].types[1] = 'postal_code'
*   geocodeResponse.address_components[5].long_name = '1000'
* 
*   type = 'route' => 
*   geocodeResponse.address_components[1].types[1] = 'route'
*   geocodeResponse.address_components[1].long_name = 'Wetstraat'
*/
function addresComponent(type, geocodeResponse, shortName) {
  for(var i=0; i < geocodeResponse.address_components.length; i++) {
    for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) {
      if (geocodeResponse.address_components[i].types[j] == type) {
        if (shortName) {
          return geocodeResponse.address_components[i].short_name;
        }
        else {
          return geocodeResponse.address_components[i].long_name;
        }
      }
    }
  }
  return '';
}

example of how to use:

...
myGeocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK && results[1]) {
    var country = addresComponent('country', results[1], true);
    var postal_code = addresComponent('postal_code', results[1], true);
  }
});
...

Upvotes: 2

chrki
chrki

Reputation: 6323

I'm assuming what you are doing is a reverse geocode.

The object returned looks something like this (from the example):

{
  "address_components": [
    {
      "long_name": "Grand St/Bedford Av",
      "short_name": "Grand St/Bedford Av",
      "types": [
        "bus_station",
        "transit_station",
        "establishment"
      ]
    },
    {
      "long_name": "Williamsburg",
      "short_name": "Williamsburg",
      "types": [
        "neighborhood",
        "political"
      ]
    },
    {
      "long_name": "Brooklyn",
      "short_name": "Brooklyn",
      "types": [
        "sublocality_level_1",
        "sublocality",
        "political"
      ]
    },
...

There is no direct way to access a result's street, house number etc. because each part of the address can be several types at the same time. You would have to iterate over the address_components and filter according to what you need.

Here's another Stackoverflow thread covering this topic: get city from geocoder results?

Upvotes: 0

Related Questions