Openlayers 选择适用于多个kml图层的功能

Openlayers 选择适用于多个kml图层的功能,openlayers,kml,Openlayers,Kml,我正在使用Openlayers和我自己的KML图层制作地图。我想通过弹出窗口从我的KML文件中访问属性-我设法做到了这一点,但问题是如何使多个KML层可选择,我只能选择最上层。我一直在寻找解决方案,但没有任何效果,我会非常小心地寻求帮助。我的代码是: var map; function init() { var options = { scales: [500, 1000, 2500, 5000, 10000],

我正在使用Openlayers和我自己的KML图层制作地图。我想通过弹出窗口从我的KML文件中访问属性-我设法做到了这一点,但问题是如何使多个KML层可选择,我只能选择最上层。我一直在寻找解决方案,但没有任何效果,我会非常小心地寻求帮助。我的代码是:

  var map;
  function init() {    
    var options = {   
                      scales: [500, 1000, 2500, 5000, 10000],
                      numZoomLevels: 26,
                      allOverlays: true,
                      projection: new OpenLayers.Projection("EPSG:900913"),
                      displayProjection: new OpenLayers.Projection("EPSG:4326"),
                      controls:[
                        new OpenLayers.Control.Navigation(),
                        new OpenLayers.Control.PanZoomBar(),
                        new OpenLayers.Control.LayerSwitcher({'ascending':false}), 
                        new OpenLayers.Control.ScaleLine(),
                        new OpenLayers.Control.MousePosition(),
                        new OpenLayers.Control.OverviewMap(),
                        new OpenLayers.Control.Attribution(),
                        new OpenLayers.Control.KeyboardDefaults()],};


    map = new OpenLayers.Map('map_element', options);  

    var gsat = new OpenLayers.Layer.Google(
    "Google Satellite",
    {type: google.maps.MapTypeId.SATELLITE} );
    map.addLayer(gsat);
    gsat.mapObject.setTilt(0);            

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

    map.addLayer(kml);     

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

    map.addLayer(vrstva_dve);

    select = new OpenLayers.Control.SelectFeature(kml); // here I tried ([kml, vrstva_dve]) , but still only layer kml is selectable 
            kml.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
            });
            map.addControl(select);
            select.activate();   


  }

  function onPopupClose(evt) {
        select.unselectAll();
  }

  function onFeatureSelect(event) {
            var feature = event.feature;
            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;
            }
  }   
var映射;
函数init(){
变量选项={
比例:[500、1000、2500、5000、10000],
numZoomLevels:26,
阿洛弗雷:是的,
投影:新OpenLayers.projection(“EPSG:900913”),
displayProjection:new OpenLayers.Projection(“EPSG:4326”),
控制:[
新建OpenLayers.Control.Navigation(),
新建OpenLayers.Control.PanZoomBar(),
新的OpenLayers.Control.LayerSwitcher({'ascending':false}),
新建OpenLayers.Control.ScaleLine(),
新建OpenLayers.Control.MousePosition(),
新建OpenLayers.Control.OverviewMap(),
新建OpenLayers.Control.Attribute(),
新建OpenLayers.Control.KeyboardDefaults()],};
map=newOpenLayers.map('map\u元素',选项);
var gsat=new OpenLayers.Layer.Google(
“谷歌卫星”,
{type:google.maps.MapTypeId.SATELLITE});
地图添加层(gsat);
gsat.mapObject.setTilt(0);
var kml=新的OpenLayers.Layer.Vector(“zahrada”{
投影:map.displayProjection,
策略:[新建OpenLayers.Strategy.Fixed()],
协议:新OpenLayers.protocol.HTTP({
url:“kml/zahrada.kml”,
格式:new OpenLayers.format.KML({
这是真的,
结论:正确,
})
})  
});
map.addLayer(kml);
var vrstva_dve=新的OpenLayers.Layer.Vector(“vrstva_dve”{
投影:map.displayProjection,
策略:[新建OpenLayers.Strategy.Fixed()],
协议:新OpenLayers.protocol.HTTP({
网址:“kml/vrstva_dve.kml”,
格式:new OpenLayers.format.KML({
这是真的,
结论:正确,
})
})  
});
地图添加层(vrstva_dve);
select=newopenlayers.Control.SelectFeature(kml);//我在这里尝试了([kml,vrstva_dve]),但仍然只有层kml是可选的
kml.events.on({
“featureselected”:在FeatureSelect上,
“featureunselected”:onFeatureUnselect
});
map.addControl(选择);
选择。激活();
}
函数onPopupClose(evt){
select.unselectAll();
}
功能onFeatureSelect(事件){
var-feature=event.feature;
var content=“”+feature.attributes.name+“”+feature.attributes.description;
如果(内容搜索(“使用:

并加上:

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

非常感谢您提供了如此快速而有用的答案,它正在发挥作用:-)
vrstva_dve.events.on({
  "featureselected": onFeatureSelect,
  "featureunselected": onFeatureUnselect
});