0

I have been searching SE for a good 2 hours now trying to solve the reason why I am losing the value of my 'locations' variable. Leveraging closure looked like the best solution, but in the context of the callback function below I couldn't figure out how to implement it properly.

Can someone lend a little insight regarding this phenomena how how exactly it could be remedied? If there are any key terms please point them out so I can research them accordingly.

Thank you for your time.

The code in question:

for (var i = 0; i < locations.length; i++) {
    geocoder.geocode({
        'address': locations[i] // Returns location as expected
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            alert(locations[i]); // returns undefined
        }
    })
}

To clarify, I have already seen the closest post similar to this one: When using callbacks inside a loop in javascript, is there any way to save a variable that's updated in the loop for use in the callback? but I am unable to make it work within the scope of the situation.

Thanks again.

Community
  • 1
  • 1
KennyGHanson
  • 106
  • 1
  • 7

1 Answers1

0
for (var i = 0; i < locations.length; i++) {
    geocoder.geocode({
        'address': locations[i] // Returns location as expected
    }, (function (i) {



        var location = locations[i];
        function getLoc()
        {

           return location;

        }

        return function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(getLoc());
        }
        }(i))
    })
}

try this

Rustam
  • 249
  • 3
  • 11
  • care to add some info about the javascript scope? :P – laconbass May 20 '14 at 18:21
  • @laconbass pass it like an object, updated code, whats the alert(locObj) output now? – Rustam May 20 '14 at 18:22
  • @laconbass you know I think that there are location variable too and its overriding your location variable, thats because its undefined, so use an object to pass data like in my code above – Rustam May 20 '14 at 18:32
  • @Rustam The code above gives me an 'Unexpected token' error at `var locObj.location = locations[i];`, not sure why that is. – KennyGHanson May 20 '14 at 18:35
  • @Rustam I'm not the OP, just suggesting you to explain "why" this happens to him – laconbass May 20 '14 at 18:40
  • @KennyGHanson I think because when that function iss passed as the second parameter to the geocoder.geocode function there are already defined variable named location, and thats why its undefined, you can try to pass another name, but I think its better to pass a object with some specific name, and the properties can have any name of this object – Rustam May 20 '14 at 18:43
  • @Rustam - Same error, this time at `}(i))`, doesn't seem to like the last closing parenthesis. Object logged, but undefined. – KennyGHanson May 20 '14 at 18:44
  • @KennyGHanson, updated, try to define a function that will return the value and get that value, cause its saves a link to that function, maybe will work – Rustam May 20 '14 at 18:57
  • @Rustam - Same error, same line. I appreciate the help. Perhaps someone will be able to decipher this mystery. – KennyGHanson May 20 '14 at 19:12
  • can you open that file where are defined the geocoder.geocode function? open it and make CTRL + F in your editor, see where it is called and try to output it there, also look if there are another location variable – Rustam May 20 '14 at 19:14
  • Just in case anyone else needs this I solved the issue by using a place holder for the locations and using an additional function to replace the placeholder with the values of `locations[i]`...since the additional function is outside of the scope of the geocoder callback, there is no longer an issue with the variable being empty. – KennyGHanson May 23 '14 at 00:07