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