我在Angular中将OpenLayers与TypeScript一起使用时遇到问题。 我的ol地图有一个事件监听器
this.map.on('click',function (e) {
let x = transform(e.coordinate,'EPSG:3857','EPSG:4326')
this.addMarker(x[0],x[1]);
});
x [0]和x [1]是我的经度和经度值。 如果我调用addMarker函数,则在点击地图时会收到错误消息:
ERROR TypeError: this.addMarker is not a function
at Map.<anonymous> (map.component.ts:90)
at Map.push.../node_modules/ol/events/Target.js.Target.dispatchEvent (Target.js:113)
at Map.push.../node_modules/ol/PluggableMap.js.PluggableMap.handleMapBrowserEvent (PluggableMap.js:871)
at MapBrowserEventHandler.push.../node_modules/ol/events/Target.js.Target.dispatchEvent (Target.js:113)
at MapBrowserEventHandler.push.../node_modules/ol/MapBrowserEventHandler.js.MapBrowserEventHandler.emulateclick_ (MapBrowserEventHandler.js:97)
at MapBrowserEventHandler.push.../node_modules/ol/MapBrowserEventHandler.js.MapBrowserEventHandler.handlePointerup_ (MapBrowserEventHandler.js:148)
at ZoneDelegate.invoketask (zone-evergreen.js:391)
at Object.onInvoketask (core.js:39680)
at ZoneDelegate.invoketask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
这是我的addMarker方法:
addMarker(lon,lat) {
let marker = new Feature({
geometry: new Point(transform([lon + 0.01,lat],'EPSG:4326','EPSG:3857'))
});
this.source.addFeature(marker);
}
我不知道如何解决此问题,或者是否有解决方法。
希望您能在这里为我提供帮助。