2007-08-16 23:14:00 Bare minimum code to use the Google Maps API to find distances
This is just about the bare minimum that one needs to see how to make calls against the Google Maps API to get distance results. From here it's easy to branch out and try other fun features. There is no error checking here, which can throw you if you only think you have working addresses and yet it's not working. Real code should check for errors. That said:
( html )
  1  <body onload="load()" onunload="GUnload()">
2 <script src="http://maps.google.com/maps?file=api&v=2&key=(your key here)" type="text/javascript">
3 </script>
4 <script type="text/javascript">
5 var gdir;
6 function load() {
7 if (GBrowserIsCompatible()) {
8 gdir = new GDirections( )
9 GEvent.addListener( gdir, "load", onGDirectionsLoad )
10 }
11 }
12 function setDirections( fromAddress, toAddress, locale ) {
13 gdir.load( 'from: '+fromAddress+' to: '+toAddress )
14 }
15 function onGDirectionsLoad() {
16 document.getElementById("getDistance").innerHTML = gdir.getSummaryHtml()
17 }
18 </script>
19 <form action="#" onsubmit="setDirections(
20 '784 Snediker Avenue Brooklyn NY', '12 5th Avenue New York, NY', 'en' ); return false"
>
21 <input name="submit" type="submit" value="Get Directions!" />
22 </form>
23 <div id="getDistance"></div>
24 </body>

Just make sure you substitute "(your key here)" with your actual Google-supplied code.
  • Ty (Tue, October 20th, 2009, 3:15am UTC)
    Thank you very much for posting this extremely helpful code!!!!!

    Ty

Leave a comment