Openlayers中的KML图层在本地主机上不起作用 [英] KML layer in Openlayers doesn't work on localhost

查看:113
本文介绍了Openlayers中的KML图层在本地主机上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OpenLayers中渲染KML文件时遇到了一个奇怪的问题。这似乎很容易,但事实并非如此。我已经从这里开始了一个例子 OpenLayers示例。我想添加自己的KML。它没有工作。我已经创建了绝对网址示例的本地副本,如下所示:

 < html> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< meta name =viewportcontent =width = device-width,initial-scale = 1.0,maximum-scale = 1.0,user-scalable = 0>
< meta name =apple-mobile-web-app-capablecontent =yes>
< link rel =stylesheethref =http://openlayers.org/dev/theme/default/style.csstype =text / css/>
< link rel =stylesheethref =http://openlayers.org/dev/examples/style.csstype =text / css/>
< script src =http://openlayers.org/dev/OpenLayers.js>< / script>
< style type =text / css>
html,body {
height:100%;
}
#map {
width:100%;
身高:80%;
border:1px纯黑色;
}
.olPopup p {margin:0px; font-size:.9em;}
.olPopup h2 {font-size:1.2em; }
< / style>
< script type =text / javascript>


var lon = 5;
var lat = 40;
var zoom = 5;
var map,select;

函数init(){
map = new OpenLayers.Map('map');

var wms = new OpenLayers.Layer.WMS(
OpenLayers WMS,
http://vmap0.tiles.osgeo.org/wms/vmap0,
{layers:'basic'}
);

var sundials = new OpenLayers.Layer.Vector(KML,{
projection:map.displayProjection,
strategies:[new OpenLayers.Strategy.Fixed()],
协议:新的OpenLayers.Protocol.HTTP({
url:http://openlayers.org/dev/examples/kml/sundials.kml,
格式:new OpenLayers.Format .KML({
extractStyles:true,
extractAttributes:true
})
})
});

map.addLayers([wms,sundials]);
警报(已损毁);
select = new OpenLayers.Control.SelectFeature(sundials);

sundials.events.on({
featureselected:onFeatureSelect,
featureunselected:onFeatureUnselect
});

map.addControl(select);
select.activate();
map.zoomToExtent(new OpenLayers.Bounds(68.774414,11.381836,123.662109,34.628906));
}
函数onPopupClose(evt){
select.unselectAll();
}
function onFeatureSelect(event){
var feature = event.feature;
//由于KML是用户生成的,因此对
// Javascript进行天真的保护。
var content =< h2>+ feature.attributes.name +< / h2> + feature.attributes.description;
if(content.search(< script)!= -1){
content =包含Javascript的内容!下面的转义内容< br> + content.replace(/< / g,& lt;);
}
popup = new OpenLayers.Popup.FramedCloud(chicken,
feature.geometry.getBounds()。getCenterLonLat(),
OpenLayers.Size(100,100),
content,
null,true,onPopupClose);
feature.popup = popup;
map.addPopup(popup);
}
function onFeatureUnselect(event){
var feature = event.feature;
if(feature.popup){
map.removePopup(feature.popup);
feature.popup.destroy();
删除feature.popup;
}
}
< / script>
< / head>
< body onload =init()>
< h1 id =title> KML图层示例< / h1>

< div id =tags>
kml,popup,功能
< / div>

< p id =shortdesc>
演示如何在底图上加载和显示KML文件。
< / p>

< div id =mapclass =smallmap>< / div>

< div id =docs>< / div>
< / body>
< / html>

它不渲染。我尝试将KML网址更改为本地副本sundial.kml( url:http://openlayers.org/dev/examples/kml/sundials.kml, - > url:/



最后,我想添加自己的KML文件,但他们不显示,我应该怎么办?



在此先感谢
Rafal

解决方案

sundials.kml 本地副本不会呈现的原因是您正在使用 OpenLayers.Protocol.HTTP ,它不会加载类型为 file://directory/file.kml 的地址。



但是我不知道为什么你的发布代码没有渲染,我现在正在面临同样的问题,我可以告诉你的是什么都不起作用。



Openlayers文档解释了如何使用GML层来加载一个KML层,不幸的是,他们在文档中给出的代码片段与他们的实际示例,它使用Vector层,就像大多数其他KML(和GML)示例一样。



无论如何我都复制了示例代码,但根据文档将Vector层更改为 var layer = new OpenLayers.Layer.GML(GML,gml / polygon.xml); 。然后我制作了Openlayers.js,polygon.xml和.css样式表的本地副本,并将url更改为指向我的本地副本。所以一切都是本地的。

 <!DOCTYPE html> 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< meta name =viewportcontent =width = device-width,initial-scale = 1.0,maximum-scale = 1.0,user-scalable = 0>
< meta name =apple-mobile-web-app-capablecontent =yes>
< title> GML不呈现< / title>
< link rel =stylesheethref =defaultstyle.csstype =text / css>
< link rel =stylesheethref =examplesstyle.csstype =text / css>
< script src =OpenLayers.js>< / script>
< script type =text / javascript>
var map;

函数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 =mapclass =smallmap>< / div>
< / body>
< / html>

无效。

我也尝试了文档中的建议:

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

无效。

甚至还有一个将本地存储的GML矢量数据加载到底图上的特定示例。它使用与文档相同的代码,所以不用说,它不起作用。我无法超链接到它,因为我还没有足够的声望制作三个超链接,但是如果您搜索示例中的GML,就可以找到它。



Openlayers具有安全功能,可以很难从非常不同的位置加载数据和JavaScript(如果真的必须这样做,则需要一个名为proxyhost的解决方法)。但我不明白,当所有的URL指向本地主机时会如何。



请温和,这是我的第一篇文章!



编辑:事实证明,a)这是一个浏览器安全功能,而不是OpenLayers,和b)它不允许本地主机上的脚本加载本地主机上的任何其他文件,甚至如果你离线。 Chrome / Chromium具有命令行参数-allow-file-access-from-files。使用这个参数,上面的例子工作。其他解决方法(谷歌他们)似乎棘手。


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 add my own KML. It didn't work. I've created a local copy of the example with absolute urls, as follows:

<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">
    <link rel="stylesheet" href="http://openlayers.org/dev/theme/default/style.css" type="text/css" />
    <link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css" />
    <script src="http://openlayers.org/dev/OpenLayers.js"></script>            
    <style type="text/css">
        html, body {
            height: 100%;
        }
        #map {
            width: 100%;
            height: 80%;
            border: 1px solid black;
        }
        .olPopup p { margin:0px; font-size: .9em;}
        .olPopup h2 { font-size:1.2em; }
    </style>
    <script type="text/javascript">


        var lon = 5;
        var lat = 40;
        var zoom = 5;
        var map, select;

        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 sundials = new OpenLayers.Layer.Vector("KML", {
                projection: map.displayProjection,
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "http://openlayers.org/dev/examples/kml/sundials.kml",
                    format: new OpenLayers.Format.KML({
                        extractStyles: true,
                        extractAttributes: true
                    })
                })
            });

            map.addLayers([wms, sundials]);
            alert("deded");
            select = new OpenLayers.Control.SelectFeature(sundials);

            sundials.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
            });

            map.addControl(select);
            select.activate();   
            map.zoomToExtent(new OpenLayers.Bounds(68.774414,11.381836,123.662109,34.628906));
        }
        function onPopupClose(evt) {
            select.unselectAll();
        }
        function onFeatureSelect(event) {
            var feature = event.feature;
            // Since KML is user-generated, do naive protection against
            // Javascript.
            var content = "<h2>"+feature.attributes.name + "</h2>" + feature.attributes.description;
            if (content.search("<script") != -1) {
                content = "Content contained Javascript! Escaped content below.<br>" + content.replace(/</g, "&lt;");
            }
            popup = new OpenLayers.Popup.FramedCloud("chicken", 
                                     feature.geometry.getBounds().getCenterLonLat(),
                                     new OpenLayers.Size(100,100),
                                     content,
                                     null, true, onPopupClose);
            feature.popup = popup;
            map.addPopup(popup);
        }
        function onFeatureUnselect(event) {
            var feature = event.feature;
            if(feature.popup) {
                map.removePopup(feature.popup);
                feature.popup.destroy();
                delete feature.popup;
            }
        }
    </script>
  </head>
  <body onload="init()">
      <h1 id="title">KML Layer Example</h1>

      <div id="tags">
          kml, popup, feature
      </div>

      <p id="shortdesc">
          Demonstrates loading and displaying a KML file on top of a basemap.
    </p>

    <div id="map" class="smallmap"></div>

    <div id="docs"></div>
  </body>
</html>

It doesn;t render. I tried to change KML url to my local copy of sundial.kml (url: "http://openlayers.org/dev/examples/kml/sundials.kml", -> url: "/sundials.kml, and it didn't work either. I don't have Idea why.

Finally I want to add my own KML files, but they do not show either. What should I do?

Thanks in advance Rafal

解决方案

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.

这篇关于Openlayers中的KML图层在本地主机上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆