【问题标题】:Multiple Ngx-leaflets maps on same page : all the tiles are displayed on the same map同一页面上的多个 Ngx 传单地图:所有图块显示在同一地图上
【发布时间】:2021-01-25 16:22:41
【问题描述】:

我正在将标准 Angular 项目转换为 Angular Universal 项目,因此我必须摆脱“$window”。该项目没有使用任何模块,但使用的一些模块,如传单,正在使用它,所以我切换到 ngx-leaflet。

组件只访问@types\leaflet 对象来生成配置,地图生成留给带有ngx-leaflet 指令的html:leaflet。 html如下:

<div class="row w75 justify-content-center mb-5">
  <div class="content-wrapper col-lg-9 col-sm11" >
    <h3  class="center-title text-uppercase">Country locations</h3>
    <div class="title-block2"></div>
      <div id="map" leaflet [leafletOptions]="options" style="min-height: 700px">
      </div>
  </div>
</div>
<div class="row justify-content-center mt-3 mb-5">
  <div class="content-wrapper col-12" >
    <h3  class="center-title text-uppercase">Woldwide locations</h3>
    <div class="title-block2"></div>
    <div id="map2" leaflet  [leafletOptions]="options2" style="min-height: 700px">
    </div>
  </div>
</div>

结果是确实出现了 2 个地图,每个地图都有其标记和功能弹出窗口,但只有第一个地图显示实际的地图图块,并且实际上显示了 2 个地图的所有图块,所以第二个地图只是gey backfgound。

在使用 ngx-leaflet 时我缺少什么吗? 例如: [containerId]="'map'" ?

控制器如下:

import {Component, OnInit} from '@angular/core';
import {icon, popup, marker, latLng, tileLayer, layerGroup, polyline,
  Polyline, LayerGroup, LatLng, TileLayer} from 'leaflet';
@Component({
  selector: 'app-map',
  templateUrl: './coverage.component.html',
  styleUrls: ['./coverage.component.scss']
})


export class CoverageComponent implements OnInit {
  title = 'leafletApps';
  popup = popup();
  width = window.innerWidth;
  polylinePoints: LatLng[] = [];
  polyline: Polyline;
  assetLayerGroup: LayerGroup = layerGroup();
  assetLayerGroup2: LayerGroup = layerGroup();
  myTitleLayer: TileLayer = tileLayer('https://tiles.stadiamaps.com/tiles/osm_bright/{z}/{x}/{y}{r}.png',
    { attribution: '© Angular LeafLet' });
  options: {layers: any[], zoom: number, center: LatLng , dragging: boolean,
            scrollWheelZoom: boolean } =
    {
      layers: [this.myTitleLayer, this.assetLayerGroup],
      zoom: 6,
      dragging: (this.width > 992),
      scrollWheelZoom: false,
      center: latLng(4.7110, -74.0721)
    };
  options2: {layers: any[], zoom: number, center: LatLng ,
             dragging: boolean, scrollWheelZoom: boolean } =
        {
          layers: [this.myTitleLayer, this.assetLayerGroup2],
          zoom: 3,
          dragging: (this.width > 992),
          scrollWheelZoom: false,
          center: latLng(20, 0)
        };
      constructor() {}
  ngOnInit() {
    this.assetLayerGroup = this.fillMap1();
    this.options.layers.push(this.myTitleLayer);
    this.options.layers.push(this.assetLayerGroup);
    this.assetLayerGroup2= this.fillMap2();
    this.options2.layers.push(this.myTitleLayer);
    this.options2.layers.push(this.assetLayerGroup2);
  }
}

知道错误在哪里吗?

【问题讨论】:

    标签: angular ngx-leaflet


    【解决方案1】:

    好吧,在输入问题时,我找到了答案.... 但是我还是发布了这个问题,以防有人遇到这个问题:

    图块放置在实际上是“tileLayer”的“titleLayer”上。 我的那个拼写错误导致了一个错误,认为它只是标题。 因此,由于我的组件没有复制切片图层,而是为 2 张地图使用了相同的图层,这就是问题所在。

    解决方案是创建 2 个切片图层对象,即使它们具有相同的内容并将每个图层分配给不同的地图...

    正确的组件代码如下:

    import {Component, OnInit} from '@angular/core';
    import {icon, popup, marker, latLng, tileLayer, layerGroup, polyline,
      Polyline, LayerGroup, LatLng, TileLayer} from 'leaflet';
    @Component({
      selector: 'app-map',
      templateUrl: './coverage.component.html',
      styleUrls: ['./coverage.component.scss']
    })
    
    
    export class CoverageComponent implements OnInit {
      title = 'leafletApps';
      popup = popup();
      width = window.innerWidth;
      polylinePoints: LatLng[] = [];
      polyline: Polyline;
      assetLayerGroup: LayerGroup = layerGroup();
      assetLayerGroup2: LayerGroup = layerGroup();
      myTileLayer1: TileLayer = tileLayer('https://tiles.stadiamaps.com/tiles/osm_bright/{z}/{x}/{y}{r}.png',
        { attribution: '© Angular LeafLet' });
      myTileLayer2: TileLayer = tileLayer('https://tiles.stadiamaps.com/tiles/osm_bright/{z}/{x}/{y}{r}.png',
        { attribution: '© Angular LeafLet' });
      options: {layers: any[], zoom: number, center: LatLng , dragging: boolean,
                scrollWheelZoom: boolean } =
        {
          layers: [this.myTileLayer1, this.assetLayerGroup],
          zoom: 6,
          dragging: (this.width > 992),
          scrollWheelZoom: false,
          center: latLng(4.7110, -74.0721)
        };
      options2: {layers: any[], zoom: number, center: LatLng ,
                 dragging: boolean, scrollWheelZoom: boolean } =
            {
              layers: [this.myTileLayer2, this.assetLayerGroup2],
              zoom: 3,
              dragging: (this.width > 992),
              scrollWheelZoom: false,
              center: latLng(20, 0)
            };
          constructor() {}
      ngOnInit() {
        this.assetLayerGroup = this.fillMap1();
        this.options.layers.push(this.myTileLayer1);
        this.options.layers.push(this.assetLayerGroup);
        this.assetLayerGroup2= this.fillMap2();
        this.options2.layers.push(this.myTileLayer2);
        this.options2.layers.push(this.assetLayerGroup2);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多