原文链接及内容

运行界面

本例中显示的是IGN的WMTS图层。有关IGN的WMTS服务的更多信息,请参阅IGN gsamoportail APIDocumentation de l’offre de donnsames et services de l’IGN(法文)。

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
45
46
47
48
49
50
51
52
53
54
import Map from 'ol/Map.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import WMTS from 'ol/source/WMTS.js';
import WMTSTileGrid from 'ol/tilegrid/WMTS.js';
import {fromLonLat, get as getProjection} from 'ol/proj.js';
import {getWidth} from 'ol/extent.js';

const map = new Map({
target: 'map',
view: new View({
zoom: 5,
center: fromLonLat([5, 45]),
}),
});

const resolutions = [];
const matrixIds = [];
const proj3857 = getProjection('EPSG:3857');
const maxResolution = getWidth(proj3857.getExtent()) / 256;

for (let i = 0; i < 20; i++) {
matrixIds[i] = i.toString();
resolutions[i] = maxResolution / Math.pow(2, i);
}

const tileGrid = new WMTSTileGrid({
origin: [-20037508, 20037508],
resolutions: resolutions,
matrixIds: matrixIds,
});

// For more information about the IGN API key see
// https://geoservices.ign.fr/blog/2021/01/29/Maj_Cles_Geoservices.html

const ign_source = new WMTS({
url: 'https://wxs.ign.fr/choisirgeoportail/geoportail/wmts',
layer: 'GEOGRAPHICALGRIDSYSTEMS.PLANIGNV2',
matrixSet: 'PM',
format: 'image/png',
projection: 'EPSG:3857',
tileGrid: tileGrid,
style: 'normal',
attributions:
'<a href="https://www.ign.fr/" target="_blank">' +
'<img src="https://wxs.ign.fr/static/logos/IGN/IGN.gif" title="Institut national de l\'' +
'information géographique et forestière" alt="IGN"></a>',
});

const ign = new TileLayer({
source: ign_source,
});

map.addLayer(ign);

界面布局文件index.html代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>IGN WMTS</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</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>