OpenLayers点击选中的Feature触发功能

OpenLayers Click on selected Feature triggering function

本文关键字:Feature 功能 OpenLayers      更新时间:2023-09-26

我使用OpenLayers在地图上使用集群策略绘制点特征。

        strategy = new OpenLayers.Strategy.Cluster();
        clusters = new OpenLayers.Layer.Vector("Clusters", {
            strategies: [strategy],
            styleMap: new OpenLayers.StyleMap({
                "default": style,
                "select": {
                    fillColor: "#ff0000",
                    strokeColor: "#ffbbbb"
                }
            })
        });
        [.......]
        clusters.addFeatures(features);

我还使用SelectFeature来选择我的地图上的点特征。

        select = new OpenLayers.Control.SelectFeature(
                clusters, {
                    clickout: false,
                    toggle: false, 
                    hover: false
                }
            );
        map.addControl(select);
        select.activate();
        clusters.events.on({"featureselected": clickPoint});

当用户选择一个集群特征时,将出现一个弹出窗口,其中包含要选择的特征列表。当他选择其中一个时,弹出窗口关闭,聚类特征保持选中。

现在问题来了。我希望能够点击集群功能,使弹出框再次出现。我唯一能做的就是设置toggle:true,但随后该功能被取消选中。

是否有一种方法来触发一个事件,当用户点击一个选定的功能?

提前谢谢,困难的

为了解决这个问题,我将unselectAll覆盖为:

mySelectControl.unselectAll = function(options) {
    OpenLayers.Control.SelectFeature.prototype.unselectAll.apply(
                              mySelectControl, arguments);
    if (options && options.except) {
        var myReselecteFeature = options.except;
        ... your code to show the popup of myReselecteFeature ...
    }
};

你可能有兴趣看看这个例子:

http://jorix.github.com/OL-FeaturePopups/examples/feature-popups.html

它是一个控件,可以完成您所做的以及更多的操作。例如,在使用集群缩放后保留选择。

注意:默认行为不是你想要的,但可以自定义

您也可以在特性被选中时取消选择。对我来说,这是实现点击事件功能的最短途径。还可以将切换标志设置为true,以在单击时触发取消选择事件。

var pdfFeatureSelector = new OpenLayers.Control.SelectFeature(pdfLayer,{
        clickout: true,
        multiple: true,
        toggle: true,
        autoActivate: true,
        onSelect: function(){
            OpenLayers.Control.SelectFeature.prototype.unselectAll.apply(
                    pdfFeatureSelector);//unselect the feature when it is selected
        }
});