无论我在 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>
我搜索了很多帖子并遵循教程,但没有什么能让我加载一张漂亮的地图。
任何人都可以帮忙吗?
谢谢,莫阿