Generating Directions from Google maps on a website
I was asked by a customer today to add driving directions to live classes listed on their website.
It seemed a job tailor made for the Google Maps API so I went ahead and got an API key to start developing a test page. While the API offers many features, the one I needed- routing and driving directions- is not part of the actual API (correct me if I’m wrong).
It turns out to be dead simple to implement driving directions by simply creating a form which does a GET to maps.google.com/maps and having the form open a new window using target=”_blank” (yes, I know this is deprecated but on a production site you sometimes must “engineer” a solution which works even if not 100% standards compliant).
For my client’s site, all the class locations are already in a database and the pages are generated from there so all I needed to do was write a few lines of code.
Of course if you only need one destination address such as your office you can simply hard code the value into the destination address (named daddr) field.
Here is an example of how to get to the Saint Lucie County Fairgrounds.
Show me directions to the Saint Lucie County Fairgrounds in Fort Pierce, FL (Opens a new window).
Here is the live site where I implemented Google Maps driving directions:
http://www.educationprograms.com/safefood/store/index.php
The code is not the prettiest around but it does work. You should be able to view the source to quickly see how the form works.