Advertisement

07.08.2008 at 05:31PM PDT, ID: 23548760
[x]
Attachment Details

Using Google Maps API to determine the driving distance between two locations

Asked by Ray_Paseur in Extensible Markup Language (XML), JavaScript, PHP Scripting Language

Tags: Google Maps API, XHTML, XML

Building on this question:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23531323.html

Visit this page and you will see that Google says the driving distance is 237 miles
http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=20016&daddr=15222&sll=39.693615,-78.54249&sspn=1.608247,3.633728&ie=UTF8&z=9

If you use the code snippet, Google says the driving distance is 245 miles.  You will need a Google API key to test it out.  Link here: http://code.google.com/apis/maps/signup.html

Those two answers are pretty close, and in fact, either answer is OK.  The central issue is getting the answer in a way that is programatically useful.

I want to know how to get step-wise driving distances from Google in a way that can be parsed in a PHP script.  If there is an HTTP solution that calls Google and returns a CSV or an XML string that would be ideal.  If you can show me the PHP code to "scrape" the answer out of the script below, that would be OK, too.

Thanks for your help,
RayStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="/style.css" rel="stylesheet" type="text/css" />
<title>Miles to ... using Maps API</title>
<script type="text/javascript" src="http://www.google.com/jsapi?key=[your Google API Key]"></script>
<script type="text/javascript">
//<![CDATA[
google.load("maps", "2");
var gdir;
function load() {
        if (GBrowserIsCompatible()) {
                gdir = new google.maps.Directions();
                google.maps.Event.addListener(gdir, "load", handleLoad);
                gdir.load("from: Washington, DC to: Pittsburgh, PA", {getSteps: true});
        }
}
function handleLoad() {
        document.getElementById("totalMiles").innerHTML = gdir.getDistance().html;
}
window.onload = load;
window.onunload = google.maps.Unload;
//]]>
</script>
</head>
 
<body>
<h3>Miles to ... </h3>
 
<div id="totalMiles"></div>
 
</body>
</html>
[+][-]07.08.2008 at 11:24PM PDT, ID: 21960989

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Extensible Markup Language (XML), JavaScript, PHP Scripting Language
Tags: Google Maps API, XHTML, XML
Sign Up Now!
Solution Provided By: fibo
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.09.2008 at 08:15PM PDT, ID: 21970205

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.10.2008 at 05:33AM PDT, ID: 21972606

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.10.2008 at 11:45AM PDT, ID: 21976290

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.10.2008 at 12:04PM PDT, ID: 21976441

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.10.2008 at 03:01PM PDT, ID: 21977765

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07.10.2008 at 03:45PM PDT, ID: 21978021

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.10.2008 at 03:59PM PDT, ID: 21978101

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.10.2008 at 06:07PM PDT, ID: 21979131

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906