Openlayers 地图图块最初未在单页应用程序中加载

Openlayers map tiles are not initially loaded in single page app

我有一个使用

构建的单页应用程序

地图容器已加载并具有带 ol- 类 的子元素以及左上角的缩放控件。这样Openlayers脚本就执行成功了。 但加载时不显示地图图块。

当我调整浏览器大小时,地图图块突然出现。所以我想知道:

什么事件会触发浏览器调整大小时突然渲染图块,我如何自己触发它以便地图在加载时正确显示?

我的 index.pug 看起来像这样:

doctype html
html(lang='de')
  head
    meta(charset='UTF-8')
    meta(http-equiv='X-UA-Compatible', content='ie=edge')
    meta(
      name='viewport'
      content='width=device-width, initial-scale=1')
    title=`myTitle`

    // Stylesheets
    link(
      rel='stylesheet',
      href='https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css')
    link(
      rel='stylesheet',
      href='/assets/style.css')

    // Scripts
    script(src='https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js')
    script(src='https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList')
    script(src='https://code.jquery.com/jquery-3.3.1.min.js')
    script(data-main='/js/main', src='/js/require.js')

  body
    include header
    include tabs

    main
      #loader Loading...
      include map

    include footer

main 部分,您会看到包含的 map 哈巴狗模板,如下所示:

section.component#component-map
  #map.map
  script.
    /**
    * Leaflet Map
    */
    // Create markers and geodata
    const mapCenter = [13.429, 52.494];
    const siteData = !{JSON.stringify(sites)};
    const features = siteData.map(site => {
      return {
        "type": "Feature",
        "geometry": {
          "type": "Point",
          "coordinates": [ Number(site.longitude), Number(site.latitude) ]
        }
      }
    });

    const image = new ol.style.Circle({
      radius: 5,
      fill: null,
      stroke: new ol.style.Stroke({color: "red", width: 1})
    });

    const styles = {
      "Point": new ol.style.Style({
        "image": image
      })
    }
    const styleFunction = function(feature) {
      return styles[feature.getGeometry().getType()];
    };

    const geojsonObject = {
      "type": "FeatureCollection",
      "features": features
    };

    const vectorSource = new ol.source.Vector({
      features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
    });

    const vectorLayer = new ol.layer.Vector({
      source: vectorSource,
      style: styleFunction
    });

    const map = new ol.Map({
      target: "map",
      layers: [
        new ol.layer.Tile({
          source: new ol.source.OSM()
        }),
        vectorLayer
      ],
      view: new ol.View({
        center: ol.proj.fromLonLat(mapCenter),
        zoom: 17,
      })
    });

自己找到了答案。我的 "solution" 更像是一种解决方法,而不是实际的解决方案。我等待执行地图脚本(当地图容器具有名称为 class 的子元素 ol-viewport 时就是这种情况),然后我手动触发浏览器 resize 事件。与我预期的不同,map.render()map.renderSync() 方法不加载图块。 所以解决方法如下所示:

const waitForMap = setInterval(function() {
  if ($('.ol-viewport').length) {
      console.log("Exists!");
      window.dispatchEvent(new Event('resize'));
      clearInterval(waitForMap);
  }
}, 100);