Skip to content

高 DPI WMTS ​

高 DPI WMTS示例。 hidpi retina WMTS

WMTS 数据源有一个 tilePixelRatio 选项。支持 HiDPI 的 WMTS 可以提供 512x512 像素的瓦片,但将其用于 256x256 像素的瓦片网格中。在这种情况下,需要将 tilePixelRatio 设置为 2。

main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import WMTSCapabilities from 'ol/format/WMTSCapabilities.js';
import {DEVICE_PIXEL_RATIO} from 'ol/has.js';
import TileLayer from 'ol/layer/Tile.js';
import WMTS, {optionsFromCapabilities} from 'ol/source/WMTS.js';

const capabilitiesUrl =
  'https://mapsneu.wien.gv.at/basemapneu/1.0.0/WMTSCapabilities.xml';

// HiDPI 支持:
// * 设备像素比 > 1 时使用 'bmaphidpi' 图层(像素比 2)
// * 设备像素比 == 1 时使用 'geolandbasemap' 图层(像素比 1)
const hiDPI = DEVICE_PIXEL_RATIO > 1;
const layer = hiDPI ? 'bmaphidpi' : 'geolandbasemap';
const tilePixelRatio = hiDPI ? 2 : 1;

const map = new Map({
  target: 'map',
  view: new View({
    center: [1823849, 6143760],
    zoom: 11,
  }),
});

fetch(capabilitiesUrl)
  .then(function (response) {
    return response.text();
  })
  .then(function (text) {
    const result = new WMTSCapabilities().read(text);
    const options = optionsFromCapabilities(result, {
      layer: layer,
      matrixSet: 'google3857',
      style: 'normal',
    });
    options.tilePixelRatio = tilePixelRatio;
    options.attributions =
      'Grundkarte: <a target="_blank" href="https://basemap.at/">basemap.at</a>';
    map.addLayer(
      new TileLayer({
        source: new WMTS(options),
      }),
    );
  });
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>高 DPI WMTS</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
      .map {
        background: white;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>

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