类:图像瓦片

ol/ImageTile~图像瓦片


继承

方法

dispatchEvent(event){boolean | undefined} inherited

触发事件并调用所有监听此类型事件的监听器。事件参数可以是字符串或具有 type 属性的对象。

Name Type 描述
event BaseEvent | string

事件对象。

返回:
false 如果事件对象上有人调用了 preventDefault,或任何监听器返回 false。

getImage(){HTMLCanvasElement | OffscreenCanvas | HTMLImageElement | HTMLVideoElement}

获取此瓦片的HTML图像元素(可能是Canvas、OffscreenCanvas、Image或Video)。

返回:
图像。

getTileCoord(){TileCoord} inherited

获取该瓦片的瓦片坐标。

返回:
瓦片坐标。

加载图像;若先前加载失败,则重试。加载操作由瓦片队列处理,仅当需要预加载或发生错误需重新加载时,才需调用此方法。

对于失败的请求,若要重试加载瓦片,请使用自定义的 tileLoadFunction 来检查错误状态码,并仅当状态码为 408、429、500、502、503 或 504,且重试次数未超过限制时,才重新加载:

const retryCodes = [408, 429, 500, 502, 503, 504];
const retries = {};
source.setTileLoadFunction((tile, src) => {
  const image = tile.getImage();
  fetch(src)
    .then((response) => {
      if (retryCodes.includes(response.status)) {
        retries[src] = (retries[src] || 0) + 1;
        if (retries[src] <= 3) {
          setTimeout(() => tile.load(), retries[src] * 1000);
        }
        return Promise.reject();
      }
      return response.blob();
    })
    .then((blob) => {
      const imageUrl = URL.createObjectURL(blob);
      image.src = imageUrl;
      setTimeout(() => URL.revokeObjectURL(imageUrl), 5000);
    })
    .catch(() => tile.setState(3)); // error
});

setState(state) inherited

设置此瓦片的状态。若您自定义 tileLoadFunction,则当瓦片无法加载时,必须正确将状态设置为 ERROR。否则,瓦片将无法从瓦片队列中移除,并会阻塞其他请求。

Name Type 描述
state ol/TileState

状态。