How to acces to data from JSON? help with GEOCODER API.

1,294 views
Skip to first unread message

edgardo.garza

unread,
Mar 31, 2011, 9:35:01 PM3/31/11
to Google Maps JavaScript API v3
hello, it´s my first message, and i really need help i just dont know
how to acces data from the JSON archive that generates the geocoder
API for example this:

{
"status": "OK",
"results": [ {
"types": [ "locality", "political" ],
"formatted_address": "Washington D. C., Distrito de Columbia,
EEUU",
"address_components": [ {
"long_name": "Washington D. C.",
"short_name": "Washington D. C.",
"types": [ "locality", "political" ]
}, {
"long_name": "Distrito de Columbia",
"short_name": "DC",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "Estados Unidos",
"short_name": "US",
"types": [ "country", "political" ]
} ],


ok, i can acces using this:

document.getElementById('name').value =
results[0].address_components[3].long_name;

and it shows, "Estados Unidos".

but I want to access it this way:

results[0].address_components.long_name.types.country.political

i know this is incorrect, but i wanna acces to the data without using
a number in "address_components[x]"


thanks.

BTW sorry for my english



edgardo.garza

unread,
Apr 1, 2011, 2:06:49 PM4/1/11
to Google Maps JavaScript API v3
anyone?

Vaishakh Thayyil

unread,
Apr 1, 2011, 1:26:32 PM4/1/11
to Google Maps JavaScript API v3
Hi ,
You could use the JSON parser available in jquery & get the data in an
array format .
You can refer http://googlelatiude.blogspot.com/2011/03/google-latitude-mashup-with-google-maps.html
which has a good example of parsing the json data .I am sorry but I
didnt get what you mentioned about
numbers .

thanks







On Apr 1, 6:35 am, "edgardo.garza" <edgardo.edel...@hotmail.com>
wrote:

Michael Geary

unread,
Apr 1, 2011, 2:28:16 PM4/1/11
to google-map...@googlegroups.com, edgardo.garza
Your English is fine, no worries there. :-)

But I'm not sure exactly what you want to do.

This can't work:

results[0].address_components.long_name.types.country.political

because address_components is an array. The array does not have a property called long_name. Each *element* of the array has a long_name property. You get to the individual elements with the [n] notation.

[n] doesn't have to be a number constant. It can be a variable containing a number.

Do you want to access each of the array elements, one by one? Then you can write a loop to do that.

But before I speculate and give code samples that may not be relevant, how about describing in more detail what you want to do with the address_components array?

-Mike



--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


JFrancis

unread,
Apr 1, 2011, 2:37:26 PM4/1/11
to Google Maps JavaScript API v3
In the version 2 API, you could get to those individual elements this
way:

var place = response.Placemark[0];
var point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);

myAddress =
place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName

myCity =
place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName

myState =
place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName

myZipCode =
place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber


Sadly, I have not seen an equivalent method of retrieving that
information in the v3 API. The only solution I have seen which does
not require yet ANOTHER Javascript library was posted here:

http://stackoverflow.com/questions/5341023/retrieving-postal-code-with-google-maps-javascript-api-v3-reverse-geocode/5344337

Not a very elegant solution to something which should have been
carried forward.

-- JF

JKurtock

unread,
Apr 1, 2011, 3:15:46 PM4/1/11
to Google Maps JavaScript API v3
To access the address result with "types" "country" requires that you
step through the array of address components. Why? You cannot tell
in advance which address components are available. (Though "country"
is almost always available, you still don't know where it will be in
the array.)

This is not difficult:
for (var componentsIndex in results[0].address_components) {
if (results[0].address_components[componentsIndex].types[0] ==
'country' ) {
alert('Country name: ' +
results[0].address_components[componentsIndex].long_name);
}
}

[not tested, but believed to have been typed correctly]

- jeff

On Apr 1, 11:37 am, JFrancis <jfran...@mge.com> wrote:
> In the version 2 API, you could get to those individual elements this
> way:
>
> var place = response.Placemark[0];
> var point = new GLatLng(place.Point.coordinates[1],
> place.Point.coordinates[0]);
>
> myAddress =
> place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Local ity.Thoroughfare.ThoroughfareName
>
> myCity =
> place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Local ity.LocalityName
>
> myState =
> place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName
>
> myZipCode =
> place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Local ity.PostalCode.PostalCodeNumber
>
> Sadly, I have not seen an equivalent method of retrieving that
> information in the v3 API. The only solution I have seen which does
> not require yet ANOTHER Javascript library was posted here:
>
> http://stackoverflow.com/questions/5341023/retrieving-postal-code-wit...
Reply all
Reply to author
Forward
0 new messages