Friday, October 2, 2009

Mystery solved

I guess I had expected something more in the way of documentation for OpenLayers that would explain the behavior of the different cases I had created for trying to display my KML file on an OpenLayers map. But thanks to Scott Davis, who does a marvelous job of teaching "GIS for Web Developers", I now have an OpenLayers map that displays the KML in Firefox and in IE. Both browsers are consistent in handling the display for both the OpenLayers.Layer.Vector class and the OpenLayers.Layer.GML class. I can't say that I understand everything that's happening behind the scenes or which of the two classes is the preferred one to use. But I do understand a whole lot more about JavaScript and OpenLayers and troubleshooting than I did before.

"Due to security restrictions in Javascript, it is not possible to retrieve information from remote domains via an XMLHttpRequest. Classes like Layer.WFS and Layer.GeoRSS use XMLHTTPRequest to get their data." Those two statements from the OpenLayers FAQ page were the key to solving the problem. I think I had suspected that before taking Scott's class, but I suffer from a need to know exactly why things work the way they do, even in the most obscure details that most people don't care about. I wanted to see something in black and white that said if you are using the Layer.Vector and Layer.GML you need to have a proxy. The OpenLayers examples for KML don't show the proxy host variable being set like they do for the Layer.WFS examples. I guess I was being a bit laxy, too, and not wanting to create a servlet to make the call if I didn't have to.

But without getting into the gory details, Scott assured me that if I just accepted the rule that the HTML with the JavaScript must be in the same domain as the server from which the data is being retrieved my problems would go away. I bit the bullet and created a servlet to intercept the OpenLayers call, grab the data from the remote domain and push it back to OpenLayers. Now I know to always use a proxy. And I have a better proxy servlet than the first attempt at a servlet I made months ago when I was using the Layer.WFS class.

For anyone who is doing web development that involves GIS and has struggled with the learning curve, I highly recommend purchasing the book "GIS for Web Developers" by Scott Davis. And to really get a good foundation check out his class.