8

我正在制作一个将同时显示多个 KML 图层的 OpenLayers 地图。我希望能够单击任何图层中的功能并弹出一个显示我一些信息的窗口。到目前为止,我只能单击最近添加的图层。如果我想点击之前添加的图层,我必须关闭之前添加的所有图层。显然,这不太理想。到目前为止,这是我的代码:

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

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

4

2 回答 2

10

不久前我遇到了同样的问题。您可以从Openlayers 示例中找到很好的示例:OpenLayers 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");
        }
    });
}
于 2012-02-05T20:49:58.637 回答
1

这就是我所做的,以使 CHOSEN 图层上的所有功能成为可选调用弹出窗口:

var selectStop = new OpenLayers.Control.SelectFeature([layerKMLClient, layerKMLStops,  layerKMLTarget],{onSelect: onFeatureSelect, onUnselect: onFeatureUnselect});
    layerKMLStops.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
            });
    layerKMLClient.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
            });
    layerKMLTarget.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
            });
    map.addControl(selectStop);
    selectStop.activate(); 

请注意,我有多个其他层(主要来自 KML 文件,但也有一些来自 txt 文件的矢量图层),其特征是不可选择的。您可以通过为每种图层类型自定义 onFeatureSelect 来更改您的行为。

奖励:如果您决定cluster strategy在(某些)层上使用(相信我,您会在某个时间点使用)- 在您的onFeatureSelected函数语句中包括检查 feature.cluster 是否为真:

function onFeatureSelect(event) {
   var feature = event.feature;
   if (feature.cluster) {
于 2013-03-10T14:10:39.833 回答