1
            var events = <?php echo $sf_data->getRaw('jsonData'); ?>;//some perfecly fine json
            var locations = new Array();
            var geocoder = new google.maps.Geocoder();
            $(events).each(function(i,e){
                geocoder.geocode({address: e.place},function(results,statusCode){
                    locations[e.id] = results[0].geometry.location;
                })
            })
            alert(locations.length);

Okay, the question is easy. How do i modify the locations array inside of closure? The alert shows 0.

2
  • You access it exactly the way you re. However, I don't see you define events, before you each() it. I think we need more code... Commented Aug 4, 2011 at 16:34
  • use firebug's console.log it's much better for debugging. Commented Aug 4, 2011 at 16:35

1 Answer 1

4

The alert shows 0 because you modify locations asynchronously. Anonymous function where you modify locations will be called later then you show alert.

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

2 Comments

Is geocoder.geocode() async? If not, I see no async code in this posted example. (I don't know anything about the Google Maps API)
aw, snap. I forgot it's async. I guess that solves the problem, thanks

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.