原文链接及内容

运行界面

上述地图由两个图层组组成:水彩基础图层,来自Stamen的地形标签图层。

注:Stadia Maps的图层不需要API密钥来localhost开发或大多数生产网络部署,详见:https://docs.stadiamaps.com/authentication/

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
import Map from 'ol/Map.js';
import StadiaMaps from 'ol/source/StadiaMaps.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import {fromLonLat} from 'ol/proj.js';

const map = new Map({
layers: [
// NOTE: Layers from Stadia Maps do not require an API key for localhost development or most production
// web deployments. See https://docs.stadiamaps.com/authentication/ for details.
new TileLayer({
source: new StadiaMaps({
layer: 'stamen_watercolor',
// apiKey: 'OPTIONAL'
}),
}),
new TileLayer({
source: new StadiaMaps({
layer: 'stamen_terrain_labels',
// apiKey: 'OPTIONAL'
}),
}),
],
target: 'map',
view: new View({
center: fromLonLat([-122.416667, 37.783333]),
zoom: 12,
}),
});

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Stadia Maps (Stamen Tile Layer Composition)</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 type="module" src="main.js"></script>
</body>
</html>