我目前正在尋找一種解決方案來選擇(或突出顯示)OpenLayers.Layer.Vector中的矢量。如何在OpenLayers的矢量圖層上以編程方式選擇要素?
我已經構建了一個簡單的網格表,用戶可以選擇一個向量(以WKT格式化字符串給出),該向量應該高亮顯示圖層上的相應向量。當用戶訪問該網站時,網格表中的所有矢量都會繪製到地圖上的矢量圖層。
我發現我要麼需要OpenLayers.Control.ModifyFeature的selectFeature(feature)功能或OpenLayers.Control.SelectFeature(見dev.openlayers.org/apidocs/files/OpenLayers/Control/SelectFeature-js.html's選擇(功能)功能(這可能不存在或不再存在?)。從郵件列表中查看帖子: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;
}
}
}
感謝您的答覆這個工作對我來說!我發現當前版本中的OpenLayers API Doc已損壞(並且沒有'select'方法)。這裏的工作[鏈接到OpenLayers API](http://dev.openlayers.org/docs/files/OpenLayers/Control/SelectFeature-js.html#OpenLayers.Control.SelectFeature.select) – 2012-01-13 10:19:46
我對未來的建議也是如此查看openlayers源代碼而不是文檔。很容易找到你需要的東西,並且你會更好地理解事情的工作方式。您經常會在源代碼中找到您在文檔中看不到的有價值的評論。 – igorti 2012-01-13 13:20:26
請注意,您需要將「selectFeature」控件添加到地圖,然後才能對其進行調用。 map.addControl(selectFeature); – JSancho 2013-06-15 11:35:21