0

Let's say I have the following code:

var locations = [];

$.each(data.articles_data, function (index, article) {
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({ 'address': article.location}, function(results, status) {
        if (status == 'OK') {
            var lat = results[0].geometry.location.lat();
            var lng = results[0].geometry.location.lng();

            locations.push(lat + ' - ' + lng);
        } else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
});

if(locations.length > 0) {
    // Unfortunately code never get to this point cause locations array is empty
}

As you can see, my locations array initially declared outside of the $.each is still empty even if I have added data to it inside the callback function of geocoder.geocode.

Is there a way that I can get this to work? I really need populate my locations inside that callback and access it outside.

Thanks in advance for any help

user765368
  • 19,590
  • 27
  • 96
  • 167

0 Answers0