我正在尝试对一组位置进行地理编码并将结果放入一个数组中,但我似乎无法让它正常工作。是否可以从结果函数将数据推送到我的数组中?
编辑 - 我能够通过使用信息调用的单独函数对每个位置进行地理编码,但我不确定数组是否填充正确
这是代码
var data = [];
geocoder = new google.maps.Geocoder();
function pushData(latlon,h1,d1){
alert(latlon + h1 + d1); //this is popping up with the correct info
data.push({
loc: latlon,
h: h1,
d: d1
});
}
cityArray = ['Chicago', 'New York', 'Dallas']
for (var i = 0; i < cityArray.length; i++) {
//document.write(cityArray[i]);
geocoder.geocode( { 'address': cityArray[i] }, function(results, status) { //use latlang to enter city instead of coordinates
if (status == google.maps.GeocoderStatus.OK) {
var latlon = results[0].geometry.location;
var h = 'test';
var d = 'test';
pushData(latlon,h,d);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}