Andrew
Andrew

Reputation: 7768

JavaScript getting variable from one function to another

I am having issues passing two coordinates from one function to another. I don't really know JavaScript, but it seems to be somewhat correct. Could you please let me know where is my error?

        <script type="text/javascript"> 

        function initialize() {
            var address = "Chicago IL";
            locs= getLatLong(address);

            alert(locs.lat()); //does not work
            var myOptions = {
                zoom: 4,
                center: new google.maps.LatLng(41, -87),
                mapTypeId: google.maps.MapTypeId.ROADMAP          
            };

            var map = new google.maps.Map(document.getElementById('map_canvas'),
            myOptions); 
        }

        function getLatLong(address){
            var geo = new google.maps.Geocoder;
            geo.geocode({'address':address},function(results, status){
                if (status == google.maps.GeocoderStatus.OK) {

                    locations[0] = results[0].geometry.location.lat(); 
                    locations[1] = results[0].geometry.location.lng();                  

                    //alert(locations[0]); //test is good- works! Need to pass it back to function
                    //alert(locations[1]); //test is good- works! Need to pass it back to function
                    locs =results[0].geometry.location; 
                    return locs;                
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
    </script>

Upvotes: 1

Views: 1414

Answers (1)

Tomalak
Tomalak

Reputation: 338128

It is not possible to return a value from an asynchronous function. Do not try it.

Use a callback instead.

function setLocationOnMap(locs) {
    alert(locs.lat()); // works now!
    var myOptions = {
        zoom: 4,
        center: new google.maps.LatLng(41, -87),
        mapTypeId: google.maps.MapTypeId.ROADMAP          
    };
    var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); 
}

function initialize() {
    var address = "Chicago IL";
    getLatLong(address, setLocationOnMap);
}

function getLatLong(address, callback){
    var geo = new google.maps.Geocoder;
    geo.geocode({'address':address},function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
            // processing...
            locs = results[0].geometry.location; 

            callback(locs);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

You must understand that asynchronous function calls like geo.geocode() return immediately, i.e. before any result is ready. That's why you can't use return to return a value from them - they don't have it yet.

Once the result (most often, an HTTP request) is ready, the asynchronous function relies on a callback function to handle it. You must do all your further processing in that callback function, either directly or by making another function call.

Upvotes: 2

Related Questions