我在OpenLayers中呈现KML文件时遇到了奇怪的问题。这看起来很简单,但事实并非如此。我从这里的一个例子OpenLayers example开始。我想添加我自己的KML。它没有起作用。我已经使用绝对urls创建了示例的本地副本,如下所示:
<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>它不会渲染。我试图将KML更改为我本地的sundial.kml (url: "http://openlayers.org/dev/examples/kml/sundials.kml", -> url: "/sundials.kml )副本,但也不起作用。我不知道为什么。
最后,我想添加我自己的KML文件,但它们也不显示。我该怎么办?
提前感谢拉法尔
发布于 2011-09-10 15:49:28
您的本地sundials.kml副本不能呈现的原因是您使用的是OpenLayers.Protocol.HTTP,它不会加载file://directory/file.kml类型的地址。
但是我不知道为什么你发布的代码不能被渲染。我现在也在努力解决同样的问题。我能告诉你的就是其他不起作用的东西。
Openlayers documentation解释了如何使用GML层来加载KML层。不幸的是,他们在文档中提供的代码片段与他们实际的example不匹配,后者使用向量层,就像他们的大多数其他KML (和GML)示例一样。
我复制了示例代码,但根据文档将向量层更改为var layer = new OpenLayers.Layer.GML("GML", "gml/polygon.xml");。然后,我制作了Openlayers.js、polygon.xml和.css样式表的本地副本,并将urls更改为指向本地副本。所以一切都是本地化的。
<!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> 它不起作用。
我还尝试了文档中的建议:
var layer = new OpenLayers.Layer.GML("KML", "kml/lines.kml", {
format: OpenLayers.Format.KML,
formatOptions: {
'extractStyles': true
}
});它不起作用。
甚至有一个加载“在底图上本地存储的GML矢量数据”的具体示例。它使用与文档相同的代码,因此,不用说,它不起作用。我不能超链接到它,因为我还没有足够的声誉来制作三个超链接,但你可以找到它,如果你搜索"GML“的例子。
Openlayers有一个安全特性,使得从非常不同的位置加载数据和javascript变得困难(如果你真的必须这样做,还有一个称为"proxyhost“的变通办法)。但是当所有的urls都指向本地主机时,我不明白这会涉及到什么。
请温文点,这是我的第一篇文章!
编辑:事实证明,a)这是一个浏览器安全特性,而不是OpenLayers;b)它不允许本地主机上的脚本加载本地主机上的任何其他文件,即使您处于离线状态。Chrome/Chromium有一个命令行参数-allow-file-access-from-files。使用此参数,上面的示例可以正常工作。其他的变通方法(谷歌搜索)似乎很棘手。
发布于 2015-01-23 03:14:26
这个帖子是旧的,但我知道它仍然会被关注。
尝试在创建KML图层时设置投影。这对我很管用。
var kmllayer = new OpenLayers.Layer.Vector("KML", {
projection: map.displayProjection,https://stackoverflow.com/questions/7331997
复制相似问题