trouble when adding the geocode() to a loop

2,185 views
Skip to first unread message

Jervis

unread,
Sep 28, 2011, 10:13:37 AM9/28/11
to Google Maps JavaScript API v3
I have an array named "data" which is filled with some address string
like["New York","Nanjing","Boston"...]. And I want to geocode the
array to add markers in a google map. But when i recall geocode() in a
loop . The callback function excute only once when the loop is ended.
Can you help me~~
A function is defined as follows
function getCoordinates(address) {
var geocoder = new google.maps.Geocoder();
var geocoderRequest = {
address: address
};
geocoder.geocode(geocoderRequest, function(results, status) {
if(status == 'OK') {
if(!marker) {
marker = new google.maps.Marker({
map: map
});
}
var location = results[0].geometry.location;
marker.setPosition(location);
markers.push(marker); //markers is an Array that defined
global for remove or delete the marker later
}
});
}

and when I recall this function in a loop, for example:
for(var i in data) {
getCoordinates(data[i]); //data is a string array filled with
address
};

callback function will not excute until the for loop is ended.
Why????? How can i fix this?

BruceB

unread,
Sep 28, 2011, 1:34:24 PM9/28/11
to google-map...@googlegroups.com
This thread is worth a read:  https://groups.google.com/d/topic/google-maps-js-api-v3/GP4tZLPt_vc/discussion
The function called in your for loop (getCoordinates) is then calling the geocoder and returning immediately to the for loop, because the call to the geocoder is an asynchronous call with a callback function.  The browser isn't going to wait for the geocoder call to be completed before moving on.

In general it's a bad practice to use the Javascript geocoder in a loop like this.  Depending on how many locations are in your array, you could quickly send too many requests over a very short period of time, and Google will start rejecting requests.  You're better off geocoding your locations in advance with the web services geocoder, then using the Javascript APIs to display those locations using lat/lng values.

I didn't see any checks in your callback function for when status != 'OK'.  My guess is that you're getting rejections but don't know because you're not checking:

Jervis

unread,
Sep 28, 2011, 1:56:55 PM9/28/11
to Google Maps JavaScript API v3
thank you very much for your advice, I am too eager to accomplish the
basic function of my code. Some conditions like status != 'OK' is not
taken into cosideration currently...I would complet these after.
But the "data" here is not the same in my code, where the address data
is passed by another function, so it's dynamic rather than static. I
am wandering how can I pre-geocode these address before I use the
their locations in adding markers.

On Sep 28, 9:34 pm, BruceB <bru...@google.com> wrote:
> This thread is worth a read:
>  https://groups.google.com/d/topic/google-maps-js-api-v3/GP4tZLPt_vc/d...
> The function called in your for loop (getCoordinates) is then calling the
> geocoder and returning immediately to the for loop, because the call to the
> geocoder is an asynchronous call with a callback function.  The browser
> isn't going to wait for the geocoder call to be completed before moving on.
>
> In general it's a bad practice to use the Javascript geocoder in a loop like
> this.  Depending on how many locations are in your array, you could quickly
> send too many requests over a very short period of time, and Google will
> start rejecting requests.  You're better off geocoding your locations in
> advance with the web services geocoder, then using the Javascript APIs to
> display those locations using lat/lng values.
>
> I didn't see any checks in your callback function for when status != 'OK'.
>  My guess is that you're getting rejections but don't know because you're
> not checking:http://code.google.com/apis/maps/documentation/javascript/services.ht...

BruceB

unread,
Sep 28, 2011, 2:09:33 PM9/28/11
to google-map...@googlegroups.com
This article might be worth reading:  http://code.google.com/apis/maps/articles/geocodestrat.html
The client-side (Javascript) geocoder isn't ideally used for geocoding a large array of addresses on demand.  More commonly it would geocode one or maybe two, the results of which would be shown on a map, and then the browser would wait for the next user interaction.  If your function fires off more than a few requests at the same time (which it will do in your for loop), the geocoder will start rejecting requests.

I realize you want to get the basics working first, but I'd still encourage you to add a few lines to get the status:
function getCoordinates(address) {
       var geocoder = new google.maps.Geocoder();
       var geocoderRequest = {
               address: address
       };
       geocoder.geocode(geocoderRequest, function(results, status) {
               if(status == 'OK') {
                       if(!marker) {
                               marker = new google.maps.Marker({
                                       map: map
                               });
                       }
                       var location = results[0].geometry.location;
                       marker.setPosition(location);
                       markers.push(marker); //markers is an Array that defined global for remove or delete the marker later
                    }
                    else {
                       window.console.error("geocode request failed with: " + status);
                    }
       });
}

Or use window.console.log, alert, or some other custom logging function to show you what's coming back from the geocoder.

Jervis

unread,
Sep 29, 2011, 1:10:24 AM9/29/11
to Google Maps JavaScript API v3
It's really nice of you!Thanks a lot! I will take your advice.

>                     *else {*
> *                       **window.console.error**("geocode request failed
> with: " + status);*
> *                    }
> *       });

Reply all
Reply to author
Forward
0 new messages