无论我在 Angular 7 中尝试哪种方法,我的 Leaflet 地图都无法正确加载。我得到了一个方块拼图,在屏幕的一半是灰色的,另一半是随意不连贯的地图块(见图片)。
地图方块拼图:
我的 HTML 可以是以下之一:
地图方块拼图:
![](https://istack.dev59.com/h3Bo4.webp)
<div
leaflet
[leafletOptions]="options">
</div>
或者
<div id="map" class="map"></div>
我的组件是:
import * as L from "leaflet";
...
@Component( {
styleUrls: [ "../../../../node_modules/leaflet/dist/leaflet.css" ],
templateUrl: "./map.html"
} )
...
export class Map implements OnInit {
map: any;
options = {
layers: [
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 7, attribution: '...' })],
zoom: 5,
center: L.latLng([ 46.879966, -121.726909 ])};
async ngOnInit() {
this.map = L.map('map').setView( [37.8, -96], 4 );
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
var osm = new L.TileLayer( osmUrl, { minZoom: 1, maxZoom: 7, attribution: osmAttrib } );
this.map.addLayer( osm ); // */
}
}
我的app-module.ts文件中已经添加了"LeafletModule.forRoot()"到导入模块中。
invalidateSize()方法对我来说没有起作用,尽管可能是我使用不正确。我是通过setTimeout而不是方法调用来实现的。
我是否遗漏了什么?我需要像这样在INDEX.HTML中添加一个脚本吗?
<script src="http://cdn.leafletjs.com/leaflet-0.4/leaflet.js"></script>
我搜索了很多帖子并跟随教程,但没有一个能够加载漂亮的地图。
有人能帮忙吗?
谢谢, Moa