【发布时间】:2019-07-26 13:41:21
【问题描述】:
无论我在 Angular 7 中尝试哪种方式,我的传单地图都无法正确加载。我得到一个块拼图,一半是灰色的,另一半是随机脱节的地图块(见图)。
地图块拼图:
我的 HTML 是:
<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 在 Imports 中添加了“LeafletModule.forRoot()”。 invalidateSize() 对我不起作用,尽管也许我用错了。我是用 setTimeout 做的,而不是在方法调用中做的。
我错过了什么吗?我是否必须像这样向 INDEX.HTML 添加脚本?
<script src="http://cdn.leafletjs.com/leaflet-0.4/leaflet.js"></script>
我搜索了很多帖子并关注了教程,但没有什么能让我加载一张漂亮的地图。
谁能帮忙?
谢谢, 恐鸟
【问题讨论】:
-
我之前也遇到过同样的问题,只是为了确定,一旦地图加载完毕,调整浏览器页面的大小可以让地图正确加载?
-
当我调整浏览器大小时,地图仍然是 gobbledegook。所以 invalidateSize() 不是问题。
标签: angular typescript leaflet angular-leaflet-directive