Getting the Full Country Name

1,660 views
Skip to first unread message

Randy Johnson

unread,
Oct 27, 2009, 11:41:36 PM10/27/09
to Google Maps JavaScript API v3
Hello,

I been working with a the reverse geocoding. I cannot seem to figure
out how to reliably get the country name from the reults. any ideas?

Thanks!

Randy

Mike Williams

unread,
Oct 28, 2009, 3:13:31 AM10/28/09
to google-map...@googlegroups.com
I seem to remember that for some places you only get the country name
code, and not the country name, but I can't seem to find any examples at
present. Everywhere I try at the moment seems to return both.

Anyway, if you are finding that some places return the code but not the
country name, you could build your own lookup table for ISO 3166-1
Alpha-2 codes. There's a list here:

http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

--
Mike Williams
Gentleman of Leisure

Randy Johnson

unread,
Oct 28, 2009, 8:47:18 AM10/28/09
to Google Maps JavaScript API v3
I cannot seem to figure out how to reliably get the country code
either.

This will get me the address-components, but I cannot see how to grab
just the country name or country code.

console.log("ac: " + results[1].address_components);

Randy

Esa

unread,
Oct 28, 2009, 3:11:07 PM10/28/09
to Google Maps JavaScript API v3
I have noticed that reverse geocoding results of type

"natural_feature"
"transit_station"
"point_of_interest"

do not contain any country data. Sometimes reverse geocoding returns
one of those as the first result. However there usually are more
detailed results in the result set.

Try rightclicking (plus Search) e.g. a lake on this page:
http://koti.mbnet.fi/ojalesa/geocode/prettygeocode_v3.htm

pi5701

unread,
Nov 3, 2009, 4:54:36 AM11/3/09
to Google Maps JavaScript API v3
same prob here
wanna sort geocoder output like in v2:
- CountryName
- AdministrativeAreaName
- SubAdministrativeAreaName
- LocalityName
- ThoroughfareName

for now i only got to work:
result1 = results[i].address_components[5].long_name;
result2 = ", " + results[i].address_components[4].long_name;
result3 = ", " + results[i].address_components[3].long_name;
result4 = ", " + results[i].address_components[2].long_name;
result5 = ", " + results[i].address_components[1].long_name;
result6 = ", " + results[i].address_components[0].long_name;

any solution?

pi5701

unread,
Nov 3, 2009, 4:56:06 AM11/3/09
to Google Maps JavaScript API v3
live example here:
just click around

http://www.mario-baldauf.org/tlmf/API_v3/index.html

Esa

unread,
Nov 3, 2009, 11:16:58 AM11/3/09
to Google Maps JavaScript API v3
First you have to choose the best result out of the result set.
Sometimes the most detailed result does not have a type at all:

"types": []

I gave up sorting by type. In reverse geocoding, the best result is
the one with the highest number of address_components.

results.sort(function(a,b){
return(b.address_components.length - a.address_components.length)
});

Now you have the best result as results[0]. Still it is tricky to
extract the details.

Esa

unread,
Nov 3, 2009, 1:44:18 PM11/3/09
to Google Maps JavaScript API v3
I wrote a function that creates a result object with easier-to-use
properties.

function reformat(result){
var me = {};
var components = result.address_components;
var len = components.length;
for(var i=0; i<len; i++){
for(var ii=0; ii<components[i].types.length; ii++){
me[components[i].types[ii]] = components[i].short_name;
me[components[i].types[ii]+"_long"] = components[i].long_name;
}
}
for(var prop in result){
if(prop != "address_components") me[prop] = result[prop];
}
return me;
}


It sniffs the address_components by types[] and names its properties
by those types names. E.g.

{"long_name": "Finland",
"short_name": "FI",
"types": ["country"]},

in the original response is reformatted to following:

{"country": "FI",
"country_long": "Finland"}

It does not alter other properties but address_components. One result
is taken as the parameter. I still recommend sorting the results by
the number of address_components and picking the first one.

See it in action
http://koti.mbnet.fi/ojalesa/geocode/sortgeocode_v3.htm
Reply all
Reply to author
Forward
0 new messages