我在我的网页上使用 Angular 5 + ngx-leaflet 来显示带有来自 WMS 服务器的图层数据的地图。
父组件html
<div class="majorContainer">
<app-leftcol (leftcolUpdate)="leftColChange($event)"></app-leftcol>
<div class="mainRightContainer">
<app-mapcomponent></app-mapcomponent>
</div>
</div>
父组件.ts
export class MajorComponent implements OnInit {
@Output() updateMapLayer = new EventEmitter<any>();
constructor() { }
ngOnInit() {
}
leftColChange($event) {
console.log('emit to leaflet layer');
this.updateMapLayer.emit({newModelName: $event.newModelName, view:
$event.view});
}
}
地图组件html:
<div class="mapContainer">
<div id="mapid"
leaflet
[leafletOptions]="options"
[leafletLayers]="layers"
[leafletLayersControl]="layersControl"
(leafletMapReady)="initMap($event)"
(updateMapLayer)="updateLayer($event)">
</div>
</div>
map.component.ts:
export class MapComponent implements OnInit {
constructor() { }
options = {...};
layers = [ ... ];
layersControl = {
overlays: {
'Map WMS Layer": tileLayer.wms('http://...', {version: '1.1.1', newModelName: 'default', view: 'default', year: '2018');
}
};
ngOnInit() {}
initMap(map: Map) {...}
updateLayer($event) {
const updateOptions = {version:'1.1.1',newModelName:$event.newModelName,view:$event.view,year:$event.year};
this.layersControl.overlays['Map WMS Layer'].setParams(updateOptions);
this.layersControl.overlays['Map WMS Layer'].redraw();
}
}
我想要实现的目标:当左栏中的值发生变化时,地图图层中的 WMS URL 将被更新并刷新。
我尝试使用 EventEmitter 将更新的值解析到父组件并调用子地图组件来更新 WMS 图层,但它不起作用。是不是因为方法不对?
谢谢你。