简体   繁体   English

LeafletJS 直到移动地图才加载所有图块

[英]LeafletJS not loading all tiles until moving map

I am trying to load a simple leaflet map in my Ionic 2 app.我正在尝试在我的 Ionic 2 应用程序中加载一个简单的传单地图。 Unfortunately not all tiles are loaded currectly until a moving the map.不幸的是,在移动地图之前,并非所有图块都被正确加载。

this.map = new L.Map('mainmap', {
      zoomControl: false,
      center: new L.LatLng(40.731253, -73.996139),
      zoom: 12,
      minZoom: 4,
      maxZoom: 19,
      layers: [this.mapService.baseMaps.OpenStreetMap],
      attributionControl: false
    });

在此处输入图片说明

There are a couple of solutions for this problem:这个问题有几个解决方案:

1- Add "./node_modules/leaflet/dist/leaflet.css" in the styles array in `angular.json'. 1-在`angular.json'的样式数组中添加"./node_modules/leaflet/dist/leaflet.css"

2- Invalidate size when a map is ready: 2-当地图准备好时使大小无效:

onMapReady(map: L.Map) {
    setTimeout(() => {
      map.invalidateSize();
    }, 0);
}

Add this to your template:将此添加到您的模板中:

<div style="height: 300px;"
   leaflet
   (leafletMapReady)="onMapReady($event)">
</div>

And this will bind onMapReady method which you have in your component.这将绑定您在组件中拥有的onMapReady方法。

3- Install Leaflet typings for Typescript: 3-为 Typescript 安装传单类型:

npm install --save-dev @types/leaflet

Vanilla JavaScript:原生 JavaScript:

1- Validate the size of map: 1-验证地图的大小:

onMapReady(map: L.Map) {
   setTimeout(() => {
     map.invalidateSize();
   }, 0);
}

2- Add leaflet stylesheet leaflet/dist/leaflet.css in the <head> of your document. 2-在文档的<head>中添加传单样式表leaflet/dist/leaflet.css

this work for me fine :这对我来说很好:

this.map = L.map('map');
const self = this;
    
this.map.on("load",function() { setTimeout(() => {
   self.map.invalidateSize();
}, 1); });
    
this.map.setView([36.3573539, 59.487427], 13);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Leaflet.js地图数据与Angular.js同步 - Synchronize Leafletjs map data with Angularjs LeafletJS:地图容器已经初始化 - LeafletJS: Map container is already initialized ui-bootstrap选项卡中的Mapbox映射未加载图块 - Mapbox map in ui-bootstrap tab not loading tiles 为什么并非所有Google Map Tiles都是在AngularJS应用程序的初始加载时呈现的? - Why are not all Google Map Tiles rendered on the initial load of an AngularJS app? 延迟加载主页,直到加载所有模板 - Delay Loading of Homepage Until All Templates are Loaded 在根级别显示加载指示器,直到完成所有路由解析 - Show loading indicator at root level until all route resolves are complete Leaflet map 未完全加载。 在开始时它正确加载,但是当我想以大的高度和宽度打开它时,它只会加载一半的瓷砖 - Leaflet map not loading fully. At begining it load properly,but when I want open with large height and width it wont working only half of tiles loaded 显示angular-loading-bar并禁用页面的所有内容,直到页面加载 - Show angular-loading-bar And disable all contents of the page until page loads 当通过angular指令使用geojson数据时,为什么它没有显示在leafletjs地图上? - why doesn't the geojson data show up on a leafletjs map when used via an angular directive? 与Leafletjs的angularjs - angularjs with Leafletjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM