我已经查看了很多与此相关的其他问题,但没有找到直接答案。
与所有其他问题类似,我在数组中有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]) );
}
这是控制台日志:
感谢帮助!