hi i am having the same probem as here Return address from Google Maps Geocoder v3 , i dont understand how to set my global variable to the return vlaue
i got this at the moment where b returns undefined
var a;
var b;
function outputGeo(result){
b = result;
}
geocoder.geocode({'latLng': event.latLng},function(results, status) {
a = results[1].formatted_address;
outputGeo(a);
});
alert(b);
geocoder.geocodeis an asynchronous function because it’s performing a JSONP request. In other words, before the results have come back, thealert(b)line is already being called. You need to put thatalertline into the callback function afteroutputGeo(a).Javascript’s a funny language. This async thing bit me until I researched JSONP and the synchrony of Javascript. I suggest you do the same. A great way to learn about this is to write a JSONP-request wrapper of your own. (Just a thought.)