[GIS] Problem displaying OpenLayers KML layer in IE

openlayers-2

I am serving the following files from a the same directory on localhost:

map.html
OpenLayers.js
output.kml

map.html simply creates an OpenLayers map and displays the KML file as per the OpenLayers example. I access it via http://localhost:8080/map.html and it all works fine in Firefox and Chrome but not in IE. In IE I get the navigation buttons but no map. The OpenLayers example does work in IE…

The contents of map.html are shown below:

<html>
<head>
<title>Map</title>
<script src="OpenLayers.js"></script>
</head>
<body>
<div style="width: 100%; height: 100%" id="map"></div>
<script defer="defer" type="text/javascript">      

var map = new OpenLayers.Map('map');

  <!-- Add background map -->  
    var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
    map.addLayer(wms);

    <!-- Add kml layer -->  
    var kml = new OpenLayers.Layer.Vector("KML", {
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: "output.kml",
            format: new OpenLayers.Format.KML({
                extractStyles: true, 
                extractAttributes: true,
                maxDepth: 2
            })
        })
    });
    map.addLayer(kml);

    <!-- Zoom -->
    kml.events.register("loadend", kml, function (e) {
    map.zoomToExtent(kml.getDataExtent());
    });

    </script>
</body>
</html>

Any ideas?

Best Answer

I think you might have an error in the javascript code which is only triggered by IE. To better understand what's going on in IE I recommend you install the Developer Toolbar, available as a free add-on from Microsoft:

http://www.microsoft.com/en-us/download/details.aspx?id=18359

IE9 should already have it installed by default.

After installing the IE Developer Toolbar open it by pressing F12 and then look for errors in the javascript console.

Alternatively you can comment out portions of code starting from kml.events.register and see if that fixes it.

Updated after comment

IE chokes because the web server does not return application/xml as the content type. Use the dev toolbar to confirm (inspect network traffic).

To fix configure files with .kml extension as application/xml into your web server configuraion.