GDirections 25 element limit
April 28th, 2008
There’s an undocumented feature in the Google Maps API when retrieve driving direction wherein any GDirections request with more than 25 elements returns an error with code G_GEO_BAD_REQUEST (400). This isn’t particularly helpful but I found the answer in the Google Maps forums somewhere. So the trick is to partition your array of waypoints into 25 part segments. Using Prototype 1.5 this is really simple using the inGroupsOf method, however using earlier Prototype versions is a little more tricky. I’m including a function that can do it with either.
function doDirections(elements) {
var mapLimit = 25;
elements = $A(elements);
var groups = null;
if('inGroupsOf' in elements) {
groups = elements.inGroupsOf(mapLimit);
} else {
groups = [];
var tempGroup = [];
elements.each(function(point, index) {
if(index == mapLimit) {
groups.push(tempGroup);
tempGroup = [];
}
tempGroup.push(point);
}
}
groups.each(function(i) {
var direction = new GDirections();
//Insert your options for directions objects here.
direction.loadFromWaypoints(i);
}
}
brian said:
Do you have a working example for this? I cannot get it to work…Thanks
Kalpesh patel said:
Above code is not working