如何通过捕捉功能获取要素坐标? 我正在使用这个示例 OpenLayers 网站: http://openlayers.org/en/latest/examples/snap.html?q=snap
我尝试这样做:
snap.on('select', function(evt) {
var coord = evt.selected[0].getGeometry().getCoordinates();
alert(coord);
});
但是不起作用。 我需要使用 'select'、'drawend'、modifyend' 事件。
最佳答案
我自己很难找到解决方案,但在调试 ol 代码后,我想出了这个解决方案。
当在 ol 中创建事件时,所有交互都会从最后到第一个迭代。捕捉交互替换指针坐标(如果捕捉),并且之前的所有交互(迭代时之后,因为向后迭代)将获得此坐标。
因此,要么在自定义交互中运行代码,要么使用自定义交互来提取坐标,如 TypeScript 中的示例:
let snapCoordinate: ol.Coordinate;
let getSnapCoordinateInteraction = new ol.interaction.Interaction({
handleEvent: (evt: ol.MapBrowserEvent) => {
snapCoordinate = evt.coordinate;
return true;
}
});
this.map.addInteraction(getSnapCoordinateInteraction);
// Snap, should always be last of all interactions that should use the snap coordinate.
this.snap = new ol.interaction.Snap({
source: this.source,
});
this.map.addInteraction(this.snap);
关于javascript - 从 openlayers 中的 snap 获取坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43813089/