gpt4 book ai didi

javascript - 如何在 OpenLayers 的矢量图层上以编程方式选择特征?

转载 作者:可可西里 更新时间:2023-11-01 02:02:23 25 4
gpt4 key购买 nike

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

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

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

我尝试了以下但没有成功,所以我希望有人可以捕获这些代码行并向我展示一个有效的代码片段;-)

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

最佳答案

我不明白您为什么要使用 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.

如果您想在选择要素时在 map 上做一些事情(比如显示弹出窗口),您应该在创建矢量图层时订阅它以选择事件:

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

关于javascript - 如何在 OpenLayers 的矢量图层上以编程方式选择特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8832029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com