I have gotten this to work a bit but can’t seem to figure out what the exact phrasing to get the for loop to pass the correct value will be.
for(i=0; i < the_stores.length; i++) {
uid = the_stores[i].id;
// get the lat long and write info to the console
geocoder.getLatLng(the_stores[i].address, function(point) { return callbackFunc(point, uid); });
}
function callbackFunc(point, myID) {
if(point !== null) {
console.log(point.toString());
console.log(myID);
} else {
return false;
}
}
I’m using the Google Maps API, v2. I want that for loop to pass in a different value of uid to the callback function for each iteration through the loop, but right now it passes in the same value every time. Anyone know how to get the desired effect here? I feel like I’m pretty close. Thank you!
What you are looking for is an “Immediately Invoked Function Expression”. Since JavaScript only has function scope, not block scope, all references to
uidare being bound to the same variable.(For more, see: http://benalman.com/news/2010/11/immediately-invoked-function-expression/)
Here’s a simple way to do it: