Reputation: 20934
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 ?
Upvotes: 1
Views: 1529
Reputation: 54605
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 mi</b>
– about
<b>53 mins</b>
<span class=traffictime>
(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.
Upvotes: 1