Stand in the place where you live, Now face North, Think about direction, Wonder why you haven't before - REM

In Part 1 I showed you how to get started with the MapQuest Advantage API by getting a developer key. In Part 2 I put that key to use by providing access to a basic map in the Map It! widget. Part 3 showed you how to incorporate basic geocoding. In Part 4 I discussed more advanced geocoding topics - including handling multiple matches and specifying geocode search options. Part 5 discussed adding widget options including the default zoom level, specifying point of interest icons, and the default map type. In this installment I'll discuss how to add direction capabilities to the widget!

Directions

Listing 1 demonstrates how to add directions to our widget map. I've added an additional search box where the user can enter the destination address. I've use the Java application developed in Part 3 to obtain the coordinates of the route beginning and destination points. Using the coordinates, I've created the MQGeoAddress object for both points. A new session is created for routing. The starting point and destination are added to the way points. Once the route is calculated through the doRoute method, the highlighted route is added to then added to the map.

Listing 1 - Adding Directions

// create the first address point
var geoAddr1 = new MQGeoAddress();

// create the second address point
var geoAddr2 = new MQGeoAddress();

// set the cooridinates
geoAddr1.setMQLatLng(newCenter1);
geoAddr2.setMQLatLng(newCenter2);

// create a new session
var session = new MQSession();
var routeRes = new MQRouteResults();
var wayPoints = new MQLocationCollection();
var myBB = new MQRectLL(new MQLatLng(),new MQLatLng());

// add the beginning and ending way points
wayPoints.add(geoAddr1);
wayPoints.add(geoAddr2);
var routeOpt = new MQRouteOptions();

//create sessionID from the route server
var routeServerName = "route.access.mapquest.com";
var routeServerPort = "80";
var routeServerPath = "mq";

// obtain a route
var routeExec = new MQExec(routeServerName, routeServerPath,
  routeServerPort, routeServerName, routeServerPath, routeServerPort );
var sessId = routeExec.createSessionEx(session);
routeExec.doRoute(wayPoints,routeOpt,routeRes,sessId,myBB);

// add the route to the map!
myMap.addRouteHighlight(myBB,"http://map.access.mapquest.com",sessId,true);

Conclusion

In Part 7 I'll wrap up the widget by showing you how to retrieve addresses from the Mac OS X Address Book application. For your reference, here are some references to the MapQuest Platform: