jquery - Populating javascript array with async geocode function -


so, i'm parsing information , querying geocode multiple addresses. i'm not entirely sure what's best way of doing it. here's i'm trying 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, figured populating array , working idea. 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 before console.log("found: " + $arraycoords[$j][0] + $arraycoords[$j][1]);

i added $globalgooglearray[$j] = $arraycoords[$j][0] + ", " + $arraycoords[$j][1];

if can $globalgooglearray populated values can stop timer , trigger function work content of array. may not the best way of doing , i'm open suggestions, still, i'm not ending want. timer placed on top of for , console.log inside returns undefined, tough console.log in for (this one: console.log("found: " + $arraycoords[$j][0] + $arraycoords[$j][1]);) output expect output.

can enlighten me why can't output of geocode in globalgooglearray?

keep track of number of calls returned within callback function. when last 1 has returned, process $arraycorrds array.

var resultcount = 0; //counter number of calls 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, $arraycoords here         }      }); } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -