Javascript Array.length返回0

时间:2017-01-26 14:54:51

标签: javascript jquery arrays

我已经查看了很多与此相关的其他问题,但没有找到直接答案。

与所有其他问题类似,我在数组中有4个元素,但是array.length返回0.下面是我的代码:

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]) );
}

这是控制台日志:

Image

感谢帮助!

0 个答案:

没有答案