我正在尝试使用Ionic加载OL地图。当我用“离子服务”加载时,地图在浏览器中加载正常。问题是,当我在手机中加载地图时,地图质量真的很差。当地图容器更改大小并且地图不再加载时,这在我的Web应用程序中发生过一次。在网络应用中,我通过应用一个在容器更改大小后重新加载地图的函数对其进行了修复,如下所示:
$('#containerMap').on('change', function () {
setTimeout(function(){ map.updateSize(); }, 500);
});
我试图在home.ts页面中应用相同的功能,但没有成功。 经过研究后,我发现有人说正确的地图加载方式是使用Ionic Components,但我无法从示例中复制出来。
Ionic v4和OpenLayers v5.3
离子成分相关答案:
我遵循的其他示例:
我想做的事情(home-page.ts):
import { Component, Renderer, ViewChild } from '@angular/core';
import { NavController, Platform } from '@ionic/angular';
import { Map } from 'ol';
import { OSM, Vector as VectorSource } from 'ol/source.js';
import Point from 'ol/geom/Point.js';
import Feature from 'ol/Feature';
import { transform } from 'ol/proj.js';
import { Icon, Style } from 'ol/style.js';
import { Vector as VectorLayer } from 'ol/layer.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
@ViewChild('map') map;
constructor(platform: Platform, public renderer: Renderer) {
platform.ready().then(() => {
console.log("Platform is ready");
setTimeout(() => {
this.loadMap();
}, 1000);
})
}
// I tried to load the map with this function first, but it was not loaded.
//ionViewDidLoad() {
// this.loadMap();
//}
loadMap() {
console.log('Hello');
var map = new Map({
layers: [
new TileLayer({
source: new OSM()
})],
target: document.getElementById('map'),
view: new View({
center: [0, 0],
zoom: 3
})
});
map.updateSize(); //Tried to apply the same function used in web
}
}