【问题标题】:How to select a feature programmatically on a vector layer in OpenLayers?如何在 OpenLayers 的矢量图层上以编程方式选择特征?
【发布时间】:2012-01-12 08:32:23
【问题描述】:

我目前正在寻找在 OpenLayers.Layer.Vector 中选择(或突出显示)矢量的解决方案。

我已经构建了一个简单的网格表,用户可以在其中选择一个向量(以 WKT 格式的字符串形式给出),该向量应该突出显示图层上的相应向量。用户访问网站时,gridtable 中的所有矢量都会绘制到地图上的矢量图层。

我发现我需要 OpenLayers.Control.ModifyFeatureselectFeature(feature) 函数或 OpenLayers.Control.SelectFeature(请参阅 dev.openlayers.org/apidocs/files/OpenLayers/Control/SelectFeature-js.html 的 select(功能)功能(可能不存在或不再存在?)。请参阅邮件列表中的帖子:osgeo-org.1803224.n2.nabble.com/Programatically-Select-a-Feature-tt2192485.html# a2193928 了解更多信息。

我尝试了以下但没有成功,所以我希望有人可以抓住这个代码行并可以向我展示一个工作代码 sn-p ;-)

// ... some initializing code
this.vlayer = new OpenLayers.Layer.Vector("VectorLayer");  // VectorLayer

// some controls
this.openLayerControlPoint = new OpenLayers.Control.DrawFeature(this.vlayer, OpenLayers.Handler.Point);
this.openLayerControlPolygon = new OpenLayers.Control.DrawFeature(this.vlayer, OpenLayers.Handler.Polygon);
this.openLayerControlModify = new OpenLayers.Control.ModifyFeature(this.vlayer, {
  mode: OpenLayers.Control.ModifyFeature.RESHAPE | OpenLayers.Control.ModifyFeature.DRAG,
  standalone: false
});

// just deactivate to make sure everything is really deactivated
this.openLayerControlPoint.deactivate();
this.openLayerControlPolygon.deactivate();
this.openLayerControlModify.deactivate();

// add the just created layer to the map
this.map.addLayer(this.vlayer);

// add all (deactivated) controls to the map
this.map.addControl(this.openLayerControlPoint);
this.map.addControl(this.openLayerControlPolygon);
this.map.addControl(this.openLayerControlModify);

后面的代码:

// ... another function doing the action
selectVector: function(wktVector) {
  this.openLayerControlModify.activate();

  // this is no elegant solution, this should only show how I would 
  // test the functionallity.
  for (var i = 0; i < this.vlayer.features.length; ++i) {
    // returns a WKT formatted string: 
    // 'POLYGON((xxxx.xxx xxxx.xxx), (xxxx.xxx xxxx.xxx))'
    var wktVectorCurrent = this.vlayer.features[i].geometry.toString(); 
    if (wktVector == wktVectorCurrent) {
      // \/ doesn't work :-(
      this.openLayerControlModify.selectFeature(this.vlayer.features[i]);
      break;
    }
  }
}

【问题讨论】:

    标签: javascript openlayers


    【解决方案1】:

    我不明白您为什么要使用 ModifyFeature 来选择功能。 OpenLayers.Control.SelectFeature 专门用于选择功能,因此我建议您改用此控件。

    所以,创建 SelectFeature 控件:

    var selectFeature = new OpenLayers.Control.SelectFeature(this.vlayer);
    selectFeature.activate();
    

    然后在你的 if 语句中(我猜它可以通过比较几何来找到你想要选择的特征?)使用 select 方法:

    if (wktVector == wktVectorCurrent) {
       selectFeature.select(this.vlayer.features[i]);
    }
    

    根据文档,此方法应将功能标记为已选择并引发适当的事件:

     * Method: select
     * Add feature to the layer's selectedFeature array, render the feature as
     * selected, and call the onSelect function.
    

    如果您想在选择要素时在地图上做某事(例如显示弹出窗口),您应该在创建矢量图层时订阅选择事件:

    this.vlayer.events.on({'featureselected': function(){
       //Handle select event
    }});
    

    【讨论】:

    • 感谢您的回复,这对我有用!我发现当前版本中的 OpenLayers API Doc 已损坏(并且没有 select 方法)。这是工作link to the OpenLayers API
    • 我对未来的建议是查看 openlayers 源代码而不是文档。在那里很容易找到你需要的东西,你会更好地理解事情是如何运作的。您经常会在源代码中找到在文档中看不到的有价值的 cmets。
    • 请注意,您需要先将“selectFeature”控件添加到地图,然后才能对其调用激活。 map.addControl(selectFeature);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多