javascript - 将其传递到 map 上的点击处理程序中

标签 javascript events arguments this openlayers

我正在尝试在 Angular 4 应用程序中实现 map (使用 openlayers)。我是这样实现的: 网页:

<div id="map" class="map" style="width:80vw;height: 60vh;"></div>

typescript :
声明:

@ViewChild("mapElement") mapElement:ElementRef;
public map:any;

初始化:

this.map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            })
        ],
        target: 'map',
        view: new ol.View({
            center: [2905830.0672892607,5532192.9828046],
            zoom: 12
        })
    });

在我的 ts 文件中,我还有 selectedLat 和 selectedLon (任何类型的变量)。我想根据用户单击 map 触发的事件中找到的坐标来设置这些变量。为此,我尝试了以下:

this.map.on('click',this.some_function);

某些功能:

some_function(evt) {
    this.selectedLon=ol.proj.toLonLat(evt.coordinate)[0];
    this.selectedLat=ol.proj.toLonLat(evt.coordinate)[1];
}

我遇到的问题是,this(在 some_function 中)是由 map 表示的,而不是我的原始组件。 有没有办法让我将原始的 this 元素传递给 some_function?

我也尝试过但失败了:

this.map.on('click',{param1:this},this.some_function);

感谢您的帮助:)

最佳答案

Openlayers v4.6.5 documents比如说,map.on 可以有三个参数。一个是事件类型,另一个是监听器函数,最后一个是您要用作 this 的可选对象。

因此您可能需要使用 this.map.on('click', this.som_function, this)

关于javascript - 将其传递到 map 上的点击处理程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50470712/

相关文章:

javascript - 未捕获错误 : You should not use <Switch> outside a <Router>, 但开关在里面

Javascript:根据来自另一个下拉列表的 onchange 数据填充 html 下拉列表

javascript - AngularJS 和 IFrame srcdoc

javascript - 是否可以从 onkeyup 获取 key 代码?

ios - objective-c - 传递给子 block 的父函数的参数在内部没有改变

javascript - 从 Map 获取与所使用的原始键相同但不是引用的值

javascript - 当从其他类调用方法时,获取对方法对象的引用(javascript)

javascript - 每次单击按钮时,我都想在两个功能之间切换

c - 声明 -n 参数以在命令行中接受整数 - C

java - Netbeans如何在Java中设置命令行参数