Hack 13. Create a Route with a Click (or Two)

You can even take Google Maps where the roads don't go.

Driving directions don't always take you where you want to go, or they may take you the wrong way. You can use a little Google Maps hack to build up your own list of points that can be saved in different formats that can be loaded into a GPS or other tool that supports the GPX standard.

The click-to-route tool is at http://mappinghacks.com/projects/gmaps/clicktoroute.html. You click on the map to create a continuous track. An example is shown in Figure 2-9.

Figure 2-9. No roadsonly walkers, horses, and bikes here!

Once you've created your route, click on one of the Export buttons. Clicking on CSV (Comma Separated Values) generates a pure-text file with the latitude and longitude separated with commas. GPX Track makes a GPX Tracklog file. GPX Route generates a set of points that can be loaded into a GPS. Here is a sample of the route as a CSV file:

	38.4047068183193, -122.84743666648865
	38.4041771393969, -122.84764051437378
	38.403941725296505, -122.84796237945557

And here is an example of a GPX Tracklog:

	
	
	Google Maps Hacks is Good
	
	
	
	
	
	
	

Many GPS units have a limit on the number of points that can be used in a route. This GPSBabel command will simplify your list of points to 30.

	gpsbabel -r -i gpx -f route.gpx 
	 -x simplify,count=30 -o gpx 
	 -F shorter_route.gpx
 

See "Load Driving Directions into Your GPS" [Hack #35] for more on reducing the number of points and loading a route file into your GPS.

 

2.5.1. The Code

This hack is almost identical to "How Far Is That? Go Beyond Driving Directions" [Hack #12], with the addition of one function that generates the formatted list of points, and buttons to call this function. The buttons are inserted with this HTML code:


 

The onClick event is set to call the exportPoints function with a parameter to set the format of the exported points. This example shows the exportPoints function with the code for the GPX exports removed. The GPX format is simple XML, and that clutters up the example.

	function exportPoints(format) {
	 var export_string;
	 if (format=='csv') {
	 //csv header 
	 export_string = export_string + "latitude, longitude
";
	 }

	 for (i = 0; i < x_array.length; i++) {
	 var lon = x_array[i];
	 var lat = y_array[i];
	 if (format=='csv') {

	 export_string = export_string + lat + ", " + lon + "
";
	 }
	 }
	
	 // write into document
	 document.getElementById("output").value=export_string;
	}

The results of your selected route will appear in a textarea below the map. You may need to scroll down to see the list. You can select the whole text area and paste it into your own document. Once you have a list of points to map back on Google Maps [Hack #37], export the points to your GPS [Hack #35], and even calculate driving directions between the points [Hack #36].

You can also use this technique to plan a trip or to explore more about a trip you took without a GPS (or a trip where the GPS didn't work because the darn satellite signals wouldn't penetrate the steel canyons of the city).


You Are Here: Introducing Google Maps

Introducing the Google Maps API

Mashing Up Google Maps

On the Road with Google Maps

Google Maps in Words and Pictures

API Tips and Tricks

Extreme Google Maps Hacks



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

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