Skip to content

瓦片加载事件 ​

瓦片加载事件示例。 瓦片 events 加载

图像瓦片源会触发与瓦片加载相关的事件。你可以监听 tileloadstart、tileloadend 和 tileloaderror 类型的事件来监控瓦片加载进度。本示例为这些事件注册了监听器,并在地图底部渲染一个瓦片加载进度条。进度条会根据地图的 loadstart 和 loadend 事件进行显示和隐藏。

main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import ImageTile from 'ol/source/ImageTile.js';

/**
 * 渲染一个进度条。
 * @param {HTMLElement} el 目标元素。
 * @class
 */
function Progress(el) {
  this.el = el;
  this.loading = 0;
  this.loaded = 0;
}

/**
 * 增加正在加载的瓦片计数。
 */
Progress.prototype.addLoading = function () {
  ++this.loading;
  this.update();
};

/**
 * 增加已加载的瓦片计数。
 */
Progress.prototype.addLoaded = function () {
  ++this.loaded;
  this.update();
};

/**
 * 更新进度条。
 */
Progress.prototype.update = function () {
  const width = ((this.loaded / this.loading) * 100).toFixed(1) + '%';
  this.el.style.width = width;
};

/**
 * 显示进度条。
 */
Progress.prototype.show = function () {
  this.el.style.visibility = 'visible';
};

/**
 * 隐藏进度条。
 */
Progress.prototype.hide = function () {
  const style = this.el.style;
  setTimeout(function () {
    style.visibility = 'hidden';
    style.width = '0px';
  }, 250);
};

const progress = new Progress(document.getElementById('progress'));

const key = 'Get your own API key at https://www.maptiler.com/cloud/';
const attributions =
  '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> ' +
  '<a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>';

const source = new ImageTile({
  attributions: attributions,
  url: 'https://api.maptiler.com/maps/streets-v2/{z}/{x}/{y}.png?key=' + key,
  tileSize: 512,
});

source.on('tileloadstart', function () {
  progress.addLoading();
});
source.on(['tileloadend', 'tileloaderror'], function () {
  progress.addLoaded();
});

const map = new Map({
  layers: [new TileLayer({source: source})],
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 2,
  }),
});

map.on('loadstart', function () {
  progress.show();
});
map.on('loadend', function () {
  progress.hide();
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>瓦片加载事件</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
      .map {
        background: #85ccf9;
      }
      .wrapper {
        position: relative;
      }
      #progress {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 0;
        box-shadow: 0px 0px 1px 2px rgb(255,204,0);
        width: 0;
        transition: width 250ms;
      }
    </style>
  </head>
  <body>
    <div class="wrapper">
      <div id="map" class="map"></div>
      <div id="progress"></div>
    </div>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
json
{
  "name": "tile-load-events",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}