My approach involved the following steps:
import { AfterViewInit, Component, OnInit } from '@angular/core';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit, AfterViewInit {
map!: Map;
ngOnInit(): void {
let view = new View({
center: [0, 0],
zoom: 1,
});
let layers = [
new TileLayer({
source: new OSM(),
}),
];
this.map = new Map({
view: view,
layers: layers,
target: 'ol-map',
});
this.map.updateSize();
}
ngAfterViewInit(): void {
this.map.updateSize();
}
}
I believe that my solution offers a concise and effective way to tackle this issue by initializing the map in ngOnInit
and updating its size once the view is ready in ngAfterViewInit
. This method provides simplicity and efficiency.