根据 ngx-leaflet 文档 link (leafletMapMoveEnd)
和 (leafletMapZoomEnd)
都是公开事件。
我假设这些事件在 map 初始化的同一个 DOM 中公开,并且应该像这样实现:
<div
leaflet
[leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"
(leafletMapMoveEnd)="onMapMove($event)"
(leafletMapZoomEnd)="onMapZoom($event)">
这是捕获这些事件的正确方法吗?
(leafletMapReady)
似乎工作得很好。
但是,当我弄乱 map 本身时,(leafletMapZoomEnd)
或 (leafletMapMoveEnd)
似乎都没有触发。
我尝试平移 map 以及放大和缩小。这些交互都不会导致 handleMapZoomEnd($event)
handleMapMoveEnd($event)
方法被命中。
import { Component, Input, OnChanges, OnInit, Output, EventEmitter } from '@angular/core';
import * as fromLeafLet from 'leaflet';
import 'leaflet.markercluster';
@Component({
selector: 'map',
templateUrl: './map.component.html',
styleUrls: [
'./map.component.css',
'./extra-marker-icon.css'
]
})
export class MapComponent implements OnInit, OnChanges {
constructor(){}
onMapReady(map: fromLeafLet.Map): void {
this.map = map;
}
onMapZoom(event: any):void{
console.log('Zoom');
this.onMapDirty.emit();
}
onMapMove(event: any):void{
console.log('Move');
this.onMapDirty.emit();
}
}
最佳答案
在 Github repo对于 @asymmetrik/ngx-leaflet ngcli 教程,我添加了一个分支 demo/events
,它展示了使用缩放/移动事件的非常简单的示例。
git clone <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8afa1bc88afa1bca0bdaae6aba7a5" rel="noreferrer noopener nofollow">[email protected]</a>:Asymmetrik/ngx-leaflet-tutorial-ngcli.git
git checkout demo/events
感兴趣的文件是:
./src/app/app.component.html
./src/app/app.component.ts
模板(如下)包含两个处理程序,一个用于 leafletMapMoveEnd 和 leafletMapZoomEnd 输出:
<div class="map"
leaflet
[leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"
(leafletMapMoveEnd)="handleMapMoveEnd($event)"
(leafletMapZoomEnd)="handleMapZoomEnd($event)"
></div>
组件(如下)仅将这些事件打印到控制台。我删除了演示事件工作所需的大部分内容。
import { Component } from '@angular/core';
import * as L from 'leaflet';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [
'./app.component.css'
]
})
export class AppComponent {
streetMaps = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
detectRetina: true,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
});
map: L.Map;
options = {
layers: [ this.streetMaps ],
zoom: 7,
center: L.latLng([ 46.879966, -121.726909 ])
};
onMapReady(map: L.Map): void {
this.map = map;
}
handleMapZoomEnd(map: L.Map):void{
console.log('onMapZoomEnd');
}
handleMapMoveEnd(map: L.Map):void{
console.log('onMapMoveEnd');
}
}
关于javascript - ngx-leaflet map 缩放事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974852/