我正在尝试使用removeLastPoint()
OpenLayers的功能解决我的应用程序问题。问题在于,在移动设备上removeLastPoint()
正在按错误的顺序去除点。在桌面模式下,一切正常。
这是此问题的解释。
让我在图像上介绍一下这种“以错误的顺序消除点的问题”在实践中是如何工作的。
我们从画一些开始LineString
:
现在LineString
,我们已经从第5点的连接中创建了一个。现在,我试图removeLatPoint()
通过单击我的调用函数removeButton
,就像这样:
removeButton.on('click', () => {
draw.removeLastPoint(); // this function comes from ol.integration.Draw class
});
现在,我期望得到这样的结果:
但是我得到了什么?我得到这样的东西:
在下一步中,我将获得:
接下来:
最后,我只会得到第5点。
问题在于,在OpenLayers
removeLastPoint()
函数中不是删除最后一个点,而是删除它之前的一个。
这是removeLastPoint()
来自的当前功能代码OpenLayers
:
问题在于这条线对我来说:coordinates.splice(-2, 1);
因为它的工作就像我展示的那样。
我迫不及待要解决这个问题,所以我写了我的临时解决方案。在我的修复程序中,我在removeLastPoint()
自己的函数之前调用,该函数删除了最后一个坐标并复制了它之前的坐标。就像这样:
There is once difference. I'm using draw.extend(measureFeature);
function to duplicate this coordinate before this last, because I have to refresh information which have been saved in private variable this.sketchCoords_
on the OpenLayers
site. When I was trying to do it on the other way then this.sketchCoords_
still remembered previously saved state of drawed geometry and instead of call removeLastPoint()
for myArray = ['1','2','3','4','4']
then it was calling for myArray = ['1','2','3','4','5']
. So I have to use draw.extend()
to duplicate current last item and update this.sketchCoords_
.
Here is my own fix on this problem:
removeButton.on('click', () => {
if (this.isMobileDevice) {
this.removeLastPoint();
}
this.draw.removeLastPoint();
});
removeLastPoint() {
let measureFeature = this.sketch;
measureFeature = measureFeature.clone();
const geom = measureFeature.getGeometry();
if (geom instanceof ol.geom.LineString) {
const coords = geom.getCoordinates();
let preparedCoords: [number, number][] = new Array();
if (coords) {
preparedCoords = coords;
preparedCoords.splice(-1, 1);
geom.setCoordinates(preparedCoords);
measureFeature.setGeometry(geom);
if (preparedCoords.length > 0) {
this.draw.extend(measureFeature);
}
}
} else if (geom instanceof ol.geom.Polygon) {
}
}
My fix is working as I exected. After call removeLastPoint()
is removing redundant item and I've got a correct collection.
And here is starting my real problem. Polygons. On polygons removeLastPoint()
function doesn't work well too and there we've got this same problem with removing wrong point (not last but before it).
So I'm going to write a similar mechanism to modify polygon coordinates as I wrote for LineString
geometries.
But how can I update this.sketchCoords_
by inject my modified coordinates collection if draw.extend()
is working only for LineString
geometries?
I haven't idea how can I do it. Class Draw
from OpenLayers
hasn't any helpful function for polygons which will be change or update current drawing polygon.
Here is full of Draw
class.
Have you got any idea how can I update it and inject my modified coordinate collection?
Here is my fiddle, Maybe it'll helpful. I don't know why but on my fiddle sketch is always null so this fiddle is not working well but it able to show my code a little bit.
Once again I solved the problem which I put by myself into StackOverflow. Awesome!
So let me present the solution.
I said that after modify a polygon I need update this.sketchCoords_
object because without it the removeLastPoint()
from OpenLayers
will be working on the old coordinates collection.
If draw.extend()
is working only for LineString
geometries then I had to find another solution how to do it.
And I found it.
The solution was:
writing own class to extend class Draw from OpenLayers from comes this.sketchCoords_
object:
declare namespace ol {
namespace interaction {
class ExtendedDrawClass extends ol.interaction.Draw {
sketchCoords_?: ol.Coordinate | ol.Coordinate[] | ol.Coordinate[][];
}
}
}
and after that just modify this collection in own code. For example just like that:
var myNewCoordinates = geometry.getCoordinates();
myNewCoordinates.split(-1,1);
(<ol.Coordinate[][]>(<ol.interaction.ExtendedDrawClass>this.draw).sketchCoords_) = [myNewCoordinates];
And that's all.
当代码从那时开始调用removeLastPoint()
函数时,OpenLayers
它已经将一个myNewCoordinates
集合设置为this.sketchCoords_
对象。
我将此解决方案留给有类似问题的人。也许会有所帮助。如果是,请支持我的提问和回答。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句