原文链接及内容

运行界面

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

演示如何在OpenLayers地图中添加mapbox-gl-js图层。注意:在使用此示例时,请确保在https://www.maptiler.com/cloud/ 上获取自己的API密钥。当API密钥过期时,将看不到地图。在这个例子中使用Mapbox-gl-js版本是1.13.0,因为该版本之后的版本需要Mapbox访问令牌,并且与旧的浏览器不兼容。

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
import GeoJSON from 'ol/format/GeoJSON.js';
import HeatmapLayer from 'ol/layer/Heatmap.js';
import Layer from 'ol/layer/Layer.js';
import Map from 'ol/Map.js';
import Source from 'ol/source/Source.js';
import VectorSource from 'ol/source/Vector.js';
import View from 'ol/View.js';
import {fromLonLat, toLonLat} from 'ol/proj.js';

const center = [-98.8, 37.9];
const key = 'Get your own API key at https://www.maptiler.com/cloud/';

const mbMap = new mapboxgl.Map({
style: 'https://api.maptiler.com/maps/bright/style.json?key=' + key,
attributionControl: false,
boxZoom: false,
center: center,
container: 'map',
doubleClickZoom: false,
dragPan: false,
dragRotate: false,
interactive: false,
keyboard: false,
pitchWithRotate: false,
scrollZoom: false,
touchZoomRotate: false,
});

const mbLayer = new Layer({
render: function (frameState) {
const canvas = mbMap.getCanvas();
const viewState = frameState.viewState;

const visible = mbLayer.getVisible();
canvas.style.display = visible ? 'block' : 'none';
canvas.style.position = 'absolute';

const opacity = mbLayer.getOpacity();
canvas.style.opacity = opacity;

// adjust view parameters in mapbox
const rotation = viewState.rotation;
mbMap.jumpTo({
center: toLonLat(viewState.center),
zoom: viewState.zoom - 1,
bearing: (-rotation * 180) / Math.PI,
animate: false,
});

// cancel the scheduled update & trigger synchronous redraw
// see https://github.com/mapbox/mapbox-gl-js/issues/7893#issue-408992184
// NOTE: THIS MIGHT BREAK IF UPDATING THE MAPBOX VERSION
if (mbMap._frame) {
mbMap._frame.cancel();
mbMap._frame = null;
}
mbMap._render();

return canvas;
},
source: new Source({
attributions: [
'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a>',
'<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>',
],
}),
});

const cities = new HeatmapLayer({
source: new VectorSource({
url: 'data/geojson/world-cities.geojson',
format: new GeoJSON(),
}),
weight: function (feature) {
return feature.get('population') / 1e7;
},
radius: 15,
blur: 15,
});

const map = new Map({
target: 'map',
view: new View({
center: fromLonLat(center),
zoom: 4,
}),
layers: [mbLayer, cities],
});

界面布局文件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
24
25
26
27
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mapbox-gl Layer</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mapbox-gl/1.13.0/mapbox-gl.css">
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
/* Reset font size changed by Mapbox CSS */
.map {
font-size: medium;
font-family: 'Quattrocento Sans', sans-serif;
}
</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 src="https://cdnjs.cloudflare.com/ajax/libs/mapbox-gl/1.13.0/mapbox-gl.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>