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.

3 comments:

Anonymous said...

Hi there,
My name is Andres Deparis and I am new in openlayers. What I do not understand is that I have put everything in the same folder in my domain; the root. I still get the same error. Localhost will never work for me then if I use a different domain?
In my case if I have something like www.AnyDomain.com for examples, would that mean tht I will never be able to put my kml files into my map in OpenLayers?
Thanks.

Barbara said...

You can put your kml files on your open layers map when your kml files are on localhost. What you have to do is create a proxy--it's sort of a way that you fool the browser into thinking that the files are coming from a different domain. It's explained on the Open Layers website http://trac.osgeo.org/openlayers/wiki/FrequentlyAskedQuestions#ProxyHost but they explain it assuming you use CGI scripts. I was doing my work using Java so I created a proxy host as a servlet.

Anonymous said...

Hello Barbara,

Thank you for your reply. After a few days of trying hard to solve the problem, I have decided to to step back for a while. After reading other articles, I have found many people with the same problem; using KML files. I will give it a break for now. This is just for my personal knowledge, no specific project ahead.

I will keep you posted as soon as I have it figured out.

Cheers again!
Andres
======