Javascript 如何在回调函数中为正确的对象添加值?
每次while循环运行时,下面的代码都会覆盖var存储。因此,当回调运行时,存储值通常是存储数组中的最后一项Javascript 如何在回调函数中为正确的对象添加值?,javascript,google-maps,asynchronous,Javascript,Google Maps,Asynchronous,每次while循环运行时,下面的代码都会覆盖var存储。因此,当回调运行时,存储值通常是存储数组中的最后一项 var stores = []; stores.push({'address': "1 California Ave, San Francisco, CA"}); stores.push({'address': "16 California Ave, San Francisco, CA"}); stores.push({'address': "20 California Ave, San
var stores = [];
stores.push({'address': "1 California Ave, San Francisco, CA"});
stores.push({'address': "16 California Ave, San Francisco, CA"});
stores.push({'address': "20 California Ave, San Francisco, CA"});
stores.push({'address': "30 California Ave, San Francisco, CA"});
var geocoder = new google.maps.Geocoder();
var i = 0;
while( i < stores.length){
var store = stores[i];
geocoder.geocode({'address':store.fullAddress}, function(data, status){
store.latlng = data[0].geometry.location;
}
});
i++;
};
var存储=[];
商店。推({地址):“1加利福尼亚大道,旧金山,CA”});
商店。推({地址):“16加利福尼亚大道,旧金山,CA”});
商店。推({地址):“20加利福尼亚大道,旧金山,CA”});
商店。推({地址):“30加利福尼亚大道,旧金山,CA”});
var geocoder=new google.maps.geocoder();
var i=0;
while(i
如何将正确的板条添加到正确的门店
这也不起作用,因为var i也发生了变化
var i = 0;
var storesObject = {}
while( i < stores.length){
var store = stores[i];
storesObject[i] = stores[i];
geocoder.geocode({'address':store.fullAddress}, function(data, status){
storesObject[i].latlng = data[0].geometry.location;
}
});
i++;
};
var i=0;
var storesObject={}
while(i
如果这样做,如何将结果与存储阵列匹配
var i = 0;
var results = [];
while( i < stores.length){
var store = stores[i];
geocoder.geocode({'address':store.fullAddress}, function(data, status){
results.push(data[0].geometry.location);
}
});
i++;
};
var i=0;
var结果=[];
while(i
更新添加功能范围修复了我的问题:
var i = 0;
while( i < stores.length){
(function(){
var store = stores[i];
geocoder.geocode({'address':store.fullAddress}, function(data, status){
store.latlng = data[0].geometry.location;
}
});
})();
i++;
};
var i=0;
while(i
见:
功能地理编码地址(存储){
geocoder.geocode({'address':stores[store].address},函数(数据,状态){
if(status==google.maps.GeocoderStatus.OK){
存储[store].latlng=data[0].geometry.location;
}否则{
警报(“地理代码失败:+状态”);
}
});
}
var存储=[];
商店。推({地址):“1加利福尼亚大道,旧金山,CA”});
商店。推({地址):“16加利福尼亚大道,旧金山,CA”});
商店。推({地址):“20加利福尼亚大道,旧金山,CA”});
商店。推({地址):“30加利福尼亚大道,旧金山,CA”});
var geocoder=new google.maps.geocoder();
var i=0;
while(i
功能地理编码地址(存储){
geocoder.geocode({'address':stores[store].address},函数(数据,状态){
if(status==google.maps.GeocoderStatus.OK){
存储[store].latlng=data[0].geometry.location;
}否则{
警报(“地理代码失败:+状态”);
}
});
}
var存储=[];
商店。推({地址):“1加利福尼亚大道,旧金山,CA”});
商店。推({地址):“16加利福尼亚大道,旧金山,CA”});
商店。推({地址):“20加利福尼亚大道,旧金山,CA”});
商店。推({地址):“30加利福尼亚大道,旧金山,CA”});
var geocoder=new google.maps.geocoder();
var i=0;
while(i
加入功能范围回答了我的问题。我应该结束这个问题吗?它可能最终会作为一个副本结束。我会留下它,因为人们仍然能够找到它,而且你的标题可能会被一些搜索所捕获。加入功能范围回答了我的问题。我应该结束这个问题吗?它可能最终会作为一个副本结束。我会留下它,因为人们仍然能够找到它,而且你的标题可能会被一些搜索所捕获。
function geocodeAddress(store) {
geocoder.geocode({'address':stores[store].address}, function(data, status){
if (status == google.maps.GeocoderStatus.OK) {
stores[store].latlng = data[0].geometry.location;
} else {
alert("Geocode failed: " + status);
}
});
}
var stores = [];
stores.push({'address': "1 California Ave, San Francisco, CA"});
stores.push({'address': "16 California Ave, San Francisco, CA"});
stores.push({'address': "20 California Ave, San Francisco, CA"});
stores.push({'address': "30 California Ave, San Francisco, CA"});
var geocoder = new google.maps.Geocoder();
var i = 0;
while( i < stores.length){
geocodeAddress(i);
i++;
};