ngx-leaflet地图缩放事件未触发

时间:2018-08-22 20:45:59

标签: javascript angular typescript leaflet ngx-leaflet

根据ngx-leaflet文档link (leafletMapMoveEnd)(leafletMapZoomEnd)都是公开事件。

我假设这些事件在与地图初始化相同的DOM中公开,并且应该这样实现:

<div 
 leaflet
 [leafletOptions]="options"
 (leafletMapReady)="onMapReady($event)"
 (leafletMapMoveEnd)="onMapMove($event)"
 (leafletMapZoomEnd)="onMapZoom($event)">

这是捕获这些事件的正确方法吗?

(leafletMapReady)似乎工作正常。

但是当我弄乱地图本身时,(leafletMapZoomEnd)(leafletMapMoveEnd)似乎都没有触发。

我尝试平移地图以及放大和缩小。这些交互都不会导致点击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();
  }
}

1 个答案:

答案 0 :(得分:2)

在用于{asymmetrik / ngx-leaflet ngcli教程的Github repo中,我添加了一个分支demo/events,该分支显示了一个使用zoom / move事件的非常简单的示例。

git clone git@github.com:Asymmetrik/ngx-leaflet-tutorial-ngcli.git
git checkout demo/events

感兴趣的文件是:

./src/app/app.component.html
./src/app/app.component.ts

模板(如下)包含两个处理程序,每个letletletMapMoveEnd和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: '&amp;copy; &lt;a href="https://www.openstreetmap.org/copyright"&gt;OpenStreetMap&lt;/a&gt; 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');
  }
}