I'm pretty new to the Google Maps API and I was wondering if it is possible to tap into the driving directions feature taht is available on Google Maps.
Not directly through the API, although I've heard talk of submitting a query to the normal Google Maps engine, then parsing out the points and putting them on your map. Haven't seen any examples yet, but I think it'd be cool! (Though there may be a serious Terms of Use issue in doing an automated query on Google Maps).
> I'm pretty new to the Google Maps API and I was wondering if it is > possible to tap into the driving directions feature taht is available > on Google Maps.
I have actually figured this out and now have a map with overlayed route and the driving directions. As an added feature it also shows travel time per segment (something that normal google maps does not do) and travel time for the trip.
Here is my solution...
First of all I assume people only want to go from an address to the locations in my database. Each location in my database has an associated lattitude and longitude. I take advantage of the fact that google permits directions from coordinates to an address and vice versa. I first create a form in in the bubble that pops up to collect the destination address (note the target must be _parent if working in an i frame), and then I place hidden form fields that contain the latt and long to silently pass this information forward to a directions page. Then I created a new xml file that grabs the google JS file for directions then splits it up at specific tags and places its contents in the approperiate places using PHP scripting, becasue the nature of google drawing the line on the road and placing the start/end markers is solely dependent on an XML file.
The google JS file is generated by the url: http://maps.google.com/maps?saddr=[ADD1]&daddr=[ADD2]&hl=en where [ADD1] and [ADD2] are the address (or coma seperated latt/long points) and [ADD1] is the starting address and [ADD2] is the destination address. Using forms and PHP it is rather easy to auto generate this URL then read the contents back to a string then work from there. The important part is the XML file that the JS contains. This is the magic key for directions. It contains all the needed information and the code for the map.
I then place this page in an iframe in my main page which collects the posted info from the original bubble form then passes it to the iframe in the src attribute used to call an iframel which I then cause the xml to recieve the variables via a GET request.
My approach has this file captured twice, once for generating the map and second time for generating the driving directions, since the strings of data are too long and create problems passing via GET.
I then re grab the google JS file and then begin to split it apart as an array looking for the segment tags which are of course delieved as XML values and attributes.
Using DOM XML get element I'm able to extract the info from the given xml then using nested while statments I parse it back to the user .
In furhter testing I discovered from a friend who uses IE that the map with line drawing broke in windows IE. I've tried adding the stlyle elements and adding the VML attributes to the html tag, but I'm still stumped
Here is an example of my iframe map window, this URL will work in Firefox but not in IE
Due to this problem with IE I have not implemented a full usage of this, and therefore have limited access to this function only when one searches by zip code and then clicks on the map location link, then the directions text box opens up. http://www.lutheranstudentfellowship.org/zipsearch.php
I admit this approach is a bit clumsy and it breaks in IE. If anyone would be interested I would be willing to post my code snippets and perhaps some explanation behind it.
No time to look at the source tonight, but could you clarify something for me: Is the final map you see in the iframe (i.e. the one showing the route) a custom Google map, or is it fed from maps.google.com? i.e. how easy would it be to add other markers to the final map?
Sorry to bombard you with posts. I did poke at your source code a little afterall. Correct me if I'm wrong, but it's not using the API, right? I'd love to hear your thoughts on API-ifying this.
That's very nice. What are you doing for the different levels? I noticed that overlay line works well up to a point, but then it starts missing intermediate points.
robert wrote: > I have actually figured this out and now have a map with overlayed > route and the driving directions. As an added feature it also shows > travel time per segment (something that normal google maps does not do) > and travel time for the trip.
> Here is my solution...
> First of all I assume people only want to go from an address to the > locations in my database. Each location in my database has an > associated lattitude and longitude. I take advantage of the fact that > google permits directions from coordinates to an address and vice > versa. I first create a form in in the bubble that pops up to collect > the destination address (note the target must be _parent if working in > an i frame), and then I place hidden form fields that contain the latt > and long to silently pass this information forward to a directions > page. Then I created a new xml file that grabs the google JS file for > directions then splits it up at specific tags and places its contents > in the approperiate places using PHP scripting, becasue the nature of > google drawing the line on the road and placing the start/end markers > is solely dependent on an XML file.
> The google JS file is generated by the url: > http://maps.google.com/maps?saddr=[ADD1]&daddr=[ADD2]&hl=en > where [ADD1] and [ADD2] are the address (or coma seperated latt/long > points) > and [ADD1] is the starting address and [ADD2] is the destination > address. Using forms and PHP it is rather easy to auto generate this > URL then read the contents back to a string then work from there. The > important part is the XML file that the JS contains. This is the magic > key for directions. It contains all the needed information and the code > for the map.
> I then place this page in an iframe in my main page which collects the > posted info from the original bubble form then passes it to the iframe > in the src attribute used to call an iframel which I then cause the > xml to recieve the variables via a GET request.
> My approach has this file captured twice, once for generating the map > and second time for generating the driving directions, since the > strings of data are too long and create problems passing via GET.
> I then re grab the google JS file and then begin to split it apart as > an array looking for the segment tags which are of course delieved as > XML values and attributes.
> Using DOM XML get element I'm able to extract the info from the given > xml then using nested while statments I parse it back to the user .
> In furhter testing I discovered from a friend who uses IE that the map > with line drawing broke in windows IE. I've tried adding the stlyle > elements and adding the VML attributes to the html tag, but I'm still > stumped
> Here is an example of my iframe map window, this URL will work in > Firefox but not in IE
> Due to this problem with IE I have not implemented a full usage of > this, and therefore have limited access to this function only when one > searches by zip code and then clicks on the map location link, then the > directions text box opens up. > http://www.lutheranstudentfellowship.org/zipsearch.php
> I admit this approach is a bit clumsy and it breaks in IE. If anyone > would be interested I would be willing to post my code snippets and > perhaps some explanation behind it.
I've found a way to API-ify this... Using just the output XML file and writing the polylines to the map using the API. The only problem most people have experienced is that the XML file comes back with the Polylines' coordinates encrypted. To solv this problem, I *borrowed* some code from Google's main map site to decrypt the XML data. look for the function named decodePolyline in "maps.11.js". It takes a little modifying, but you can get it to decode for yourself. I'm sure Google ment for this to help keep people from using their mapping engine, as it probably would bog down thier servers if a lot of people started using for driving directions.... But in the name of open source... they didn't try too hard. Very interesting thing though is that you can accomplish a server-side geocoding tool for handeling coordinates. A project that I am working on uses the "refinement" tag to automatically fix the driving directions... I love google though, so I'm not going to blatently show how to do this... All I can say is that it can be done.. I've done it, and google provides you more than enough info on how to do it. Good luck. BTW... mine works fine in IE,FireFox.... Google shows you how to do this if you read the API.
Here's another example for you to check out. I am concidering the idea of replacing the caching engine I put in place which caches the XML with a direct browser request for the XML from Google. Would eliminate any chance of it being considered "automated querying", but it also puts more load on Google's site as I wouldn't be caching queries.
Another thing, if they didn't want us doing that, why did they put the functions in the API which directly operate on the XML from that packet. I will leave the functions left unsaid as they aren't documented (yet?), but I am sure anyone who wants to find them can easily do so like I did.
What's the easiest way to get a list of lat/long coordinates for a planned route?
I only need to generate 4 or 5 routes so programatically creating something to get the routes seems like too much work. I just want to be able to go to google's driving directions, punch in my start and end locations, hit get directions, then get a file from there to parse through for the lat/longs.
What file do I need to get after I enter the directions in google's search engine?
Well I put the directions in the google direction finder. Got the xml from there, and the points "polyline" string.
Went through it with my modified "decodePolyline" method borrowed from maps.11.js, and I get an output of alternating lat/longs for my route. The list is sorted by longitude, which makes sense because the route I chose spans the country from west to east.
Only problem is that the lats are not correct, even though all the longs are perfect.
In the decodePolyline method, the lats are decoded first, and then the longs. I can't see what I'm doing wrong. The method is so clear and simple, and it does the same exact set of operations for both the lat and the long.
If I were doing something like that, I guess I'd build a temporary map
page that helped me to construct it.
If I could get addListener to work properly for "click" on the base map,
then I'd use that, otherwise I'd "borrow" considerable chunks of code
from <http://www.sueandpaul.com/gmapPedometer/> and use doubleclicks.
This temporary map would have a side panel to display a list of all the
long/lat co-ordinates that were clicked, in a format suitable for
copying and pasting into a Polyline (or an XMaps Polyline). I'd zoom
right in, click along the roads and grab a reasonably large chunks of
Polyline data at a time.
I'd grab the locations of individual topographical features along the
route in the same way, and mark them with different customised icons.