原文链接及内容

运行界面

渲染从maptiler.com加载的EPSG: 4326投影的矢量切片数据的示例(使用ol-mapbox-style设置样式)。注:使用本例时,请务必在 https://www.maptiler.com/cloud/ 获取你自己的API key。当API key过期时,将无法加载地图数据。

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
import VectorTileLayer from 'ol/layer/VectorTile.js';
import VectorTileSource from 'ol/source/VectorTile.js';
import {Map, View} from 'ol/index.js';
import {applyBackground, applyStyle} from 'ol-mapbox-style';
import {createXYZ} from 'ol/tilegrid.js';

const key = 'Get your own API key at https://www.maptiler.com/cloud/';
const url = 'https://api.maptiler.com/maps/basic-4326/style.json?key=' + key;

// Match the server resolutions
const tileGrid = createXYZ({
extent: [-180, -90, 180, 90],
tileSize: 512,
maxResolution: 180 / 512,
maxZoom: 13,
});

const layer = new VectorTileLayer({
declutter: true,
source: new VectorTileSource({
projection: 'EPSG:4326',
tileGrid: tileGrid,
}),
});
applyStyle(layer, url, '', {resolutions: tileGrid.getResolutions()});
applyBackground(layer, url);

const map = new Map({
target: 'map',
layers: [layer],
view: new View({
projection: 'EPSG:4326',
zoom: 0,
center: [0, 30],
}),
});

界面布局文件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>Vector tiles in EPSG:4326</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map" style="background:none;"></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>