Retrieve driving distance from one zip code to another using Google Maps API

MrComputrMan
MrComputrMan used Ask the Experts™
on
I would like some examples of javascript I might use to interface with Google Maps API so that I can get the driving distance from one address code to another.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Is there nothing simple out there that I can use?  The searching that I found just didn't yeild any code snipets.  It doesn't have to be the google API either.  It could be any of the free stuff that's out there.
Most Valuable Expert 2011
Top Expert 2016
Commented:
I worked this issue in late 2008 and early 2009, and maybe there is a better solution set available to us now, but here is what I learned.  Driving distances can be VERY different from the Haversine distance.  When you use the Haversine formula, you get a distance that is point-to-point and if there is a river in the way, the distance calculation will take you across the river, whether there is a bridge or not.

To get an accurate driving distance you need to take the geolocations of every point along the way where the road bends.  Then you sum the Haversine distances for every pair of points.

In the end I abandoned the project, but Google may have something in the API now that will give you that information.  Or you can do this sort of thing that I tried - scrape the Google web page (not recommended, but it seemed to work).  There is some pidgin code here because of my framework, but you can see the concept that I tried - and dispensed with since it would violate the Google TOS.

In the end I took the Haversine distance and multiplied by 1.25.  Nobody seemed too concerned about that level of accuracy. ;-)

Good luck with it, ~Ray
<?php // db_call_driving_distance.php

require_once('_config.php');

$zip_home = substr(get_clean_positive_integer_string($_GET["a"]),0,5);
$zip_away = substr(get_clean_positive_integer_string($_GET["b"]),0,5);

echo "<html><head><title>ZIP code A $zip_home to ZIP code B $zip_away Driving Distance</title></head>\n";

if (strlen($zip_home . $zip_away) == 10) 
{
    // CONSTRUCT A URL TO GET A GOOGLE PAGE
    $google_url  = "http://maps.google.com/maps?f=d&hl=en&saddr=" . $zip_home . "&daddr=" . $zip_away . "&ie=UTF8&z=9&pw=2";
    $google_html = my_curl($google_url);

    // TRUNCATE HEAD OF GOOGLE HTML STRING
    $google_html = ereg_replace(' +', ' ', $google_html);
    $pointer     = strpos($google_html, "timedist ul");
    if ($pointer === FALSE) { die("No timedist ul"); }
    $google_html = substr($google_html, $pointer, strlen($google_html));

    // TRUNCATE TAIL
    $pointer     = strpos($google_html, "</td>");
    $google_html = substr($google_html, 0, $pointer);

    // TIDY UP IN CASE GOOGLE EVER GOES TO STRICT
    $google_html = str_replace('"', '', $google_html);

    // ISOLATE THE STRING WE WANT
    $x = explode('<div class=pw>', $google_html);
    $answer = trim(strip_tags($x[1]));
    $answer = str_replace('&#160;', ' ', $answer);

    echo "From $zip_home to $zip_away is $answer [<a target=\"_blank\" href=\"$google_url\">View</a>] \n";

    // GET THE PRICE OF GAS FROM THE GOVERNMENT
    $gasoline_url  = "http://www.eia.doe.gov/oil_gas/petroleum/data_publications/wrgp/padd_us_mini_report.html";
    $gasoline_html = my_curl($gasoline_url);
    $x = explode('$', $gasoline_html);
    $gas_price     = strip_tags($x[2]);
    $gas_price_p   = '$' . number_format($gas_price,2);
    echo "at $gas_price_p per gallon, \n";

    // COMPUTE GAS COSTS FOR THIS TRIP
    $miles       = explode(' ', $answer);
    $miles       = str_replace(',', '', $miles); // OVER 1,000 ?
    $mpg         = 20; // REASONABLE USA ASSUMPTION
    $gas_costs   = $miles[0] * $gas_price / $mpg;
    $gas_costs_p = '$' . number_format($gas_costs,2);
    echo "it's gonna cost you $gas_costs_p to go. \n";

}


?>
<body>
<form method="get">
ZIP code Point A: <input type="text" name="a" autocomplete="off" /> &nbsp;
ZIP code Point B: <input type="text" name="b" autocomplete="off" /> &nbsp;
<input type="submit" name="_submit" value="go" />
</form>
</body>
</html>

Open in new window

Author

Commented:
On my iPhone, so I'll keep it breif.

I do have a solution that gives the "as the crow flies" distance, and unfortunately my particular need will not tolerate the inaccuracy, though I know nothing out there is perfect.  I think your right in saying that the latest apis should have somthing that will do this with a degree of ease.  Thank you for yor post. :). It's good to know that I am not the only one that is having trouble finding a good soltion for driving distance.  I thought I was loosing my edge.

Still hopeful!!! Is there anyone out there that has a code snippet yeilding driving distance via JavaScript for the google, bing, or mapquest APIs?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Most Valuable Expert 2011
Top Expert 2016

Commented:
You might want to ask a moderator to put this into the JavaScript Zone.  Also, don't forget the Yahoo APIs

Best of luck and best regards, ~Ray

Author

Commented:
You're right.  I must have clicked the wrong zone when I set this up.  

I did find the solution though.  

http://briancray.com/2009/06/23/calculate-driving-distance-google-maps-api/
Most Valuable Expert 2011
Top Expert 2016

Commented:
Very cool!  I did not test those solutions yet, but I'm glad you're on the right track, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial