views:

309

answers:

1

Hello All,

I would like to get driving time information from one location to another using google maps.

I have found another fantastic post on this site here

Although the above link is based on geo codes I woud like to use zip codes if possible.

But I really like to get the time via a php call as this time is being worked out in the backend and not for customer show. Can I do this type of request via HTTP Request ?

Really hope someone can point me in right direction.

Thank you in Advanced if you can help.

+1  A: 

For e.g

FROM ZIP: 98010

TO ZIP: 98011

just issue this HTTP Request:

http://maps.google.com/maps?saddr=98010&daddr=98011&output=html&oi=nojs

Then parse the response for a div with class=dditd and id=dditd and from this div you can extract all the information needed (distance, traveltime, traveltime in traffic (only sometimes given). This could be done via RegEx or XPath or whatever you want.

Once you got the information you must still transform the "textual representation" (e.g. 50mins / 1hour 12 mins / 1day 1hour / ...) into a number if needed for a e.g. DB-field.

Sample what the div looks like for the above query.

<div class=dditd id=dditd>
  <div>
     <b>38.1&#160;mi</b>
     &#8211; about
     <b>53 mins</b>
     <span class=traffictime>
       &nbsp;(up to 1 hour 30 mins in traffic)
     </span>
  </div>
</div>

But keep in mind that the given times are not really accurate as a ZIP code can be a rather large geographic area and I don't know from which point to which google then really measures the time.

So the times given could be off a varying amount depending what the real starting and destination points are.

jitter
Be aware that hacking Google Maps like this is against Google's Terms. Since it's a not supported part of the API Google could well change things without warning that would break that strategy. They have made changes in the past that broke other similar hacks.
Mike Williams