【问题标题】:OpenLayers 2 - move vertex programmaticallyOpenLayers 2 - 以编程方式移动顶点
【发布时间】:2017-04-25 08:34:49
【问题描述】:

是否有在 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();

这些都不起作用。有没有办法让这些处理程序刷新?

【问题讨论】:

    标签: javascript openlayers


    【解决方案1】:

    我设法创建了一个简单的解决方法 - 刚刚从 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
    ));
    

    也许这不是一个完美的方法,但它正在工作:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 2019-12-07
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      相关资源
      最近更新 更多