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 7193405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:09:50+00:00 2026-05-28T20:09:50+00:00

I am calling a google api function repeatedly in a loop. Each time i

  • 0

I am calling a google api function repeatedly in a loop.
Each time i want to remove one element of the address array.

for(var i = 0; i < AddressObject.addressToArray.length; i++ ){              
  srPerformGeocode(AddressObject);    
  console.log(AddressObject.addressToArray);
  AddressObject.addressToArray.splice(0, 1);                
}
    // --------------------------------------------------------------
    //  Perform geocoding 
    // --------------------------------------------------------------
    function srPerformGeocode(AddressObject)
    {       
        address = AddressObject.addressToArray.join(",");   
        console.log(AddressObject.addressToArray);      
        if (geocoder){                      
            geocoder.geocode({'address': address.trim() }, function (results, status) 
            {                                           
                if (status == google.maps.GeocoderStatus.OK){                   
                    console.log("geocoded " + AddressObject.addressToArray);
                                        // Do something 
                }
                else{                   
                    alert("FAIL");
                }
            }); 
        }
    }

What appears to be happening is that the loop is running i times,
and function srPerformGeocode is running i times each time using
the last value of the i loop.

['field1' ,'field2' ,'field3' ,'field4' ]
['field1' ,'field2' ,'field3' ]
['field1' ,'field2'  ]
['field1'  ]
geocoded field1
geocoded field1
geocoded field1
geocoded field1
  • 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-28T20:09:51+00:00Added an answer on May 28, 2026 at 8:09 pm

    This isn’t a closure issue, because you never actually close over any variables (other than address in srPerformGeocode, but that’s inconsequential to your problem). Read more about what closures are here.

    JavaScript passes objects by reference1, so you’re passing the same array around on each iteration of the loop (and thus, each invocation of srPerformGeocode).

    This is actually fine until the asynchronous callback is executed – the geocoding service is called with the parameters you see in the first set of console outputs, but when the callback is executed and you log “geocoded …”, you’ll only see AddressObject as it existed after the last iteration of the loop, because the loop finished a long time ago (this is very nature of async JavaScript).

    The way to fix your problem is to pass each invocation of srPerformGeocode a copy of the AddressObject array. Conveniently, arrays have a method that return a new array – slice. (splice only modifies an array; it does not create a new one.)

    for (var i = 0; i < AddressObject.addressToArray.length; i++) {              
     srPerformGeocode(AddressObject.slice(0, -i));
    }
    

    (Note that this is a shallow copy; that is, any objects in the array still point to the same objects as the original array. This doesn’t matter here since your array is full of primitives.)

    Some other notes:

    1. String.trim doesn’t exist in IE ≤ 8, so your call to address.trim() will throw an exception.
    2. Google Maps doesn’t really do bulk geocoding; you may find yourself getting rate limited or even shut down completely if you make too many geocoding calls.

    1 Pedants will disagree with this statement; see the linked answer.

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

Sidebar

Related Questions

I'm finding plenty of examples through Google of how to call an API function
i'm having trouble with java programming. This java is calling Google API which provide
Calling Validate() on an XmlDocument requires passing in a ValidationEventHandler delegate. That event function
When calling php via cli, the current directory is NOT changed to the one
I'm using the navigator.geolocation.getCurrentPosition(function) api in firefox 3.6. When i try to call this
import com.google.appengine.api.labs.taskqueue.Queue; import com.google.appengine.api.labs.taskqueue.QueueFactory; import static com.google.appengine.api.labs.taskqueue.TaskOptions.Builder.*; // ... Queue queue = QueueFactory.getDefaultQueue(); queue.add(url(/worker).param(key,
The following function works perfectly on our production site function flickrGetPhotos(){ $.getJSON(http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key= + flickrApiKey
Im using this .sortable function on my list. My problem is that I want
I want to call google map intent without showing Complete Action Dialog? Is it
I am trying to send get requests to the Google Places API with this

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.