angular - 如何在 Openlayers 4 中的 ol.control 上添加点击事件监听器?
问题描述
我正在尝试记录 UI 交互,因此我想从我的ol.controls
( ol.control.Zoom
, ol.control.Rotate
, ol.control.Attribution
) 获取点击/标签事件。
但是怎么做?我在文档中找不到任何内容
解决方案
我当时也没有找到,但我也可能错过了重点。您可能会忘记使用 OpenLayers API,而使用纯 JavaScript。
// Check zoom +/- buttons event
document.querySelector('.ol-zoom').addEventListener('click', evt => {
if (evt.target.classList.contains('ol-zoom-in')) {
console.log('Zoom in');
} else if (evt.target.classList.contains('ol-zoom-out')) {
console.log('Zoom out');
}
});
// Check if rotate button clicked (normally visible only if map rotated)
document.querySelector('.ol-rotate').addEventListener('click', evt => {
console.log('Rotate');
});
// Check if attribution button opened or closed
document.querySelector('.ol-attribution').addEventListener('click', evt => {
if (evt.currentTarget.classList.contains('ol-collapsed')) {
console.log('Collapsed');
} else {
console.log('Opened');
}
});
如果您想观察来自 OpenLayers 的事件而不关心单击发生的位置(因为缩放事件可以通过鼠标滚轮、双击地图或单击 +/- 缩放按钮来生成),您可能想要监听ol.View
事件
map.getView().on('change:rotation', evt => {
console.log('Rotation event', evt);
})
map.getView().on('change:resolution', evt => {
console.log('Resolution event', evt);
})
PS:您可以在视图上侦听其他事件,但我让您在 API 中检查它们
推荐阅读
- google-cloud-platform - GCP Composer (Autoscaling) 环境错误 - “某些 GKE pod 无法正常运行”
- flutter - Flutter stack + 容器来构建类似火种的东西
- node.js - NodeJS ImageMagick 如何转换具有透明背景的图像
- proxy - APACHE - proxypass 问题(排除带有 wordpress 安装的子文件夹)
- python - 是否可以在 Python Tkinter 的屏幕中间居中放置一个框架?
- xml - XHTML 页面,查看元素内的内容
- javascript - 如何将大型数据集结果按块存储到nodejs中的csv文件中?
- android - 我应该如何更改jsp sql?
- c++ - 解释visual studio profiler,这个减法慢吗?我可以让这一切更快吗?
- c# - 在 c# (DAG) 中向上遍历有向图