【问题标题】:Change leafletLayers after a dragend event在 dragend 事件后更改leafletLayers
【发布时间】:2018-02-17 01:18:51
【问题描述】:

我需要删除所有的leafletLayers 并在“dragend”事件之后添加其他的。所以,我进行如下:

ma​​pParent.component

template: '<app-map [leafLetmarkers]="markers" (refreshMap)="refresh($event)"></app-map>'

...

markers: L.Layer[] = [];
refresh(position) {
    //delete all markers
    var markers = []; 
    //set the new markers
    this.markers= newMarkers;
}

ma​​p.component

template: '<div leaflet style="height: 100%;"
                [leafletOptions]="options"
                [leafletLayers]="markers"
                (leafletMapReady)="onMapReady($event)">
           </div>'

...

@Input('leafLetmarkers') markers: L.Layer[];
@Output() refreshData = new EventEmitter<L.LatLng>();

onMapReady(map: L.Map) {
    map.on('dragend', e => this.refreshMap.emit(map.getCenter()));
}

这是正确的方法吗?

问候。

【问题讨论】:

    标签: leaflet angular5 ngx-leaflet


    【解决方案1】:

    您的一般方法是正确的。

    您可能遇到的问题是您正在更改来自 Leaflet 的回调内部的绑定属性 this.markers。 Leaflet 回调位于 Angular 区域之外(Angular 不会尝试跟踪其区域之外的更改)。这是 ngx-leaflet 的一种设计选择,用于防止可能影响应用程序性能的过度更改检测。

    解决办法是手动触发变更检测:

    fitBounds: any = null;
    circle = circle([ 46.95, -122 ], { radius: 5000 });
    
    // Inject the Change Detector into your component
    constructor(private changeDetector: ChangeDetectorRef) {}
    
    ngOnInit() {
    
        // The 'add' event callback happens outside of the Angular zone
        this.circle.on('add', () => {
    
            // Because we're outside of Angular's zone, this change won't be detected
            this.fitBounds = this.circle.getBounds();
    
            // But, it will if we tell Angular to detect changes 
            this.changeDetector.detectChanges();
    
        });
    }
    

    有关更多详细信息,您可以查看 ngx-leaflet README 的这一部分: https://github.com/Asymmetrik/ngx-leaflet#a-note-about-change-detection

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 2020-03-29
      • 1970-01-01
      • 2023-03-20
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2013-11-07
      相关资源
      最近更新 更多