[英]KML layer in Openlayers doesn't work on localhost
I have strange problem with rendering KML file in OpenLayers. 我在OpenLayers中渲染KML文件时遇到了奇怪的问题。 It seems to be easy, but it's not. 这似乎很容易,但事实并非如此。 I've started with an example from here OpenLayers example . 我从这里的OpenLayers示例开始 。 I wanted to add my own KML. 我想添加自己的KML。 It didn't work. 它没用。 I've created a local copy of the example with absolute urls, as follows: 我已经使用绝对URL创建了示例的本地副本,如下所示:
<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, "<");
}
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. 我试图将KML url更改为我的sundial.kml本地副本( url: "http://openlayers.org/dev/examples/kml/sundials.kml", -> url: "/sundials.kml
,它没有也没办法。我没有想法为什么。
Finally I want to add my own KML files, but they do not show either. 最后我想添加自己的KML文件,但它们也没有显示。 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
. 您的sundials.kml
本地副本无法呈现的原因是您使用的是OpenLayers.Protocol.HTTP
,它不会加载类型file://directory/file.kml
的地址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. Openlayers文档解释了如何使用GML图层加载KML图层。 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. 不幸的是,他们在文档中提供的代码片段与他们的实际示例不匹配,后者使用Vector层就像大多数其他KML(和GML)示例一样。
I copied the example code anyway, but changed the Vector layer to var layer = new OpenLayers.Layer.GML("GML", "gml/polygon.xml");
我无论如何都复制了示例代码,但将Vector层更改为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. 然后我制作了Openlayers.js,polygon.xml和.css样式表的本地副本,并将URL更改为指向我的本地副本。 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". 甚至存在将“本地存储的GML矢量数据加载到底图上”的特定示例。 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". 我无法超链接到它,因为我还没有足够的声誉来制作三个超链接,但如果你搜索“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). Openlayers具有安全功能,使得从异常不同的位置加载数据和javascript变得困难(如果你真的必须这样做,则称为“proxyhost”的解决方法)。 But I can't see how that would get involved when all the urls point to the localhost. 但是,当所有网址指向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. 编辑:事实证明,a)它是一个浏览器安全功能,而不是OpenLayers,和b)它不允许localhost上的脚本加载localhost上的任何其他文件,即使您处于脱机状态。 Chrome/Chromium has a command-line parameter -allow-file-access-from-files. Chrome / Chromium有一个命令行参数-allow-file-access-from-files。 Using this parameter, the above examples work. 使用此参数,上述示例有效。 Other workarounds (Google them) seem tricky. 其他解决方法(谷歌他们)似乎很棘手。
This thread is old but I know it will still be looked at. 这个帖子已经过时但我知道它仍然会被看到。
Try setting the projection when creating the KML layer. 尝试在创建KML图层时设置投影。 That worked for me. 这对我有用。
var kmllayer = new OpenLayers.Layer.Vector("KML", {
projection: map.displayProjection,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.