【问题标题】:Rendering multiple leaflet maps in Angular在 Angular 中渲染多个传单地图
【发布时间】:2019-11-02 08:25:46
【问题描述】:

我正在尝试使用 angular 在同一页面上呈现多个传单地图。 ngFor 用于动态渲染地图。地图的数量取决于从 REST 服务接收的数据。

ma​​p.component.html

<div *ngFor="let m of mapIds">
    <div class="map" [id]="m"></div>
</div>

ma​​p.component.ts

[版本 1] - this.mapIds 中未填充数据

ngOnInit() {
    PopulateData();

    // do other stuff
}

PopulateData() {            

    // Invoke Web service
    this.cService.getMapData(cInput).subscribe(
        (res: getMapData[]) =>
        {
            // form a list of mapIds
            this.mapIds = mapIds
        }
}

ngAfterViewInit() {
    // Initialize leaflet maps
    this.mapIds.forEach( x => {         // <--- this.mapIds is null
        this.maps[x] = L.map(x, {       
            attributionControl: false,
            minZoom: 3,
            maxZoom: 7,
            zoomControl: false
        });

        // rest of map initialization 
    })
}

[版本 2] 给出以下错误

错误:找不到地图容器。

ngOnInit() {
    PopulateData();

    // do other stuff

    InitMaps();
}

PopulateData() {            

    // Invoke Web service
    this.cService.getMapData(cInput).subscribe(
        (res: getMapData[]) =>
        {
            // form a list of mapIds
            this.mapIds = mapIds
        }
}

InitMaps() {
    // Initialize leaflet maps
    this.mapIds.forEach( x => {
        this.maps[x] = L.map(x, {        // <-- Error thrown here
            attributionControl: false,
            minZoom: 3,
            maxZoom: 7,
            zoomControl: false
        });

        // rest of map initialization 
    })
}

有没有办法等到this.mapIds 填充到版本 1 中?或者有什么其他建议?

【问题讨论】:

  • 代码中的“L”是什么?

标签: angular leaflet


【解决方案1】:

如果您将ngAfterViewInit() 代码移动到populateData(),您应该能够实现您想要做的事情

populateData() {            
    // Invoke Web service
    this.cService.getMapData(cInput).subscribe(
        (res: getMapData[]) =>
        {
            res.forEach( x => {
                this.maps[x] = L.map(x, {       
                    attributionControl: false,
                    minZoom: 3,
                    maxZoom: 7,
                    zoomControl: false
                });                        
        }
}

你得到this.mapIds is null的原因是因为在ngAfterViewInit()执行的时候,PopulateData()还没有完成。

【讨论】:

  • 这不起作用 - 仍然出现“找不到地图容器”错误。顺序应该如下:调用Webservice并填充“mapIds”->这将触发角度以形成适当的div标签->使用L.map(...)初始化传单
猜你喜欢
  • 1970-01-01
  • 2013-07-11
  • 2016-12-29
  • 2022-11-15
  • 2021-09-12
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 2020-01-27
相关资源
最近更新 更多