JavaScript将数组推入数组
我目前正在使用GoogleMaps的地理编码,需要创建一个数组,其中包含数组作为元素 基本上,我需要创建以下内容:JavaScript将数组推入数组,javascript,arrays,multidimensional-array,google-geocoder,Javascript,Arrays,Multidimensional Array,Google Geocoder,我目前正在使用GoogleMaps的地理编码,需要创建一个数组,其中包含数组作为元素 基本上,我需要创建以下内容: var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Be
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
但是动态的!
我需要这个阵列,以便稍后在地图上放置图钉
我在做什么:
var locations = []; // The initial array
for (var i = 0; i < addresses.length; ++i){
var address=addresses[i]; // the address e.g. 15 Main St, Hyannis, MA
geocoder.geocode({ 'address': address}, function(results){
var obj = {
0: address,
1: results[0].geometry.location.hb,
2: results[0].geometry.location.ib,
3: i
};
console.log(obj);
locations.push(new Array());
locations[i].push(obj);
});
};
console.log(locations.length);
var位置=[];//初始数组
对于(变量i=0;i
问题,问题:
我没有看到任何错误,但在末尾位置[]数组为空
如果需要,这里有一个控制台屏幕:
这应该是您所需要的全部:
geocoder.geocode({ 'address': address}, function(results){
locations.push([
address,
results[0].geometry.location.hb,
results[0].geometry.location.ib,
i //this is actually going to always be
//addresses.length because the callback won't fire
//until well after the loop has completed.
//Is this really a necessary field to have
//in your array? if so, you'll need to refactor a bit
]);
});
我没有时间测试代码,但它应该是这样工作的:
function requestLocations( addresses, callback ) {
var remainingLocations = addresses.length;
var locations = [];
for (var i = 0; i < addresses.length; ++i){
var address=addresses[i]; // the address e.g. 15 Main St, Hyannis, MA
locations[i] = [];
geocoder.geocode({ 'address': address}, (
function(idx) {
return function(result) {
locations[idx] = [ addresses[idx],
results[0].geometry.location.hb,
results[0].geometry.location.ib,
idx
];
//decrement the number of remaining addresses
--remainingLocations;
//if there are no more remaining addresses and a callback is provided then call this calback with the locations
if( remainingLocations === 0 && callback ) {
callback(locations);
}
}; // returns the real callback function for your geocoding
})(i) //direct invocation of function with paramter i for scoping
);
}
}
requestLocations(addresses, function( locations ) {
console.dir(locations);
console.log(locations.length);
});
geocode
是异步的。在处理array.Hm之前,您需要等待所有呼叫回复。我认为应该完成循环,脚本才能继续?否?在这个问题中,将空数组推入另一个数组的基本方法并不明显,因此我要指出,对于数组,它是myArray.push(new array())
myArray
必须至少实例化为空数组,即myArray=[]
有意义。。。但是console.log(locations.length);仍然显示0@rinchik可能是因为geocoder.geocode()
是异步的。。。而位置
在您记录时并未更改。请参阅普林霍恩对您的问题的评论…它是异步的,您只需在所有地理代码都已更改后启动控制台.log
finished@BLSully您应该提到,i
将始终是地址。由于范围限制,长度
:我刚测试过!它起作用了!感谢您的详细回答和我的代码:)+1@rinchik其中有一个错误address
应该是回调中的addresses[idx]
。请重新检查您的代码,否则您的结果中将始终具有相同的地址。嗯。。。快速提问!出于某种原因,(var i=0;i var locations = []; // The initial array
for (var i = 0; i < addresses.length; ++i) {
var address = addresses[i]; // the address e.g. 15 Main St, Hyannis, MA
geocoder.geocode({
'address': address
}, function(results) {
//this part is called later when that data is ready (it is an asynchronous callback)
});
};
//because of the async request this is still 0
console.log(locations.length);
function(results) {
var obj = {
0: address,
1: results[0].geometry.location.hb,
2: results[0].geometry.location.ib,
3: i
};
console.log(obj);
locations.push(new Array());
locations[i].push(obj);
}