1

In this code:

$(document).ready(function() {
        var lat = 0;
        var lng = 0;
        function getLatLng(address) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    lat = results[0].geometry.location.lat(); //how do I access lat 
                    lng = results[0].geometry.location.lng() //and lng outside function ormake it global
                }
            });
            alert(lat); // does not display only show's the 0
        }
        getLatLng();
    });

I want the alert(lat) to show the lat not zero.

how can I access this?

thanks in advance!

Chirag Vidani
  • 2,519
  • 18
  • 26
sandip
  • 3,279
  • 5
  • 31
  • 54
  • 2
    The alert is not inside the callback function, so it's actually running that *before* the geocode call is complete. Move it up 1 line. – Reinstate Monica Cellio Jan 27 '14 at 09:56
  • 2
    You are already accessing `lat` just fine. The problem is that the `geocode` call runs asynchronously and has not yet completed when the `alert` is executed. You cannot work around that with brute force: it's impossible to print a result before you have received it. – Jon Jan 27 '14 at 09:57
  • geocoder.geocode() is an asynch process so you need to alert in callback function. – Sohil Desai Jan 27 '14 at 09:58
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Salman A Jan 27 '14 at 09:59

3 Answers3

4

Consider using callback function when you work with asynchronous operations:

function getLatLng(address, callback) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            lat = results[0].geometry.location.lat(); //how do I access lat 
            lng = results[0].geometry.location.lng() //and lng outside function ormake it global
            callback(lat, lng);
        }
    });
}

getLatLng(function(lat, lng) {
    alert([lat, lng]);
});
dfsq
  • 191,768
  • 25
  • 236
  • 258
1

Your alert prints 0 because it is run before geocoder finishes its job. You can use callback to be notified when geocoder finishes:

$(document).ready(function() {
    var lat = 0;
    var lng = 0;
    var geocoderFinished = function(){
        alert(lat);
    };
    function getLatLng(address) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                lat = results[0].geometry.location.lat(); //how do I access lat 
                lng = results[0].geometry.location.lng() //and lng outside function ormake it global
                geocoderFinished();
            }
        });
    }
    getLatLng();
});
hsz
  • 148,279
  • 62
  • 259
  • 315
0

It is because alert(lat); is getting executed before the success function. You should be alerting it either inside callback function or use setTimeout and give the alert.

it is bad practise to use setTimeout as we never know how long it will take to execute server side call. So its better practise to call the code in callback function to ensure the changes

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125