GDirections 25 element limit

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);
  }
}

APIs are Hard but fun

Currently I’m working on an abstraction layer for the Google Maps API, this is purely written in Javascript, allowing any language to produce JSON objects and pass them quite seemlessly onto a Google Map. Originally this was planned to be able to be used across maps APIs (Virtual Earth, Yahoo Maps) but has descended into just being Google Maps – to begin with anyway. Currently the plan is to provide a set of handlers that know how to process specific datatypes and interact with the map ie. placing the markers, drawing the routes and monitoring changes. I think going forward, this can really help us at Xebidy move quickly, building rich location based applications very quickly.

It has been mentioned a number of times recently (though I can’t think who, probably John Resig) that building API’s is hard and I can’t help but agree. While it’s definitely challenging, I think it can be an extremely rewarding thing to get right. Developers really do feel the benefits of having a well thought out APIs with productivity gains and less headaches.

One of the biggest curses yet greatest benefits of using Javascript is the many ways you can morph the language. Using ‘apply’ and ‘call’ you can provide a developer down the track some powerful built-in functionality. Throughout the XEMap API I’ve tried to avoid any developers ever instantiating a handler of their own (no new XELocationHandler). This lets me rewrite the this variable whenever or however I please. To store configuration variables I pass an options array to each function. This in turn leaves those functions to be reusable out of the context of the API.

Google Maps API: GDirections with no Markers

I’ve been working with the Google Maps API quite a lot over the past couple of weeks, but one thing that has continuously thwarted me, has been how to remove the little Info Window maps from a route that has been loaded using GDirections and finally I have found the answer. When you load a GDirections object with the optional map panel in the constructor it automatically places the route and markers on the map. The work around is to not provide any arguments to the constructor, and instead load retrieve the GPolyline with the getPolyline method and the getPolyline: true option. Then in the “load” event, add it to the map and presto.


var dirs = new GDirections();
GEvent.addListener(dirs, "load", function() {
var polyline = dirs.getPolyline();
map.addOverlay(polyline);
});
dirs.loadFromWaypoints("Directions from here to there", {getPolyline: true});