tags:

views:

565

answers:

3

I've used the Google Maps API to make a map that displays direction polylines for multiple users to a common destination. So each user has their own polyline displayed on the map at the same time.

The problem is that there appears to be a limit of how many polylines get returned. When I have more than about 10 users, i stop receiving polyline data. (the last users to be added to the map don't get a polyline). I might point out that the load function still appears to work (the error event listener is not being called), but i either don't get polyline data, or it refuses to draw to the map.

The number of polylines that get drawn is slightly different every time which makes me think google just doesn't like receiving so many requests at once from the same user but i can't be certain.

Does anyone have any ideas how to get around this? I've tried providing latitudes and longitudes instead of address strings to try and cut down on the workload but it doesn't seem to have any effect on the number of polylines that get drawn.

+1  A: 

According to the Google Maps API blog, you should be able to make 10,000 GDirections requests a day without being throttled by Google. However I found a post on the Google Maps API Group that suggests there is a separate throttling mechanism that will return 620 HTTP errors based on the rate that you make requests.

From your question, it looks like your error listener is not being called, so this doesn't quite fit. But I haven't found a official description of this behavior from Google, so I am not sure.

Are you able to buffer your GDirections requests and reduce the rate you are making them? This might be a good test to see if you are being affected by a request rate throttling mechanism.

Cannonade
Finally figured out how to buffer my requests and it seems to work :) If i give 250 milliseconds between each call, i appear to get all the results. I got 32 out of 32 polylines drawn. I haven't tested anything higher but this should be good enough for most cases.
Dominic
That's awesome. Glad you sorted it out.
Cannonade
A: 

Dominic, would you mind posting your solution - I'm having the same issue, but I'm not smart enough to figure out how to buffer my calls. Thanks

Sean
A: 

I just did something fairly simple as shown below. I create an array of all the directions I want to add beforehand. I loop through my directions, add a marker (I add a marker separate to the direction so that there is always a marker even if the direction fails to draw), then I call the setTimeout function to schedule the direction call so that Google doesn't get them all at once. Then I increase the value of milliseconds by about 250 (might have to play around with this value).

var milliseconds = 0;
for(i in directions){
    addMarker(directions[i]); //I place the marker separate to the direction so that there is always a marker even if the direction fails to draw.

    setTimeout( function(){
                  addDirection(directions[directionCounter]); //This is my function that makes the direction request to Google
                  directionCounter++;
                }
                , milliseconds);

    milliseconds += 250;
}

Hope this is helps.

Dominic