I’m working with the Google Maps Geocoder. I’ve got everything working fine, but I can’t seem to figure out how to ‘traverse’ (parse?) the JSON results.
How can I get the postal code from the Geocoder’s JSON results?
My attempt as been to loop through ‘address_components’, testing each “values” key for an array containing “postal_code”.
So here’s a snippet of what I’ve written so far:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address : cAddress }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
var fAddress = results[0].formatted_address;
var contactLatLng = results[0].geometry.location;
var postalCode = $.each(results[0].address_components,
function(componentIndex, componentValue) {
var typesArray = componentValue.types;
if ($.inArray("postal_code", typesArray)) {
return componentValue.long_name;
}
})
}
}
});
The problem specifically is postalCode is
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object]`
Obviously, there is something I’m missing.
For reference, here is the link to Google Maps Geocoder JSON results:
http://code.google.com/apis/maps/documentation/geocoding/#JSON
Thanks for any help!
~Amos
Also note, “return” doesn’t work. It’s a asynchronous function. So by the time your function runs, the parent function has finished.
So your function must do something explicitly with the result. for example…