Openlayers-重新绘制图层时丢失选择或样式


10

当我缩放或移动地图时,将重新绘制图层。这就是我想要的,因为我分配了要立即加载的功能。这是代码:

    wfs = new OpenLayers.Layer.Vector("WFS", {
    strategies: [new OpenLayers.Strategy.BBOX({
                    resFactor: 1,
                    ratio:1
                })],
    protocol: new OpenLayers.Protocol.WFS({
              maxFeatures:1000,
              url:  myUrl,
              featureType: myFeatureType,
              featureNS: myFeaturedNS,
              version: "1.1.0"
              }),
      filter: setFilter(year, variant)
});

以下代码是我的选择代码。这一切都按我的意愿进行。但是,当重新绘制图层时,所选内容将丢失并且onFeatureUnselect不会触发(应该触发)。我不确定选择是否真的丢失或样式是否已重置。

    selectCtrl = new OpenLayers.Control.SelectFeature(
        wfs,
        {
            clickout: true, toggle: false,
            multiple: false, hover: false,
            box: true,
            toggleKey: "shiftKey", // ctrl key removes from selection
            multipleKey: "shiftKey",
            eventListeners: {
                featurehighlighted: onFeatureSelect,
                featureunhighlighted: onFeatureUnselect

            }
        }
    );

@geographika

我的onFeatureSelect和onFeatureUnselect函数:

function onFeatureSelect(e) {
var feature = e.feature.attributes;
var featureclone = e.feature.clone();

var style = {
    pointRadius: 10,
    fillOpacity:0,
    strokeColor: "#000000"
    };

featureclone.style = style;

layer2.addFeatures([featureclone]);
selectedFeatures[featureclone.attributes.receptor_id] = featureclone;
}

function onFeatureUnselect(e) {
    var feature = e.feature.attributes;
    layer2.removeFeatures(selectedFeatures[feature.receptor_id]);   
    delete selectedFeatures[feature.receptor_id];
}

您的onFeatureSelect函数中有什么?
geographika,

我已使用以下两个功能更新了帖子
Boedy 2011年

Answers:


4

我发现在缩放,平移和重绘中维护矢量选择的最简单方法是在地图上添加一个新的空矢量图层,并在选择要素时在该图层中放置一个副本。

您可以将选择图层样式设置为清晰可见的样式。

在选择工具突出显示功能中,在选择时添加功能的副本,然后在第二次选择时将其删除。

onFeatureSelect(feat){
  //check if the feature is in the select layer
  var cloned_feat = selectionLayer.getFeatureById(feat.id); 
  //or a getFeatureBy with a unique property

  if(cloned_feat){
      selectionLayer.removeFeatures([cloned_feat]);
  }
  else {
      var featCopy = feat.clone();
      this.sketchLayer.addFeatures([featCopy]);
  }
}

这略有简化(未经测试)-您可能希望将选择层存储为选择工具的属性。

还要检查getFeatureById函数-您可能必须在功能上使用其他唯一属性而不是ID。


谢谢回复。我认为这可能是一个很好的解决方案。我对某些代码不满意,但是我坚持认为,当选择丢失时,onFeatureUnselect事件不会触发。那么,在什么情况下我应该删除功能部件克隆?见我的原始帖子。
Boedy 2011年

@Boedy看到更新的答案。您可能应该在select函数中做所有事情。选择一次以选择,然后再次取消选择。
geographika,

非常感谢!检查特征是否在所选图层中有助于。现在,我只遇到样式问题,但是我敢打赌,我自己就能弄清楚这个问题。
Boedy 2011年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.