Right The function you pass getLocations () will not be called until the data is available, so returning the "country" before setting it will not help you.
The way you need to do this is to execute the function that you pass to geocoder.getLocations (), in fact to do what you wanted to do with the return values.
Something like that:
function reverseGeocode(latitude,longitude){ var geocoder = new GClientGeocoder(); var latlng = new GLatLng(latitude, longitude); geocoder.getLocations(latlng, function(addresses) { var address = addresses.Placemark[0].address; var country = addresses.Placemark[0].AddressDetails.Country.CountryName; var countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode; var locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName; do_something_with_address(address, country, countrycode, locality); }); } function do_something_with_address(address, country, countrycode, locality) { if (country==="USA") { alert("USA A-OK!");
If you want to do something different every time you get the location, then pass the function as an optional reverseGeocode parameter:
function reverseGeocode(latitude,longitude, callback){
If this looks a little dirty, you can take a look at something like the “Snooze” function in Dojo, which makes the chain between the functions a little clearer.
Mark Bessey Apr 10 '10 at 3:10 2010-04-10 03:10
source share