javascript – 在OpenLayers中使多个矢量图层可单击

我正在开发一个OpenLayers地图,它将同时显示多个KML图层.我希望能够点击任何图层中的某个功能,并弹出一个向我显示的信息.到目前为止,我只能点击最近添加的图层.如果我想点击之前添加的图层,我必须关闭之前添加的所有图层.显然这不太理想.到目前为止,这是我的代码
var select = [];
function addLayer(layerId,layerLink,layerColor)
{

    var kmlLayer = new OpenLayers.Layer.Vector("Layer_"+layerId,{
        strategies: [new OpenLayers.Strategy.Fixed()],protocol: new OpenLayers.Protocol.HTTP({
            url: layerLink,format: new OpenLayers.Format.KML({
        extractStyles: true,extractAttributes: true,maxDepth: 2
        })
    })
    });

    kmlLayer.events.on({
        "featureselected": onKMLSelect,"featureunselected": onKMLUnselect
    });

    select["Layer_"+layerId] = new OpenLayers.Control.SelectFeature(kmlLayer);
    map.addControl(select["Layer_"+layerId]);
    select["Layer_"+layerId].activate();  

    map.addLayer(kmlLayer);

}
function onKMLPopupClose(evt) {
    for(s in select)
    {
        select[s].unselectAll();
    }
}
function onKMLSelect(event) {
        var feature = event.feature;

    var content = "<h2>"+feature.attributes.name + "</h2>" + feature.attributes.description;
    popup = new OpenLayers.Popup.FramedCloud("chicken",feature.geometry.getBounds().getCenterLonLat(),new OpenLayers.Size(100,100),content,null,true,onKMLPopupClose);
    feature.popup = popup;
    map.addPopup(popup);
}
function onKMLUnselect(event) {
    var feature = event.feature;
    if(feature.popup) {
        map.removePopup(feature.popup);
        feature.popup.destroy();
        delete feature.popup;
    }
}

任何帮助将不胜感激.
谢谢,

解决方法

我刚才有同样的问题.你可以在 Openlayers ExamplesOpenLayers Select Feature on Multiple Layers Example找到一个很好的例子.

以下是代码的主要部分:

var map,selectControl;
function init(){
    map = new OpenLayers.Map('map');
    var wmsLayer = new OpenLayers.Layer.WMS(
        "OpenLayers WMS","http://vmap0.tiles.osgeo.org/wms/vmap0",{layers: 'basic'}
    ); 

    var vectors1 = new OpenLayers.Layer.Vector("Vector Layer 1");
    var vectors2 = new OpenLayers.Layer.Vector("Vector Layer 2");

    map.addLayers([wmsLayer,vectors1,vectors2]);
    map.addControl(new OpenLayers.Control.LayerSwitcher());

    selectControl = new OpenLayers.Control.SelectFeature(
        [vectors1,vectors2]
    );

    map.addControl(selectControl);
    selectControl.activate();

    map.setCenter(new OpenLayers.LonLat(0,0),3);

    vectors1.addFeatures(createFeatures());
    vectors2.addFeatures(createFeatures());


    vectors1.events.on({
        "featureselected": function(e) {
            showStatus("selected feature "+e.feature.id+" on Vector Layer 1");
        },"featureunselected": function(e) {
            showStatus("unselected feature "+e.feature.id+" on Vector Layer 1");
        }
    });
    vectors2.events.on({
        "featureselected": function(e) {
            showStatus("selected feature "+e.feature.id+" on Vector Layer 2");
        },"featureunselected": function(e) {
            showStatus("unselected feature "+e.feature.id+" on Vector Layer 2");
        }
    });
}

相关文章

事件冒泡和事件捕获 起因:今天在封装一个bind函数的时候,发现el.addEventListener函数支持第三个参数...
js小数运算会出现精度问题 js number类型 JS 数字类型只有number类型,number类型相当于其他强类型语言...
什么是跨域 跨域 : 广义的跨域包含一下内容 : 1.资源跳转(链接跳转,重定向跳转,表单提交) 2.资源...
@ &quot;TOC&quot; 常见对base64的认知(不完全正确) 首先对base64常见的认知,也是须知的必须有...
搞懂:MVVM模式和Vue中的MVVM模式 MVVM MVVM : 的缩写,说都能直接说出来 :模型, :视图, :视图模...
首先我们需要一个html代码的框架如下: 我们的目的是实现ul中的内容进行横向的一点一点滚动。ul中的内容...