【问题标题】:OpenLayers not displaying kml layerOpenLayers 不显示 kml 层
【发布时间】:2013-05-16 14:02:35
【问题描述】:

我在使用 OpenLayers 显示 kml 图层时遇到问题。

这是我的 kml 文件,Light.kml:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Folder>
<open>1</open>
<visibility>1</visibility>
<GroundOverlay>
<name>2013-05-15 12:00:00Z</name>
<visibility>1</visibility>
<Icon><href>http://localhost/graphics/Light_0.jpg</href></Icon>
<LatLonBox>
<north>9.4896821975708</north>
<south>-66.0149154663086</south>
<east>197.33328247070312</east>
<west>90.69839477539062</west>
</LatLonBox>
<TimeStamp><when>2013-05-15T12:00:00Z</when></TimeStamp>
</GroundOverlay>
</Folder>
</kml>
</xml>

这里是 OpenLayers javascript 代码:

var map = new OpenLayers.Map({
    div: "map",
    layers: [
        new OpenLayers.Layer.WMS(
            "WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0",
            {layers: "basic"},
            {wrapDateLine: true}
        )
    ],
    center: new OpenLayers.LonLat(140,-30),
    zoom: 3
});

var light = new OpenLayers.Layer.Vector("Light", {
strategies: [new OpenLayers.Strategy.Fixed()], protocol: new OpenLayers.Protocol.HTTP({
                url: "graphics/Light.kml",
                format: new OpenLayers.Format.KML({
                    extractStyles: true,
                    extractAttributes: true
                })
            })
        });
map.addLayers([light]);

地图显示但未显示 kml 图层。有谁知道可能出了什么问题?

【问题讨论】:

    标签: javascript openlayers kml


    【解决方案1】:

    也许添加到您的代码中:

    light.setVisibility(true);
    map.addLayer(light);
    

    在我看来,您的代码中有错字,请仅尝试使用 map.addLayer。或者使用以下结构可以解决问题

    var map = new OpenLayers.Map({
        div: "map",
        layers: [
            new OpenLayers.Layer.WMS(
                "WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0",
                {layers: "basic"}
            ),
            new OpenLayers.Layer.Vector("KML", {
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "kml/lines.kml",
                    format: new OpenLayers.Format.KML({
                        extractStyles: true, 
                        extractAttributes: true,
                        maxDepth: 2
                    })
                })
            })
        ],
        center: new OpenLayers.LonLat(-112.169, 36.099),
        zoom: 11
    });
    

    【讨论】:

    • 嗨,Gery,该示例有效(一旦我获取了 kml 文件并将其放置在适当的目录中)。那么问题可能出在 Light.kml 文件上?
    • 嗨,Gery,kml 文件(您可以在上面看到的内容)是为 google earth 自动生成的。我需要改变什么才能让它工作吗?
    【解决方案2】:

    尝试删除策略:[new OpenLayers.Strategy.Fixed()]

    【讨论】:

    • 感谢 Gery,但不幸的是删除了策略:[new OpenLayers.Strategy.Fixed()] 不起作用。
    【解决方案3】:

    这个怎么样:

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://earth.google.com/kml/2.0">
        <Folder>
        <open>1</open>
        <visibility>1</visibility>
        <GroundOverlay>
        <name>2013-05-15 12:00:00Z</name>
        <visibility>1</visibility>
        <Icon><href>http://localhost/graphics/Light_0.jpg</href></Icon>
        <LatLonBox>
        <north>9.4896821975708</north>
        <south>-66.0149154663086</south>
        <east>197.33328247070312</east>
        <west>90.69839477539062</west>
        </LatLonBox>
        <TimeStamp><when>2013-05-15T12:00:00Z</when></TimeStamp>
        </GroundOverlay>
        </Folder>
    </kml>
    

    这对我的朋友有用吗?

    【讨论】:

    • 你改变了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 2011-11-12
    • 2020-12-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多