0

因此,我正在解析信息并使用多个地址查询地理编码。我不完全确定最好的方法是什么。所以这就是我想要做的。

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 中获得地理编码的输出?

4

2 回答 2

1

跟踪回调函数中返回的调用次数。当最后一个返回时,处理您的$arrayCorrds数组。

var resultCount = 0; //counter for number of calls that have returned

for($j = 0; $j < $rawArray.length; $j++){

    ...

    $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] = '';
        }

        resultCount++; //increment count
        if(resultCount === ($rawArray.length - 1)) {
            //the last result has been retrieved, do something with $arrayCoords here
        }

    });
}
于 2013-05-13T16:47:39.163 回答
0

所以,知识终于像一杯冷水一样打动了我。我只是应该模拟一个循环。

我最终创建了 2 个相互调用的函数。我也认为这就是我应该如何处理这种情况。

函数一由带有空字符串的外部函数调用:

function collectCoords($fromGoogleStr){

    //If being called with params, add them
    if($fromGoogleStr)
        $globalGoogleArray[($globalGoogleArray? $globalGoogleArray.length : 0)] = $fromGoogleStr;

    //Generate address string here here\\


    if ($tmpCounter < $fullArray.length - 1){
        $tmpCounter++;
        generateGoogleCoords($adressString);
    }else{
    //Do something with the complete answer
    }
}

生成地址字符串后,调用 Google 异步函数:

function generateGoogleCoords($baseStr){

    $geocoder = new google.maps.Geocoder();
    $arrayCoords = new Array();

    $geocoder.geocode({
        address: $baseStr
        }, function(locResult, status) {
            $arrayCoords[$j] = new Array();
            $arrayCoords[$j][0] = locResult[0].geometry.location.lat();
            $arrayCoords[$j][1] = locResult[0].geometry.location.lng();
            collectCoords($arrayCoords[$j][0] + ", " +$arrayCoords[$j][1]);
    });
}

所以,答案不是什么花哨的。当 Google 回答时,collect 函数会再次被调用,并且该函数中的 if 会阻止所有这些进入一个永无止境的循环。

GL&HF

阿克塞尔

于 2013-05-13T22:19:57.970 回答