gpt4 book ai didi

angular - Ionic-Framework (4) - Openlayers map 不工作/可见

转载 作者:太空狗 更新时间:2023-10-29 18:35:57 24 4
gpt4 key购买 nike

我尝试将 Openlayers 与 Ionic 一起使用,但 map 在 setTimeout 之前不可见。

这是我的工作代码:

import { Component, OnInit } from '@angular/core';

import OlMap from 'ol/map';
import OlOSM from 'ol/source/osm';
import OlTileLayer from 'ol/layer/tile';
import OlView from 'ol/view';
import OlProj from 'ol/proj';

@Component({
selector: 'app-tab1',
template: '<ion-content><div id="map" class="map"></div></ion-content>',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page implements OnInit {
map: OlMap;
ngOnInit() {
const layer = new OlTileLayer({
source: new OlOSM({ url: 'https://{a- b}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png' })
});

setTimeout(() => this.map = new OlMap({
target: 'map',
layers: [layer],
view: new OlView({ zoom: 13, center: OlProj.fromLonLat([42, 10]) }),
controls: []
}), 0);
}
}

应用程序模块.ts:

@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})

但是如果我不使用setTimeout, map 是不可见的...

我也曾尝试将代码放入 NgAfterViewInit 中,但这也不起作用。

对我来说,使用 setTimeout 似乎是一种肮脏的解决方法。有谁知道为什么会出现这个问题或如何以更好的方式解决它?

(我的行为与 Angular 和 BrowserAnimationsModule 的行为相似。see here)

最佳答案

使用我最近在您提到的那个中发布的答案创建一个组件

https://stackoverflow.com/a/54281422/2701198

然后在您的页面中使用该组件。

希望对您有所帮助。

关于angular - Ionic-Framework (4) - Openlayers map 不工作/可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54105934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com