Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1091921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:36:24+00:00 2026-05-16T23:36:24+00:00

I use http://tile.cloudmade.com/wml/latest/web-maps-lite.js to geocode. There is a address array containing around 20 addresess

  • 0

I use http://tile.cloudmade.com/wml/latest/web-maps-lite.js to geocode.

There is a address array containing around 20 addresess

addresses[n] = {where:where,who:who,contact:contact,note:note,type:type};

Then I loop the array to geocode

for (var i = 0; i < addresses.length; i++) {
    geocoder2.getLocations(addresses[i].where, function(response) { //a callback
       return function(k){
       Lat[k] = response.features[0].centroid.coordinates[0];
       Lng[k] = response.features[0].centroid.coordinates[1];
       latlng = new google.maps.LatLng(Lat[k], Lng[k]);
        MarkerArray[k] = new google.maps.Marker({
          map: map,
          position: latlng,
          zIndex: k,
          title: addresses[k].who,
          icon: icons(addresses[k].type.trim())
        });}(i) // a closure function called
    });
}

But the it always works on the final index. Why??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:36:24+00:00Added an answer on May 16, 2026 at 11:36 pm

    You have the Closure Loop Problem. You appear to be trying to fix it by adding the return function(k)... closure, but that’s all occurring inside the callback function, so it won’t execute until the loop has exited and i is pointing at its final value.

    You would have to push that wrapper out a level so it’s directly inside the loop:

    for (var i = 0; i < addresses.length; i++) {
        geocoder2.getLocations(addresses[i].where, function(k) { //a callback
            return function(response){
                Lat[k] = response.features[0].centroid.coordinates[0];
                Lng[k] = response.features[0].centroid.coordinates[1];
                latlng = new google.maps.LatLng(Lat[k], Lng[k]);
                MarkerArray[k] = new google.maps.Marker({
                    map: map,
                    position: latlng,
                    zIndex: k,
                    title: addresses[k].who,
                    icon: icons(addresses[k].type.trim())
                });
            }
        }(i)); // binding *here* instead!
    }
    

    Or use Function#bind to avoid the nested function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can redirect my urls to only use http://www.domain.com instead
I want to use http://msdn.microsoft.com/en-us/library/aa370654%28VS.85%29.aspx in my code. But for some reason I cannot
I am trying to use http://code.google.com/p/as3svgrendererlib/ in my flash cs 5.5 project to import
We're trying to use Axis2 to call a web service that cannot use HTTP/1.1
I use http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js but some compaty may block this site, so in this case
Im trying to use http://www.javarants.com/2008/04/13/using-google-app-engine-to-extend-yahoo-pipes/ as inspiration, but I'm having some troubles with the
If I use http://site.com/index.php?page=45 everything looks ok (wanted page loads). But when I go
when i use http://github.com/joshthecoder/tweepy-examples , i find : import tweepy in the appengine\oauth_example\handlers.py but
I am trying to use http://code.google.com/p/amazon-s3-php-class/ to force-dowload files from AWS S3. I have
I need to use HTTP Post to send a a string array like so...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.