簡體   English   中英

OpenLayers 2-以編程方式移動頂點

[英]OpenLayers 2 - move vertex programmatically

在OpenLayers 2中以編程方式移動單個頂點的正確方法是什么?

問題是,我需要防止用戶繪制無效的(例如,自交叉)多邊形。 為此,我創建了一個驗證多邊形的函數,然后在OpenLayers.Control.ModifyFeature類的dragComplete方法中驗證了多邊形(我基於該類創建了自己的類)。

基本上,我將當前頂點位置保存在dragStart方法中,然后在dragComplete方法中驗證多邊形,如果多邊形無效,則將頂點的坐標更改為之前保存的坐標。

工作正常,頂點移回到先前的位置。 但是拖動和旋轉處理程序存在問題。 這些處理程序並不關心該頂點是否已移動,而是將位置設置為使用此錯誤頂點(在移動之前)的幾何中心。 當我拖動或旋轉要素時,這兩個處理程序都會恢復到正確的位置。 移動此頂點時,需要正確設置它們。

我需要調用一些事件或方法以使其正常工作嗎?

這就是我在dragComplete方法開始時所做的。

dragComplete: function (vertex) {
    if (this.feature.geometry.isValid &&
        !this.feature.geometry.isValid()) {
        vertex.geometry.x = this.vertexPosition.x;
        vertex.geometry.y = this.vertexPosition.y;
        this.layer.drawFeature(this.feature);
    }

    ...
}

它將頂點正確移動到給定位置。 問題是它不會影響拖動處理程序和旋轉處理程序。 這是我嘗試使用的方法,但沒有任何效果(在dragComplete方法中,它恰好位於vertex.geometry.y = this.vertexPosition.y;下面):

this.layer.events.triggerEvent('vertexmodified', {
    feature: this.feature,
    vertex: vertex
});
this.layer.events.triggerEvent('featuremodified', {
    feature: this.feature
});
this.layer.events.triggerEvent('refresh');
this.onModification(vertex);
this.onModification(this.feature);
this.layer.drawFeature(vertex);
this.layer.redraw();

這些都不起作用。 有沒有辦法使這些處理程序刷新?

我設法創建了一個簡單的解決方法-只是從geometry對象中調用了rotate方法,將角度作為零,將中心點作為原點:

var bounds = this.feature.geometry.bounds;
var center = {
    x: bounds.left + (bounds.right - bounds.left) / 2,
    y: bounds.bottom + (bounds.top - bounds.bottom) / 2
};
this.feature.geometry.rotate(0, new OpenLayers.Geometry.Point(
    center.x, center.y
));

也許這不是一個完美的方法,但是它是可行的:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM