Hack 36. Get Driving Directions for More Than Two Locations

Going from here to there is okay, but going from here to there to another there to here is better.

In "Load Driving Directions into Your GPS" [Hack #35] we saw how to compute a route and load it into a GPS. The hack showed how to directly create a URL that will fetch the Google Maps file that contains the route points. GPSBabel can parse the points out of a route file and you can do whatever you would like with this information.

This suggests a script that takes a list of locations that Google can understand and then generates driving directions between each point. In addition to normal locations such as "San Francisco, CA to Denver, CO," Google Maps can generate driving directions based on latitude and longitude. The results of searching for directions from 38, -121 to 39.526421, -119.807539 are shown in Figure 4-19.

Figure 4-19. Directions from latitude/longitude to latitude/longitude

Lat/long routes don't always work. There are some places that are just a bit too far off of the beaten track for road-based routing to be effective. If you try 38, -121 to 39.731482, -119.53537, you get "We could not calculate driving directions between 38, -121 and 39.731482, -119.53537." Since 38, -121 worked in the first example, Google must have a problem with 39.731482, -119.53537. A glance at Figure 4-20 shows that Google should be forgiven for this routing difficulty. Apparently the engineers didn't anticipate helicopter-based mountain-top expeditions.

But lat/longbased directions work often enough to be interesting. Great, but why would you want to get driving directions for multiple points or for lat/longs? Let's say you want to drive from San Francisco to Denver; you can generate the route in Figure 4-21 with the query SFO to DEN, using an airport-to-airport search. Why do you need more than one set of directions?

You've got everything you need1,299 miles, and you are there. But it turns out you want to stop in Moab for some of that Slickrock action. You can't (yet) enter a request to Google for multipart driving directions, such as SFO to Moab, UT to DEN, but with this hack you can get something similar. Download the Perl script called takeme.pl from http://mappinghacks.com/projects/gmaps/takeme.html. The script can accept any combination of locations that Google can understand and GPS waypoints stored in GPX format. For example:

Figure 4-20. You can't always get driving directions to a lat/long

Figure 4-21. San Francisco and Denver are just a click apart

	$ perl takeme.pl SFO to wy_small.gpx to Moab, UT, to DEN

The program returns the combination of driving directions from each point to the next, as well as a simplified list of lat/long points.

	new route segment 37.61830, -122.38632 to 38.57320, -109.55060
	San Francisco Airport
	to Slick Rock Capitol

	Head west
	0.5| mi (734 meters) for 1 min
	 Take the I-380 W
	 ramp to San Francisco (I-280)/San Bruno
	0.1| mi (197 meters) for 12 secs

You might want to specify multiple locations, including a detour to visit friends, or you might want to trick the routing algorithm into using a route that it considers sub-optimal. Here's a real life example of tricking a routing algorithm: my dad and step-mom had just gotten a Prius with a navigation systemthey had had it less than two weeks. My dad, while quite bright, is not particularly technical. One day they were driving and playing with the navigation system. My dad said, "Don't set it to go to our destination until you get to this turnit will take us a bad way from here."

My nontechnical dad had figured out the tool in under a week. Without knowing how, he was dynamically gaming the system's shortest-path algorithm by artificially increasing the weight of one segment by adding the cost of turning around to get back to that segment. He made that segment less attractive to the routing algorithm by inserting another segment in between himself and the dreaded slower path. Specifying intermediate points (or desired side trips) adds local knowledge to the cost calculation used in the routing system. In Google Maps terms, it sure would be nice to have "do not use this segment" checkboxes next to each step in the route, followed by "recalculate my route without the checked segments," but until that happens, you can use the takeme.pl tool!

Google Maps Hacks
Google Maps Hacks: Tips & Tools for Geographic Searching and Remixing
ISBN: 0596101619
EAN: 2147483647
Year: N/A
Pages: 131
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net