
How do I add a Google Maps directions tool to my site?My boss has me working on our company Web site and she wants me to add a box on our "contact us" page that lets people type in their address and at the click of a button bring up a window with driving directions from their location to our office. Is that even possible? Of course it's possible, anything's possible on the modern Web! :-) There are a lot of different sites that help you with driving directions, but let's have a look at how Google Maps does it. The first thing to notice is that you get to the directions aspect of Google Maps by using a slightly different URL: you have to append ?f=d to the end of the URL, like this: http://maps.google.com/maps?f=d
But that's not quite what we want, really. That lets people enter both the starting and ending point and get directions, but what you seek is something quite a bit more sophisticated. Nonetheless, the first step is always to enter some data and see what URL is produced (you can unwrap the form, but this is easier). So let's enter a start and end address and see what Google does with it. It's not quite that simple, however, because Google's using various tricks to update material within a page, rather than loading new pages. Fortunately, we can use one of the secondary links to generate a URL. Instead of using "Link to this page", however, I'll experiment with something else. Click on the "Send" link and you'll see a small window pop up: ![]() (A word of explanation is in order here: I entered "start" and "end" as the beginning and ending points of our experimental journey and Google Maps expanded this to "Start, Uninc Richland Parish, LA" and "Vance AFT" (Oklahoma). Why? I have no idea, but it doesn't really matter, because all we need is the URL from within the message...) The URL within the message is pretty long: http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=start&
daddr=end&sll=37.0625,-95.677068&sspn=33.572881,61.171875&ie=UTF8&z=7
There's a lot here that we don't need, and after a bit of experimentation, the minimum set of fields for the URL are, ready for this? http://maps.google.com/maps?saddr=start&daddr=end
To be clear, "start" and "end" are the values I have been using for addresses, so now it's finally time to experiment with the address itself, since we've ascertained how to feed a URL to Google Maps to get directions! Let's make this interesting, though. I'm going to use a real address and invite you to try and figure out what organization uses this as its mailing address. The address is "12th & C Street SW, Washington DC", but we don't want to drop that into the URL, we want to make that a hidden field in a simple form where the visitor is invited to enter their own address. It'd look like this: <form action="http://maps.google.com/maps" method="get">
Enter your starting address: <input type="text" name="saddr" /> <input type="hidden" name="daddr" value="12th & C Street SW, Washington DC" /> <input type="submit" value="get directions" /> </form> and indeed, that's exactly the code you need, just change the address I have to the address you want as the destination. One more tweak: since you want to have the results show in a new window, you'll want to add a target="_blank" to the "form" tag. The final code: <form action="http://maps.google.com/maps" method="get" target="_blank">
Enter your starting address: <input type="text" name="saddr" /> <input type="hidden" name="daddr" value="12th & C Street SW, Washington DC" /> <input type="submit" value="get directions" /> </form> Here, try it: Simple, elegant and, yes, exactly what the boss ordered!
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
Computer and Internet Basics
(Article 7982)
Tagged: directions, google maps, hacking maps, mapping, mapquest Previous: Updated: How do I change my MySpace profile picture? Next: Are frames obsolete on Web sites? Do I need to redesign my site? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. I am in the process of creating a website that uses the above code... thanks btw... i worked great. Is there some code that will automatically check the "Avoid Highways" check box on the Google Maps webpage. Thanks for your help, so great and what about if i just need to use google map to show my office at my website no need direction from how can i do it ? Posted by: Pawint at April 30, 2008 4:54 PMHi Dave, as always, I found your articles very useful and educational. Thank you. I have a question, in this one: http://www.askdavetaylor.com/how_to_add_google_maps_directions_tool_search_web_site.html, there is a box with google map and says touch to start, it looks very nice and I would like to embed it into my new site. May be I missed something, there's nowhere in the article mention it. Can you tell me where I can get it? Thanks. Rich Posted by: rich at May 5, 2008 4:03 PMExactly what I was looking for. Thanks! Posted by: Michelle Blum at May 23, 2009 7:30 PMHi the above is great information. You have the destination box but how can I put to and from? The search!? Posted by: Eamonn at June 30, 2009 2:18 PMI want to utilize the google map function on my website. However, is it possible for this new page (displaying directions) to be embeded onto our webpage? Posted by: shaan at July 30, 2009 12:46 PMExactly what I was looking for, thank you very much for you simple and great explanations. can you tell me how to put a translation button onto my web site created on iweb 08. I have tried the google translte button but it doesn't function once I have published the page many thanks Posted by: steven at December 18, 2009 4:05 AMHi Dave, Greg, are you sure that's not a preference in your browser? Try checking the same page and directions input with a different browser or different computer?? Posted by: Dave Taylor at December 30, 2009 7:57 PMHi Dave, I think it is great that you posted this. I have so many people that ask what the code is to insert direction inquiries in to their websites. Now all I have to do is send them this URL. Thanks for sparing me some time and effort having to explain how to do this! Posted by: John at January 26, 2010 4:54 PMHow to add Google directions to my email? I would like the "Click for Directions" included in my outgoing emails. Posted by: Belinda at February 23, 2010 8:16 PMWhat if I want to use their input as the beginning of a Google Maps route? GMaps generates a URL for such a route with the non-initial steps all compressed into the "daddr" input field: daddr=39.010443,-81.090461+to:39.010714,-81.136179 But if you use this notation in your HTML form, GMaps interprets that whole string as a single destination, which it obviously can't find. Do you know of any way to use such a form to generate directions to a series of destinations? Thanks! I have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|