javascript - 在地图上的点击处理程序中传递它
问题描述
我正在尝试在 Angular 4 应用程序中实现地图(使用 openlayers)。我已经以这种方式实现了它:Html:
<div id="map" class="map" style="width:80vw;height: 60vh;"></div>
打字稿:
声明:
@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 (任何类型的变量)。我想根据用户点击地图触发的事件中找到的坐标来设置这些变量。为此,我尝试了以下方法:
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(in some_function) 由地图表示,而不是我的原始组件。有没有办法将原始的 this 元素传递给 some_function?
我也试过但失败了:
this.map.on('click',{param1:this},this.some_function);
谢谢您的帮助 :)
解决方案
正如Openlayers v4.6.5 文档所说,map.on
可以有三个参数。一个是事件类型,另一个是侦听器函数,最后一个是您要用作 this 的可选对象。
所以你可能需要使用this.map.on('click', this.som_function, this)
推荐阅读
- visual-studio-code - VSCode 自动格式化,无需换行
- python - 将每一行乘以不同的旋转矩阵
- hibernate - 休眠依赖项,找不到扫描仪类
- python - 在 python 使用 *args 的类中传递和访问参数
- android - 有没有办法将 UnityPlayerActivity 用作 AppcompatActivity?
- node.js - 在数小时内禁用 App Engine 实例/服务器以降低成本
- java - 从 ATM 取款
- laravel - 使用输入重定向回上一页
- angular - 创建托管角度小部件时的角度完全封装
- java - 避免在循环中执行数据库查询