我正在寻找在OpenLayers.Layer.Vector中选择(或突出显示)矢量的解决方案。
我构建了一个简单的网格表,用户可以在其中选择一个向量(以WKT格式的字符串给出),该向量应突出显示图层上的相应向量。当用户访问网站时,网格表中的所有向量都将绘制到地图上的矢量图层。
我发现我需要OpenLayers.Control.ModifyFeature的selectFeature(feature)函数或OpenLayers.Control.SelectFeature(请参阅dev.openlayers.org/apidocs/files/OpenLayers/Control/SelectFeature-js .html的选择(功能)功能(可能不存在或不再存在?)。查看邮件列表中的帖子: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;
}
}
}
答案 0 :(得分:20)
我不明白为什么使用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.
如果您想在选择要素时在地图上执行某些操作(如显示弹出窗口),则应在创建时将矢量图层订阅为select-event:
this.vlayer.events.on({'featureselected': function(){
//Handle select event
}});