2017-05-05 9 views
1

환영 인사와 인사!오픈 레이어의 스냅에서 좌표 가져 오기

스냅 기능에서 형상 좌표를 얻는 방법은 무엇입니까? 나는이 예제 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); 
    }); 

을하지만 작동하지 않습니다. '선택', 'drawend', modifyend '이벤트를 사용해야합니다.

답변

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