Question

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

Asked by: Ray_Paseur

I would like to determine the driving distance between two locations.  I have a vague sense that I might be able to do this with Google Maps API.

I have used zipcode-based geolocation algorithms and determined that the distance between Washington, DC and Pittsburgh, PA is about 187 miles.  However these algorithms use great-circle computations and do not take into account the road mileage.  If you use Google Maps to get the driving distance you get 237.  In my experience, that is much closer   to reality.

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

Can anyone show me how to use the Google Maps API to get the driving distance (as opposed to the great circle distance)  between two zip codes?  

I am not worried about inaccuracies that will be sure to pop up in adjacent or nearby zip codes.  I would just like to reduce the 25% error that seems to come along with the geolocation algorithms once you get out about 100 miles or so.

Thanks to all. ~Ray

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-07-01 at 12:08:25ID23531323
Tags

Google Maps API

,

XHTML

,

XML

Topics

PHP Scripting Language

,

Extensible Markup Language (XML)

,

Extensible HTML (XHTML)

Participating Experts
2
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. A star algorithm in road netwoks
    {In the A algorithm the score which is assigned to a node is a combination of the cost of the path so far and the estimated cost to solution. This is normally expressed as an evaluation function f, which involves the sum of of the values returned by two functions g and h, g r...
  2. Drawing semi circles using Bresenham's Algorithm
    Hi Guys I'm reasonably familiar with using Bresenham's Algorithm to draw a circle, but I would like to be able draw half of a circle just by specifying two points - each point , obviously, is a distance of R (radius) from the circle centre. How would I go about doing that?...
  3. searching jobs within 30 miles ,40 miles
    Hi, i am working on job search functionality.here there is a option for the user to search the job within range of 10 miles, 20 miles, 300 miles etc like almost all job site provides for job search.But i am litttle confused about it to do .Bcz i am taking location and state...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: fiboPosted on 2008-07-01 at 15:48:37ID: 21912232

Hi Ray,
1 - You have an example like what you search here
http://econym.googlepages.com/example_state_mileage.htm

2 - More broadly, the tutorial at http://econym.googlepages.com/index.htm is REALLY GREAT

 

by: Ray_PaseurPosted on 2008-07-01 at 16:28:03ID: 21912400

@fibo: Hi, fellow expert.  

I looked at that but the example state mileage went flooey.  Will try again later.

  I was more or less hoping someone had answered this question before and  could show me the Javascript code to ask for the route mileage and the simplexml parse to extract the total mileage.  Such a thing _might_ not exist yet.  I'll read the tut and see if anything jumps out at me.  Thanks for the suggestion.

Going to leave this open a day or two more. ~Ray

 

by: b0lsc0ttPosted on 2008-07-01 at 20:19:31ID: 21913186

You should be able to do what you want with the page below.  Let me know if you have a question or need more info.

bol

<!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="></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>

                                              
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:

Select allOpen in new window

 

by: b0lsc0ttPosted on 2008-07-01 at 20:20:05ID: 21913190

Of course add your key value to the script tag. :)

bol

 

by: Ray_PaseurPosted on 2008-07-01 at 20:27:01ID: 21913216

@b0lsc0tt: Thanks - late here in the East.  Will try in the morning.  Appreciate your post. ~Ray

 

by: Ray_PaseurPosted on 2008-07-08 at 13:45:26ID: 21958072

FIBO and B0LSC0TT, thanks for your suggestions - good information and helpful.  

A perfect solution would be a way to access the contents of the DIV named  "totalMiles"  programatically.  It shows on the screen in FF, but is not there in the "view source".

Is there a "document.write() or some kind of XML that could generate the totalMiles field in clear text?

Thanks, ~Ray

 

by: b0lsc0ttPosted on 2008-07-08 at 14:33:26ID: 21958492

Ray,

Did you try my method?  It will give the total miles and gdir.getDistance().html from that code can be used to get the value and use it in your program.  That value would actually be a string that would have html.  Instead of html you can use meters (e.g. gdir.getDistance().meters ) to get a numeric value of the distance in meters.  Of course you would need to convert that but you may prefer it to removing the html.  I can help with either of these if you need.

The key is you should have what you need now, even if you need to use the value in the Javascript.  The reason you don't see the value on the page when you choose View Source is it doesn't show generated source.  Firefox has an extension called Web Developer that will show the generated source to you and you could use it to see how the script adds it to the page.  It is definitely in the source though because you see it on the page, it just isn't in the original source. :)

Let me know if you have a question.

bol

 

by: Ray_PaseurPosted on 2008-07-08 at 15:40:39ID: 21958967

bol: Yes, I got a Google Maps key and tried it and it worked fine.  What I'm hoping to find is the way to get that mileage back in a data string of some sort --  XML is an obvious choice --  so I can parse it in a PHP script and extract the mileage.

I'm not very good with Javascript. Is there any way to get that mileage field to appear in clear text somewhere?  If not XML, I am willing to write a screen scraper script, so long as the mileage data can be found by my PHP script when I read the page.

Thanks for your help, ~Ray

 

by: b0lsc0ttPosted on 2008-07-08 at 15:56:53ID: 21959123

It wasn't ever clear that you needed this on your PHP script.  That would change things at least a bit.  It could almost use its own question, especially if you will need a lot of details or help.  We'll see how the below works. :)

How does the PHP script need it?  Why does it need to be in PHP and on the server?  The API is made and designed as a clientside tool so it will be hard (or impossible) to completely eliminate that.  In my opinion if you really need this on the server and just in PHP then you would be better off using something like PHP's cUrl and having it go to the Google Map's page and scrape it.  The API almost is a hinderance if you aren't doing this on the client side (at least in part).

Of course it is different if you are using this as part of a form or AJAX "app."  In that case the distance could be retrieved as we have above and then put in a form or sent with other values in the XMLHTTP object to the server.  The PHP script would get it at this time and could use it.

That is why a little more info about the use of this info and what the pages are doing is needed.  Hopefully my summary of the 2 options will give you an idea of the differences and their needs but let me know if you have a question.  I can definitely provide details if needed but please keep in mind what you originally asked here and what has been provided.  I'll admit one of the zones was PHP but the question seemed to strictly be clientside and about the API. :)  No matter what I am interested and this has been fun but I don't want it to never end or become multiple questions and a project.

bol

 

by: Ray_PaseurPosted on 2008-07-08 at 16:57:31ID: 31472513

fibo: The page never worked, but the tutorial had some value.  

b0l: I will try PHP curl and if I can't get the answer, I will post the question again and try to be clearer that I need to get an XML or CSV output containing the step-wise driving distance.  Thanks to you both for your suggestions. ~Ray

 

by: b0lsc0ttPosted on 2008-07-08 at 18:05:09ID: 21959784

Ray,

Your welcome!  Thanks for the grade, the points and the fun question.  I'm glad I could help.

If you do open a new question, especially if it is for help using PHP, cUrl and to get the results server side then feel free to post a comment here with the new question's URL.  I would like to look at it and see if I can help, if you would be interested in my help again.

If you do ask a new question on it and don't need the xml or csv for something else then don't feel you have to go through that step for PHP to use it.  It  could be an unneeded step. :)

bol

 

by: Ray_PaseurPosted on 2008-07-08 at 18:10:24ID: 21959808

bol:  Here's the new question:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_23548760.html

thanks for any help you can offer.  ~Ray

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...