Tôi hiện đang tìm kiếm giải pháp để chọn (hoặc đánh dấu) một vectơ trong OpenLayers.Layer.Vector.Làm thế nào để chọn một tính năng lập trình trên một lớp vectơ trong OpenLayers?
Tôi đã xây dựng một khung lưới đơn giản, nơi người dùng có thể chọn một véc tơ (được cho là chuỗi định dạng WKT) sẽ làm nổi bật vectơ tương ứng trên lớp. Tất cả các vectơ trong lưới có thể được vẽ đến lớp vectơ trên bản đồ khi người dùng truy cập trang web.
tôi phát hiện ra rằng tôi có cần các 's selectFeature(feature) chức năng hoặc OpenLayers.Control.SelectFeature OpenLayers.Control.ModifyFeature (xem dev.openlayers.org/apidocs/files/OpenLayers/Control/SelectFeature-js.html's chọn (tính năng) chức năng (có thể không tồn tại hoặc không tồn tại nữa?) Xem bài đăng từ Danh sách gửi thư: osgeo-org.1803224.n2.nabble.com/Programatically-Select-a-Feature-tt2192485.html#a2193928 để biết thêm infos.
tôi đã thử các sau đây không thành công, vì vậy tôi hy vọng ai đó có thể lấy dòng mã này và có thể chỉ cho tôi một đoạn mã làm việc ;-)
// ... 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);
Sau đó trong mã:
// ... 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;
}
}
}
Cảm ơn phản hồi của bạn này làm việc cho tôi! Tôi phát hiện ra rằng Tài liệu API OpenLayers trong bản phát hành hiện tại bị hỏng (và không có phương thức 'select'). Đây là cách làm việc [liên kết tới API OpenLayers] (http://dev.openlayers.org/docs/files/OpenLayers/Control/SelectFeature-js.html#OpenLayers.Control.SelectFeature.select) –
lời khuyên của tôi cho tương lai cũng vậy xem mã nguồn mở thay vì tài liệu. thật dễ dàng để tìm thấy những gì bạn cần ở đó và bạn sẽ hiểu rõ hơn về cách mọi thứ hoạt động. bạn thường sẽ tìm thấy các nhận xét có giá trị trong mã nguồn mà bạn sẽ không thấy trong tài liệu. – igorti
lưu ý rằng bạn sẽ cần thêm điều khiển "selectFeature" vào bản đồ trước khi bạn có thể gọi kích hoạt trên bản đồ. map.addControl (selectFeature); – JSancho