2013-03-10 39 views
3

我正在使用Openlayers和我自己的KML图层制作地图。我想通过弹出窗口访问我的KML文件中的属性 - 我管理的是,但问题是如何使多个KML图层可选,我只能选择最上层。我一直在寻找解决方案,但没有任何工作,我会很帮忙的。我的代码是:SelectFeature可用于多个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; 
      } 
    } 

回答

4

用途:

select = new OpenLayers.Control.SelectFeature([kml, vrstva_dve]) 

,并添加:

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

非常感谢你对这样一个快速和有用的答案,it's工作:-) – pavli 2013-03-10 14:55:21