OpenLayers not displaying kml layer

我与影子孤独终老i 提交于 2019-12-24 00:37:19

问题


I am having problems displaying a kml layer using OpenLayers.

Here is my kml file, 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>

And here is the OpenLayers javascript code:

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]);

The map shows yet the kml layer is not displaying. Does anyone know what might be wrong?


回答1:


Perhaps adding to your code:

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

It seems to me that you have a typo in your code, try only with map.addLayer. Or using the following structure could do the trick

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
});



回答2:


try deleting strategies: [new OpenLayers.Strategy.Fixed()]




回答3:


how about this one:

<?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>

would that work my friend?



来源:https://stackoverflow.com/questions/16589573/openlayers-not-displaying-kml-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!