I've been working a small javascript project that take coordinates stored in a xml/kml file and displays them in a custom Google Maps application. I have everything working the way I want except for the reverse geocoding. I'm able to display the formatted address when I run the code inside the callback function but I can't find a way to store the address for later use (for dynamic events such as display the full address when a user clicks on the marker).
Is there any way to store the address inside the callback function and then get it later? Whenever I try, it says that address is undefined. See below:
var address;
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
            address = results[1].formatted_address;
            alert(address); // this displays correctly
        }
        else {
            alert("No results found");
        }
    }
    else {
        alert("Geocoder failed due to: " + status);}
    }
);
alert(address); // this displays 'undefined'
 
    