2012-01-12 35 views
12

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

Trả lời

20

Tôi không hiểu tại sao bạn sử dụng tính năng ModifyFeature để chọn đối tượng địa lý. OpenLayers.Control.SelectFeature được thực hiện đặc biệt để chọn các tính năng nên tôi khuyên bạn nên sử dụng điều khiển này để thay thế.

Vì vậy, tạo kiểm soát SelectFeature:

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

Sau đó, trong bạn if-statement (tôi đoán nó hoạt động để tìm đối tượng bạn muốn chọn bằng cách so sánh hình học?) Sử dụng lựa chọn phương pháp:

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

Theo tài liệu, phương pháp này phải đánh dấu đối tượng địa lý là đã chọn và tăng các sự kiện thích hợp:

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

Nếu bạn muốn làm một cái gì đó trên bản đồ khi tính năng được chọn (như hiển thị một popup), bạn nên đăng ký lớp vector để chọn sự kiện khi bạn tạo ra nó:

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

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) –

+4

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

+4

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

Các vấn đề liên quan