KML layer in Openlayers doesn't work on localhost

后端 未结 2 967
刺人心
刺人心 2021-01-03 04:07

I have strange problem with rendering KML file in OpenLayers. It seems to be easy, but it\'s not. I\'ve started with an example from here OpenLayers example . I wanted to ad

相关标签:
2条回答
  • 2021-01-03 04:28

    This thread is old but I know it will still be looked at.

    Try setting the projection when creating the KML layer. That worked for me.

        var kmllayer = new OpenLayers.Layer.Vector("KML", {
                projection: map.displayProjection,
    
    0 讨论(0)
  • 2021-01-03 04:29

    The reason that your local copy of sundials.kml won't render is that you're using OpenLayers.Protocol.HTTP, which won't load an address of the kind file://directory/file.kml.

    But I've no idea why your posted code doesn't render. I'm currently wrestling with the same problem. All I can tell you is what else doesn't work.

    The Openlayers documentation explains how a GML layer can be used to load a KML layer. Unfortunately, the code snippet they give in the documentation doesn't match their actual example, which uses a Vector layer just like most of their other KML (and GML) examples.

    I copied the example code anyway, but changed the Vector layer to var layer = new OpenLayers.Layer.GML("GML", "gml/polygon.xml");, as per the documentation. Then I made local copies of Openlayers.js, polygon.xml and the .css stylesheets, and changed the urls to point to my local copies. So everything is local.

    <!DOCTYPE html> 
    <html> 
        <head> 
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> 
            <meta name="apple-mobile-web-app-capable" content="yes"> 
            <title>GML Doesn't Render</title> 
            <link rel="stylesheet" href="defaultstyle.css" type="text/css"> 
            <link rel="stylesheet" href="examplesstyle.css" type="text/css"> 
            <script src="OpenLayers.js"></script> 
            <script type="text/javascript">
                var map;
    
                function init(){
                    map = new OpenLayers.Map('map');
                    var wms = new OpenLayers.Layer.WMS(
                        "OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0",
                        {layers: 'basic'}
                    );
    
                    var layer = new OpenLayers.Layer.GML("GML", "gml/polygon.xml");
    
                    map.addLayers([wms, layer]);
                    map.zoomToExtent(new OpenLayers.Bounds(
                        -3.92, 44.34, 4.87, 49.55
                    ));
                }
            </script> 
        </head> 
        <body onload="init()"> 
            <div id="map" class="smallmap"></div> 
        </body> 
    </html> 
    

    It doesn't work.

    I also tried the suggestion from the documentation:

    var layer = new OpenLayers.Layer.GML("KML", "kml/lines.kml", {
       format: OpenLayers.Format.KML,
       formatOptions: {
           'extractStyles': true
       }
    });
    

    It doesn't work.

    There is even a specific example of loading "locally stored GML vector data on a basemap". It uses the same code as the documentation, so, needless to say, it doesn't work. I can't hyperlink to it because I haven't enough reputation yet to make three hyperlinks, but you can find it if you search the examples for "GML".

    Openlayers has a security feature to make it difficult to load data and javascript from wildly different locations (and a workaround called "proxyhost" if you really have to do this). But I can't see how that would get involved when all the urls point to the localhost.

    Please be gentle, this is my first post!

    EDIT: It turns out that a) it's a browser security feature, not OpenLayers, and b) it doesn't allow a script on the localhost to load any other file on the localhost, even if you're offline. Chrome/Chromium has a command-line parameter -allow-file-access-from-files. Using this parameter, the above examples work. Other workarounds (Google them) seem tricky.

    0 讨论(0)
提交回复
热议问题