因此,我正在解析信息并使用多个地址查询地理编码。我不完全确定最好的方法是什么。所以这就是我想要做的。
for($j = 0; $j < $rawArray.length; $j++){
$baseStr = $addresNum != 'empty' ? $rawArray[$j][$addresNum] + ' ': '';
$baseStr += $addresStr != 'empty' ? $rawArray[$j][$addresStr] + ' ': '';
$baseStr += $addresDiv != 'empty' ? ', ' + $rawArray[$j][$addresDiv] : '';
$baseStr = ($baseStr.toLowerCase().indexOf("qc") >= 0 || $baseStr.toLowerCase().match(/qu[e-é]bec/) ? $baseStr : $baseStr + ", Qc");
console.log("Looking for: " + $baseStr.match(/\w\d\w([ ])*\d\w\d/i)[0]);
$baseStr = $baseStr.match(/\w\d\w([ ])*\d\w\d/i)[0];
$geocoder.geocode({
address: $baseStr
}, function(locResult, status) {
$arrayCoords[$j] = new Array();
if (status == google.maps.GeocoderStatus.OK) {
$arrayCoords[$j][0] = locResult[0].geometry.location.lat();
$arrayCoords[$j][1] = locResult[0].geometry.location.lng();
}else {
$arrayCoords[$j][0] = '';
$arrayCoords[$j][1] = '';
}
});
console.log("found: " + $arrayCoords[$j][0] + $arrayCoords[$j][1]);
}
现在,我认为填充一个数组并使用它是一个好主意。所以我这样做了:
$timeout = setInterval(function()
{
for($j = 0; $j < $rawArray.length; $j++){
console.log($globalGoogleArray[$j]);
}
if($globalGoogleArray.length == $rawArray.length)
{
console.log($globalGoogleArray);
//TODO: stopTimer();
}
}, 100);
而就在之前console.log("found: " + $arrayCoords[$j][0] + $arrayCoords[$j][1]);
我添加了$globalGoogleArray[$j] = $arrayCoords[$j][0] + ", " + $arrayCoords[$j][1];
如果我可以$globalGoogleArray
填充我的值,那么我可以停止计时器并触发一个可以处理数组内容的函数。这可能不是最好的方法,我愿意接受建议,但我并没有以我想要的结束。计时器放置在顶部,其中for
的console.log 只返回未定义,即使for
(this one: console.log("found: " + $arrayCoords[$j][0] + $arrayCoords[$j][1]);
)中的console.log 确实输出了我期望它输出的内容。
谁能告诉我为什么我无法在我的 globalGoogleArray 中获得地理编码的输出?