1

I have viewed a lot of other questions relating to this, but found no direct answer.

Similar to all other problems, I have 4 elements in the array, however array.length returns 0. Below is my code:

for (count = 0; count < noPax; count++) {
    var latStr = "lat" + count;
    var longStr = "long" + count;
    // var latitude = document.getElementById(latStr).value;
    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById(latStr).value;
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            locations.push(latitude);
            locations.push(longitude);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
            window.location.reload();
        }
    });
    // var longitude = document.getElementById(longStr).value;
}

var bound = new google.maps.LatLngBounds();
console.log(locations);
console.log(locations.length);
for (i = 0; i < locations.length; i += 2) {
    bound.extend( new google.maps.LatLng(locations[i], locations[i+1]) );
}

Here is the console log:

Image

Appreciate the help!

guradio
  • 15,524
  • 4
  • 36
  • 57
  • The `.geocode()` function is **asynchronous**. – Pointy Jan 26 '17 at 14:55
  • The *A* in Ajax – Dave Newton Jan 26 '17 at 14:56
  • The issue you're having is a combination of the linked duplicate targets (which are the primary problem) in combination with this weird console behavior (which is why you *seem* to get a zero-length array with entries in it): http://stackoverflow.com/questions/38660832/element-children-has-elements-but-returns-empty-htmlcollection – T.J. Crowder Jan 26 '17 at 14:57

0 Answers0