Parsing XML from a returned file.
Posted: Mon Jan 21, 2008 11:07 am
I'm currently working on a large-scale PHP & MySQL application for a shipping company. One of the features of the application is a function that will find the driving distance between two points, with "driving" being the key word. There's numerous utilities that will find the distance "as the crow flies" (a straight line), but the client needs to know driving distance.
I thought doing it with the Google Maps API, but since the application will only be visible for registered users (employees, customers, etc.), I'd have to go with the Enterprise edition, and I'm looking for a free solution. (I suppose I could just go with the regular API and hope Google never checks up on it but I'd rather avoid that issue.) I did find a way to get distance data directly from Google Maps without using the Maps API.
For example, if you are doing a search for the driving distance between New York City, NY (10007) and New Haven, CT (06511) you can plug in the two ZIP codes (or full addresses) into the following url:
http://maps.google.com/maps?q=from+1000 ... output=kml
which return a .kml file. The .kml extension is an XML format used with Google Earth. The distance that is returned with the file can be found in the following code block:
In older versions of this code, the tags were a bit different but the data contained in the CDATA (before the <br/> tag) was identical.
Ideally, I'd like to set up a search function where the user types in two addresses (or chooses them from the database) and then this script will calculate the driving distance by parsing through the file that is returned for the text "Distance: ([0-9\.,]) ([a-z\.])".
Does anyone have any ideas how to do this?
Thanks,
Will
I thought doing it with the Google Maps API, but since the application will only be visible for registered users (employees, customers, etc.), I'd have to go with the Enterprise edition, and I'm looking for a free solution. (I suppose I could just go with the regular API and hope Google never checks up on it but I'd rather avoid that issue.) I did find a way to get distance data directly from Google Maps without using the Maps API.
For example, if you are doing a search for the driving distance between New York City, NY (10007) and New Haven, CT (06511) you can plug in the two ZIP codes (or full addresses) into the following url:
http://maps.google.com/maps?q=from+1000 ... output=kml
which return a .kml file. The .kml extension is an XML format used with Google Earth. The distance that is returned with the file can be found in the following code block:
Code: Select all
<description>
<![CDATA[Distance: 82 mi (about 1 hour 54 mins)<br/>Map data ©2008 Tele Atlas ]]>
</description>Ideally, I'd like to set up a search function where the user types in two addresses (or chooses them from the database) and then this script will calculate the driving distance by parsing through the file that is returned for the text "Distance: ([0-9\.,]) ([a-z\.])".
Does anyone have any ideas how to do this?
Thanks,
Will