0

So, I'm parsing information and querying geocode with multiple addresses. I'm not entirely sure what's the best way of doing it. So here's what I'm trying to do.

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]);
}

Now, I figured that populating an array and working with it would be a good idea. So I did this:

$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);

And just before the console.log("found: " + $arrayCoords[$j][0] + $arrayCoords[$j][1]);

I added $globalGoogleArray[$j] = $arrayCoords[$j][0] + ", " + $arrayCoords[$j][1];

If I can get $globalGoogleArray populated with my values then I can stop the timer and trigger a function that will work with the content of the array. This may not be the the best way of doing it and I'm open to suggestions, still, I'm not ending with what I want. The timer is placed on top of the for and the console.log inside it only returns undefined, even tough the console.log in the for (this one: console.log("found: " + $arrayCoords[$j][0] + $arrayCoords[$j][1]);) does output what I expect it to output.

Can anyone enlighten me as to why I can't get the output of the geocode in my globalGoogleArray?

2 Answers 2

1

Keep track of the number of calls that are returned within your callback function. When the last one has returned, process your $arrayCorrds array.

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
        }

    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

yeah you're right. That's clearly something that I have to do. But it isn't a solution. Inside the ­­­if that you added I wrote this: $globalGoogleArray = $arrayCoords; and a console.log after that. What happens is that I get a bunch of "found" with the right coords and then an array with all but the last slot as undefined (the last slot containing the last correct pair of coords)
0

So, finally knowledge struck me like a glass of cold water. I was just supposed to simulate a loop.

I ended up creating 2 functions that call each other. I also think thats this is how I'm supposed to tackle this kind of situations.

Function one is called by an external function with an empty string:

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
    }
}

Once the address string is generated call the Google async function:

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]);
    });
}

So, the answer wasn't anything fancy. When Google answers the collect function is called again and the if in said function prevent all of this into going into a never ending loop.

GL&HF

Axel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.