【问题标题】:Leaflet + Ionic5 / Angular - Map bad Rendering -Leaflet + Ionic5 / Angular - 映射不良渲染 -
【发布时间】:2021-10-31 19:37:49
【问题描述】:

在对它进行了一些搜索但没有成功之后,我来找你,因为我知道这有多种威胁,但正如我所说,这些并没有解决我的问题。

问题:
正如其他人所经历的那样,Leaflet 在地图和框架页面加载方面存在一些问题。

  • 如果我手动调整浏览器屏幕的大小,它会正确呈现。
  • 如果我重新加载窗口,地图不会被渲染。

    示例:

  • 如您所见,当前控制台上没有可能导致此问题的错误,所以让我们转到代码。

    代码:


    我有一些注释代码,在这篇文章中被删除了,但是因为它是为 >ionic serve 命令注释的,所以它不应该影响执行

    @Component({
      selector: 'app-map',
      templateUrl: './map.component.html',
      styleUrls: ['./map.component.scss'],
    })
    export class MapComponent implements OnInit, AfterViewInit {
    
      serviceRequestBoundsIcon = '../../../assets/custom/map-outline.svg';
      now: Date;
      private markers: L.Marker[] = [];
      private map: L.Map;
      private iconTruck = L.icon({
        iconUrl: '../../../assets/truck-39103_1280.png',
        iconSize: [40, 20],
        iconAnchor: [20, 10],
        popupAnchor: [1, -34],
        tooltipAnchor: [16, -28],
        shadowSize: [41, 41]
      });
      private iconwatch = L.icon({
        iconUrl: '../../../assets/custom/map-outline.svg',
        iconSize: [40, 20],
        iconAnchor: [20, 10],
        popupAnchor: [1, -34],
        tooltipAnchor: [16, -28],
        shadowSize: [41, 41]
      });
      private watchSubscriber: Subscription;
    
    
    
      constructor() {
      }
    
      ngOnInit() {
        this.initMap();
        console.log('oninit');
      }
    
      ngAfterViewInit() {
        this.map.invalidateSize();
        console.log('view init');
      }
    
      private initMap(){
    
        this.map = L.map('map', {
          center: [ 49.00, 10.00 ],
          zoom: 6
        });
        const tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
          maxZoom: 18,
          minZoom: 3,
          attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
        });
        tiles.addTo(this.map);
      }
    
    • 在这里,我尝试将 initMap()this.map.invalidateSize() 移动到不同的钩子(ngAfterViewInit、ngAfterContentInit、ngOnInit、构造函数),结果几乎不一样,有时它可以工作,但在重新加载时停止工作......
  • 这个角度地图组件位于离子标签生成器制作的位置标签页内,位置模块上没有逻辑。
  • 我还尝试了在 SO 的帖子中创建的 MutationObserver 解决方案。暂时没用。
    private renderMap(){
        const observer = new MutationObserver(() => {
          console.log('init mutation');
          this.map.invalidateSize();
          observer.disconnect();
          console.log('cancel observer');
        });
        console.log('init observer');
        observer.observe(document.documentElement, {
          attributes: true,
          attributeFilter: ['class'],
          childList: false,
          characterData: false,
        });
      }
    

  • index.html 内:
  • <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"/>
    
  • angular.json:
  •   "projects": {
    "app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "www",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              {
                "glob": "**/*",
                "input": "src/assets",
                "output": "assets"
              },
              {
                "glob": "**/*.svg",
                "input": "node_modules/ionicons/dist/ionicons/svg",
                "output": "./svg"
              },
              {
                "glob": "**/*",
                "input": "node_modules/leaflet/dist/images/",
                "output": "assets"
              },
              "src/manifest.webmanifest",
              "src/manifest.webmanifest",
              "src/manifest.webmanifest",
              "src/manifest.webmanifest"
            ],
            "styles": ["./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", "node_modules/leaflet/dist/leaflet.css", "src/theme/variables.scss", "src/global.scss"],
            "scripts": [],
            "aot": false,
            "vendorChunk": true,
            "extractLicenses": false,
            "buildOptimizer": false,
            "sourceMap": true,
            "optimization": false,
            "namedChunks": true,
            "serviceWorker": true,
            "ngswConfigPath": "ngsw-config.json"
          },
    

    我希望我能在社区的帮助下解决这个问题,我不明白这里会发生什么!如果需要更多详细信息,请随时询问,我会关注这个帖子!

    【问题讨论】:

      标签: angular ionic-framework leaflet ionic5 angular12


      【解决方案1】:

      当我能够解决添加setTimeout(()=&gt;{this.map.invalidateSize();},100) 的问题时,我想知道是否有更好的解决方案,请告诉我!

      【讨论】:

        猜你喜欢
        • 2019-07-26
        • 2014-03-02
        • 2022-08-05
        • 2021-04-13
        • 1970-01-01
        • 1970-01-01
        • 2016-07-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多