1

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>
1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Folks, I am quite confused. I need to add some markers on this map, and it it required to get into for loop and ack additional locations for other cities. How can I get them with callback? Sorry,I understand passing objects and variables, but never encountered calback concept before $ var position = new google.maps.LatLng(locs.lat(), locs.lng()); var marker = new google.maps.Marker({ position: position, map: map }); marker.setTitle("Chicago, IL");
To set N markers you need N requests to geocode(), so there's your loop. The geocode() function already accepts a callback (note the function() you pass as an argument?). Just use this callback to set your markers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.