Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Driving distance calculation

8:01 pm on Sep 5, 2007 (gmt 0)

New User

10+ Year Member

joined:Aug 9, 2004
votes: 0


I have just implemented the google maps api to my site, so that I can show a route from address A to B.

What I would like to accomplish is to add the number of kilometers between those two addresses. I don't want the entire list of roads to take, just the number of kilometers between the addresses, if I were to take the suggested route.

So the question is what do I need to add to my code, in order to get the driving distance in kilometers?

You can also look at my code right here...

<body bgcolor='#EBE7DE' onload='load()' onunload='GUnload()'><script src=" [maps.google.com...] type="text/javascript"></script> <script type="text/javascript"> //<![CDATA[ var map; var gdir; var geocoder = null; var addressMarker; function load() { if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById("map")); map.addControl(new GSmallMapControl()); gdir = new GDirections(map, document.getElementById("directions")); GEvent.addListener(gdir, "load", onGDirectionsLoad); GEvent.addListener(gdir, "error", handleErrors); setDirections(", 3300, danmark", ", 8800, danmark", "dk_DK"); } } function setDirections(fromAddress, toAddress, locale) { gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale }); } function handleErrors(){ if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code); else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code); else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code); // else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong // alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code); else if (gdir.getStatus().code == G_GEO_BAD_KEY) alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code); else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code); else alert("An unknown error occurred."); } function onGDirectionsLoad(){ // Use this function to access information about the latest load() // results. // e.g. // document.getElementById("getStatus").innerHTML = gdir.getStatus().code; // and yada yada yada... }load(); //]]> </script> <!-- <div id="directions" style="width: 275px"><br> --><div id="map" style="width: 330px; height: 450px"> </body>

[edited by: mona at 6:43 pm (utc) on Sep. 11, 2007]
[edit reason] no urls - thx! [/edit]