2017-05-05 301 views

回答

2

我很难自己找到解决方案,但在调试ol代码后,我想出了这个解决方案。

当在ol中创建事件时,所有交互都从上次迭代到第一次。捕捉交互取代指针坐标(如果捕捉)以及之前的所有交互(在迭代之后,因为向后迭代)将获得该坐标。

所以,无论是在自定义交互运行代码或打字稿使用自定义交互exctract坐标,像这样的例子:

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