: 本例使用的功能不是稳定API的一部分,可能会在不同版本之间发生变化。请参考API文档以了解最新版本中支持的内容。

原文链接及内容

运行界面

使用WebGL渲染一副简单的Mapbox矢量切片地图。:使用此示例时,请确保获取您自己的Mapbox API密钥。当API密钥过期时,将看不到地图。

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import MVT from 'ol/format/MVT.js';
import Map from 'ol/Map.js';
import VectorTile from 'ol/layer/VectorTile.js';
import VectorTileSource from 'ol/source/VectorTile.js';
import View from 'ol/View.js';
import WebGLVectorTileLayerRenderer from 'ol/renderer/webgl/VectorTileLayer.js';
import {Fill, Icon, Stroke, Style, Text} from 'ol/style.js';
import {asArray} from 'ol/color.js';
import {packColor, parseLiteralStyle} from 'ol/webgl/styleparser.js';

const key =
'Your Mapbox access token from https://mapbox.com/ here';

const result = parseLiteralStyle({
'fill-color': ['get', 'fillColor'],
'stroke-color': ['get', 'strokeColor'],
'stroke-width': ['get', 'strokeWidth'],
'circle-radius': 4,
'circle-fill-color': '#777',
});

class WebGLVectorTileLayer extends VectorTile {
createRenderer() {
return new WebGLVectorTileLayerRenderer(this, {
style: {
fill: {
fragment: result.builder.getFillFragmentShader(),
vertex: result.builder.getFillVertexShader(),
},
stroke: {
fragment: result.builder.getStrokeFragmentShader(),
vertex: result.builder.getStrokeVertexShader(),
},
symbol: {
fragment: result.builder.getSymbolFragmentShader(),
vertex: result.builder.getSymbolVertexShader(),
},
attributes: {
fillColor: {
size: 2,
callback: (feature) => {
const style = this.getStyle()(feature, 1)[0];
const color = asArray(style?.getFill()?.getColor() || '#eee');
return packColor(color);
},
},
strokeColor: {
size: 2,
callback: (feature) => {
const style = this.getStyle()(feature, 1)[0];
const color = asArray(style?.getStroke()?.getColor() || '#eee');
return packColor(color);
},
},
strokeWidth: {
size: 1,
callback: (feature) => {
const style = this.getStyle()(feature, 1)[0];
return style?.getStroke()?.getWidth() * 2 || 0;
},
},
},
},
});
}
}

const map = new Map({
layers: [
new WebGLVectorTileLayer({
source: new VectorTileSource({
attributions:
'© <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ' +
'© <a href="https://www.openstreetmap.org/copyright">' +
'OpenStreetMap contributors</a>',
format: new MVT(),
url:
'https://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/' +
'{z}/{x}/{y}.vector.pbf?access_token=' +
key,
}),
style: createMapboxStreetsV6Style(Style, Fill, Stroke, Icon, Text),
}),
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 2,
}),
});

界面布局文件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>WebGL Vector Tiles Layer</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>

<script src="https://openlayers.org/en/v8.1.0/examples/resources/mapbox-streets-v6-style.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>