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 は undefined を返すだけで、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つの関数を作成しました。それが、この種の状況に対処する方法だとも思います。

関数 1 は、空の文字列を持つ外部関数によって呼び出されます。

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 に答える