1

The code below overwrites the var store every time the while loop runs. So when the callback runs the store value is usually the last item in the store array.

var stores = [];
stores.push({'address': "1 California Ave, San Francisco, CA"});
stores.push({'address': "16 California Ave, San Francisco, CA"});
stores.push({'address': "20 California Ave, San Francisco, CA"});
stores.push({'address': "30 California Ave, San Francisco, CA"});

var geocoder = new google.maps.Geocoder();

var i = 0;
while( i < stores.length){
  var store = stores[i];
  geocoder.geocode({'address':store.fullAddress}, function(data, status){
          store.latlng = data[0].geometry.location;
          }
        });
  i++;
};

How do I add the correct latlng to the correct store?

This doesn't work either because the var i is changed as well.

var i = 0;
var storesObject = {}
while( i < stores.length){
  var store = stores[i];
  storesObject[i] = stores[i];
  geocoder.geocode({'address':store.fullAddress}, function(data, status){
          storesObject[i].latlng = data[0].geometry.location;
          }
        });
  i++;
};

If I do this how do I match the results to the stores array?

var i = 0;
var results = [];
while( i < stores.length){
  var store = stores[i];
  geocoder.geocode({'address':store.fullAddress}, function(data, status){
          results.push(data[0].geometry.location);
          }
        });
  i++;
};

UPDATE adding in a functional scope fixed my issue:

var i = 0;
while( i < stores.length){
  (function(){
    var store = stores[i];
    geocoder.geocode({'address':store.fullAddress}, function(data, status){
          store.latlng = data[0].geometry.location;
          }
    });
  })();
  i++;
};

see: GMaps JS Geocode: Using/Passing Variables With Asynchronous Geocode Function?

Community
  • 1
  • 1
ChickenFur
  • 2,360
  • 3
  • 20
  • 26
  • http://stackoverflow.com/questions/10555097/gmaps-js-geocode-using-passing-variables-with-asynchronous-geocode-function?rq=1 putting in a functional scope answered my question. Should I just close this question? – ChickenFur Nov 27 '13 at 18:25
  • It'll probably be closed as a duplicate eventually. I'd leave it because people will still be able to find it, and your title may catch some searches. – Pointy Nov 27 '13 at 18:27

1 Answers1

1
function geocodeAddress(store) {
   geocoder.geocode({'address':stores[store].address}, function(data, status){
     if (status == google.maps.GeocoderStatus.OK) {
       stores[store].latlng = data[0].geometry.location;
     } else {
         alert("Geocode failed: " + status);
     }
   });
}
var stores = [];
stores.push({'address': "1 California Ave, San Francisco, CA"});
stores.push({'address': "16 California Ave, San Francisco, CA"});
stores.push({'address': "20 California Ave, San Francisco, CA"});
stores.push({'address': "30 California Ave, San Francisco, CA"});

var geocoder = new google.maps.Geocoder();

var i = 0;
while( i < stores.length){
  geocodeAddress(i);
  i++;
};
geocodezip
  • 158,664
  • 13
  • 220
  • 245