原文链接及内容

运行界面

WMTS数据源对象有一个tilePixelRatio选项,支持HiDPI的WMTS可以提供512x512像素的切片,但要在256x256像素的切片网格中使用它们,需要将tilePixelRatio设置为2

main.js代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Map from 'ol/Map.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import WMTS, {optionsFromCapabilities} from 'ol/source/WMTS.js';
import WMTSCapabilities from 'ol/format/WMTSCapabilities.js';
import {DEVICE_PIXEL_RATIO} from 'ol/has.js';

const capabilitiesUrl = 'https://basemap.at/wmts/1.0.0/WMTSCapabilities.xml';

// HiDPI support:
// * Use 'bmaphidpi' layer (pixel ratio 2) for device pixel ratio > 1
// * Use 'geolandbasemap' layer (pixel ratio 1) for device pixel ratio == 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代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>High 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>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>