I'm geocoding an address with Google Maps API and I need to get the country name by a given address. This is my code:
var address = "<?php echo $address;?>";
var raw;
function initialize(){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"address": address
},function(results){
raw = results[0].address_ponents;
console.log(raw);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
Console returns an array with data and I want to get the country as seen on the image below:
Here's what the console returns
How can I achieve that? I've tried with:
raw = results[0].address_ponents.types["country"];
raw = results[0].address_ponents.types;
raw = results[0].address_ponents.country;
raw = results[0].address_ponents.types.long_name;
But all that returns either "undefined" or nothing. I just want to get "Argentina" and store it in a variable.
I'm geocoding an address with Google Maps API and I need to get the country name by a given address. This is my code:
var address = "<?php echo $address;?>";
var raw;
function initialize(){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"address": address
},function(results){
raw = results[0].address_ponents;
console.log(raw);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
Console returns an array with data and I want to get the country as seen on the image below:
Here's what the console returns
How can I achieve that? I've tried with:
raw = results[0].address_ponents.types["country"];
raw = results[0].address_ponents.types;
raw = results[0].address_ponents.country;
raw = results[0].address_ponents.types.long_name;
But all that returns either "undefined" or nothing. I just want to get "Argentina" and store it in a variable.
Share Improve this question asked Dec 20, 2015 at 0:05 Camiloo ThsCamiloo Ths 31 silver badge2 bronze badges2 Answers
Reset to default 3Since the array of objects is dynamic you'll have to iterate through it:
var raw;
var address = "1 Infinite Loop, Cupertino, CA"
function initialize(){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"address": address
},function(results){
raw = results;
//find country name
for (var i=0; i < results[0].address_ponents.length; i++) {
for (var j=0; j < results[0].address_ponents[i].types.length; j++) {
if (results[0].address_ponents[i].types[j] == "country") {
country = results[0].address_ponents[i];
console.log(country.long_name)
console.log(country.short_name)
}
}
}
});
}
initialize();
Using ES6 you can write in this way:
const geocoder = new google.maps.Geocoder();
const address = "1 Infinite Loop, Cupertino, CA";
geocoder.geocode({
"address": address
}, (raw) => {
const cities = [...new Set(raw.map(c => c.address_ponents.filter(
a => {return a.types.includes("country");}
)[0].long_name))];
console.log(cities);
});
Explanation:
- raw - object returned by
geocoder
. It is array. So we acting on this bymap
. - in map we filtering by only these ponents of address that contains types including country.
- next we convert this result to
Set
to make in unique - finally we spreading it by
...
operator obtaining simple array of strings.