原文链接及内容

运行界面

此示例展示了为文本样式设置多个选项。当选择”Text/Wrap”(例如线要素)时,通过插入字符\n使得标签换行,这将创建多行标签。“Open Sans”web字体将按需加载,以展示动态字体的加载。

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import GeoJSON from 'ol/format/GeoJSON.js';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {
Circle as CircleStyle,
Fill,
Stroke,
Style,
Text,
} from 'ol/style.js';
import {OSM, Vector as VectorSource} from 'ol/source.js';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';

let openSansAdded = false;

const myDom = {
points: {
text: document.getElementById('points-text'),
align: document.getElementById('points-align'),
baseline: document.getElementById('points-baseline'),
rotation: document.getElementById('points-rotation'),
font: document.getElementById('points-font'),
weight: document.getElementById('points-weight'),
size: document.getElementById('points-size'),
height: document.getElementById('points-height'),
offsetX: document.getElementById('points-offset-x'),
offsetY: document.getElementById('points-offset-y'),
color: document.getElementById('points-color'),
outline: document.getElementById('points-outline'),
outlineWidth: document.getElementById('points-outline-width'),
maxreso: document.getElementById('points-maxreso'),
},
lines: {
text: document.getElementById('lines-text'),
align: document.getElementById('lines-align'),
baseline: document.getElementById('lines-baseline'),
rotation: document.getElementById('lines-rotation'),
font: document.getElementById('lines-font'),
weight: document.getElementById('lines-weight'),
placement: document.getElementById('lines-placement'),
maxangle: document.getElementById('lines-maxangle'),
overflow: document.getElementById('lines-overflow'),
size: document.getElementById('lines-size'),
height: document.getElementById('lines-height'),
offsetX: document.getElementById('lines-offset-x'),
offsetY: document.getElementById('lines-offset-y'),
color: document.getElementById('lines-color'),
outline: document.getElementById('lines-outline'),
outlineWidth: document.getElementById('lines-outline-width'),
maxreso: document.getElementById('lines-maxreso'),
},
polygons: {
text: document.getElementById('polygons-text'),
align: document.getElementById('polygons-align'),
baseline: document.getElementById('polygons-baseline'),
rotation: document.getElementById('polygons-rotation'),
font: document.getElementById('polygons-font'),
weight: document.getElementById('polygons-weight'),
placement: document.getElementById('polygons-placement'),
maxangle: document.getElementById('polygons-maxangle'),
overflow: document.getElementById('polygons-overflow'),
size: document.getElementById('polygons-size'),
height: document.getElementById('polygons-height'),
offsetX: document.getElementById('polygons-offset-x'),
offsetY: document.getElementById('polygons-offset-y'),
color: document.getElementById('polygons-color'),
outline: document.getElementById('polygons-outline'),
outlineWidth: document.getElementById('polygons-outline-width'),
maxreso: document.getElementById('polygons-maxreso'),
},
};

const getText = function (feature, resolution, dom) {
const type = dom.text.value;
const maxResolution = dom.maxreso.value;
let text = feature.get('name');

if (resolution > maxResolution) {
text = '';
} else if (type == 'hide') {
text = '';
} else if (type == 'shorten') {
text = text.trunc(12);
} else if (
type == 'wrap' &&
(!dom.placement || dom.placement.value != 'line')
) {
text = stringDivider(text, 16, '\n');
}

return text;
};

const createTextStyle = function (feature, resolution, dom) {
const align = dom.align.value;
const baseline = dom.baseline.value;
const size = dom.size.value;
const height = dom.height.value;
const offsetX = parseInt(dom.offsetX.value, 10);
const offsetY = parseInt(dom.offsetY.value, 10);
const weight = dom.weight.value;
const placement = dom.placement ? dom.placement.value : undefined;
const maxAngle = dom.maxangle ? parseFloat(dom.maxangle.value) : undefined;
const overflow = dom.overflow ? dom.overflow.value == 'true' : undefined;
const rotation = parseFloat(dom.rotation.value);
if (dom.font.value == "'Open Sans'" && !openSansAdded) {
const openSans = document.createElement('link');
openSans.href = 'https://fonts.googleapis.com/css?family=Open+Sans';
openSans.rel = 'stylesheet';
document.head.appendChild(openSans);
openSansAdded = true;
}
const font = weight + ' ' + size + '/' + height + ' ' + dom.font.value;
const fillColor = dom.color.value;
const outlineColor = dom.outline.value;
const outlineWidth = parseInt(dom.outlineWidth.value, 10);

return new Text({
textAlign: align == '' ? undefined : align,
textBaseline: baseline,
font: font,
text: getText(feature, resolution, dom),
fill: new Fill({color: fillColor}),
stroke: new Stroke({color: outlineColor, width: outlineWidth}),
offsetX: offsetX,
offsetY: offsetY,
placement: placement,
maxAngle: maxAngle,
overflow: overflow,
rotation: rotation,
});
};

// Polygons
function polygonStyleFunction(feature, resolution) {
return new Style({
stroke: new Stroke({
color: 'blue',
width: 1,
}),
fill: new Fill({
color: 'rgba(0, 0, 255, 0.1)',
}),
text: createTextStyle(feature, resolution, myDom.polygons),
});
}

const vectorPolygons = new VectorLayer({
source: new VectorSource({
url: 'data/geojson/polygon-samples.geojson',
format: new GeoJSON(),
}),
style: polygonStyleFunction,
});

// Lines
function lineStyleFunction(feature, resolution) {
return new Style({
stroke: new Stroke({
color: 'green',
width: 2,
}),
text: createTextStyle(feature, resolution, myDom.lines),
});
}

const vectorLines = new VectorLayer({
source: new VectorSource({
url: 'data/geojson/line-samples.geojson',
format: new GeoJSON(),
}),
style: lineStyleFunction,
});

// Points
function pointStyleFunction(feature, resolution) {
return new Style({
image: new CircleStyle({
radius: 10,
fill: new Fill({color: 'rgba(255, 0, 0, 0.1)'}),
stroke: new Stroke({color: 'red', width: 1}),
}),
text: createTextStyle(feature, resolution, myDom.points),
});
}

const vectorPoints = new VectorLayer({
source: new VectorSource({
url: 'data/geojson/point-samples.geojson',
format: new GeoJSON(),
}),
style: pointStyleFunction,
});

const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
vectorPolygons,
vectorLines,
vectorPoints,
],
target: 'map',
view: new View({
center: [-8161939, 6095025],
zoom: 8,
}),
});

document
.getElementById('refresh-points')
.addEventListener('click', function () {
vectorPoints.setStyle(pointStyleFunction);
});

document.getElementById('refresh-lines').addEventListener('click', function () {
vectorLines.setStyle(lineStyleFunction);
});

document
.getElementById('refresh-polygons')
.addEventListener('click', function () {
vectorPolygons.setStyle(polygonStyleFunction);
});

/**
* @param {number} n The max number of characters to keep.
* @return {string} Truncated string.
*/
String.prototype.trunc =
String.prototype.trunc ||
function (n) {
return this.length > n ? this.substr(0, n - 1) + '...' : this.substr(0);
};

// https://stackoverflow.com/questions/14484787/wrap-text-in-javascript
function stringDivider(str, width, spaceReplacer) {
if (str.length > width) {
let p = width;
while (p > 0 && str[p] != ' ' && str[p] != '-') {
p--;
}
if (p > 0) {
let left;
if (str.substring(p, p + 1) == '-') {
left = str.substring(0, p + 1);
} else {
left = str.substring(0, p);
}
const right = str.substring(p + 1);
return left + spaceReplacer + stringDivider(right, width, spaceReplacer);
}
}
return str;
}

界面布局文件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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vector Labels</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
.edit-form {
display: inline-block;
margin: 5px;
padding: 10px;
border: 1px solid black;
white-space: nowrap;
}

.edit-form h2 {
font-size: 1.5em;
}

.edit-form input[type="button"] {
float: right;
}

.edit-form-elem label {
display: inline-block;
width: 85px;
}

.edit-form-elem input[type="text"],
.edit-form-elem select {
width: 130px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>

<div class="edit-form">
<input id="refresh-points" type="button" value="Refresh" />
<h2>Points</h2>
<div class="edit-form-elem">
<label for="points-text">Text: </label>
<select id="points-text">
<option value="hide">Hide</option>
<option value="normal">Normal</option>
<option value="shorten" selected="selected">Shorten</option>
<option value="wrap">Wrap</option>
</select>
<br />
<label for="points-maxreso" title="Max Resolution Denominator">MaxReso.: </label>
<select id="points-maxreso">
<option value="38400">38,400</option>
<option value="19200">19,200</option>
<option value="9600">9,600</option>
<option value="4800">4,800</option>
<option value="2400">2,400</option>
<option value="1200" selected="selected">1,200</option>
<option value="600">600</option>
<option value="300">300</option>
<option value="150">150</option>
<option value="75">75</option>
<option value="32">32</option>
<option value="16">16</option>
<option value="8">8</option>
</select>
<br />
<label for="points-align">Align: </label>
<select id="points-align">
<option value="center" selected="selected">Center</option>
<option value="end">End</option>
<option value="left">Left</option>
<option value="right">Right</option>
<option value="start">Start</option>
</select>
<br />
<label for="points-baseline">Baseline: </label>
<select id="points-baseline">
<option value="alphabetic">Alphabetic</option>
<option value="bottom">Bottom</option>
<option value="hanging">Hanging</option>
<option value="ideographic">Ideographic</option>
<option value="middle" selected="selected">Middle</option>
<option value="top">Top</option>
</select>
<br />
<label for="points-rotation">Rotation: </label>
<select id="points-rotation">
<option value="0"></option>
<option value="0.785398164">45°</option>
<option value="1.570796327">90°</option>
</select>
<br />
<label for="points-font">Font: </label>
<select id="points-font">
<option value="Arial" selected="selected">Arial</option>
<option value="'Courier New'">Courier New</option>
<option value="'Open Sans'">Open Sans</option>
<option value="Verdana">Verdana</option>
</select>
<br />
<label for="points-weight">Weight: </label>
<select id="points-weight">
<option value="bold">Bold</option>
<option value="normal" selected="selected">Normal</option>
</select>
<br />
<label for="points-placement">Placement: </label>
<select disabled id="points-placement">
<option value="line">Line</option>
<option value="point" selected="selected">Point</option>
</select>
<br />
<label for="points-maxangle">Max Angle: </label>
<select disabled id="points-maxangle">
<option value="0.7853981633974483" selected="selected">45°</option>
<option value="2.0943951023931953">120°</option>
<option value="6.283185307179586">360°</option>
</select>
<br />
<label for="points-overflow">Exceed Len: </label>
<select disabled id="points-overflow">
<option value="true">True</option>
<option value="false" selected="selected">False</option>
</select>
<br />
<label for="points-size">Size: </label>
<input type="text" value="12px" id="points-size" />
<br />
<label for="points-height">Line height: </label>
<input type="text" value="1" id="points-height" />
<br />
<label for="points-offset-x">Offset X: </label>
<input type="text" value="0" id="points-offset-x" />
<br />
<label for="points-offset-y">Offset Y: </label>
<input type="text" value="0" id="points-offset-y" />
<br />
<label for="points-color">Color: </label>
<input type="text" value="#aa3300" id="points-color" />
<br />
<label for="points-outline" title="Outline Color">O. Color: </label>
<input type="text" value="#ffffff" id="points-outline" />
<br />
<label for="points-outline-width" title="Outline Width">O. Width: </label>
<input type="text" value="3" id="points-outline-width" />
</div>
</div>

<div class="edit-form">
<input id="refresh-lines" type="button" value="Refresh" />
<h2>Lines</h2>
<div class="edit-form-elem">
<label for="lines-text">Text: </label>
<select id="lines-text">
<option value="hide">Hide</option>
<option value="normal">Normal</option>
<option value="shorten">Shorten</option>
<option value="wrap" selected="selected">Wrap</option>
</select>
<br />
<label for="lines-maxreso" title="Max Resolution Denominator">MaxReso.: </label>
<select id="lines-maxreso">
<option value="38400">38,400</option>
<option value="19200">19,200</option>
<option value="9600">9,600</option>
<option value="4800">4,800</option>
<option value="2400">2,400</option>
<option value="1200" selected="selected">1,200</option>
<option value="600">600</option>
<option value="300">300</option>
<option value="150">150</option>
<option value="75">75</option>
<option value="32">32</option>
<option value="16">16</option>
<option value="8">8</option>
</select>
<br />
<label for="lines-align">Align: </label>
<select id="lines-align">
<option value="" selected="selected"></option>
<option value="center">Center</option>
<option value="end">End</option>
<option value="left">Left</option>
<option value="right">Right</option>
<option value="start">Start</option>
</select>
<br />
<label for="lines-baseline">Baseline: </label>
<select id="lines-baseline">
<option value="alphabetic">Alphabetic</option>
<option value="bottom">Bottom</option>
<option value="hanging">Hanging</option>
<option value="ideographic">Ideographic</option>
<option value="middle" selected="selected">Middle</option>
<option value="top">Top</option>
</select>
<br />
<label for="lines-rotation">Rotation: </label>
<select id="lines-rotation">
<option value="0"></option>
<option value="0.785398164">45°</option>
<option value="1.570796327">90°</option>
</select>
<br />
<label for="lines-font">Font: </label>
<select id="lines-font">
<option value="Arial">Arial</option>
<option value="'Courier New'" selected="selected">Courier New</option>
<option value="'Open Sans'">Open Sans</option>
<option value="Verdana">Verdana</option>
</select>
<br />
<label for="lines-weight">Weight: </label>
<select id="lines-weight">
<option value="bold" selected="selected">Bold</option>
<option value="normal">Normal</option>
</select>
<br />
<label for="lines-placement">Placement: </label>
<select id="lines-placement">
<option value="line">Line</option>
<option value="point" selected="selected">Point</option>
</select>
<br />
<label for="lines-maxangle">Max Angle: </label>
<select id="lines-maxangle">
<option value="0.7853981633974483" selected="selected">45°</option>
<option value="2.0943951023931953">120°</option>
<option value="6.283185307179586">360°</option>
</select>
<br />
<label for="lines-overflow">Exceed Len: </label>
<select id="lines-overflow">
<option value="true">True</option>
<option value="false" selected="selected">False</option>
</select>
<br />
<label for="lines-size">Size: </label>
<input type="text" value="12px" id="lines-size" />
<br />
<label for="lines-height">Line height: </label>
<input type="text" value="1.2" id="lines-height" />
<br />
<label for="lines-offset-x">Offset X: </label>
<input type="text" value="0" id="lines-offset-x" />
<br />
<label for="lines-offset-y">Offset Y: </label>
<input type="text" value="0" id="lines-offset-y" />
<br />
<label for="lines-color">Color: </label>
<input type="text" value="green" id="lines-color" />
<br />
<label for="lines-outline" title="Outline Color">O. Color: </label>
<input type="text" value="#ffffff" id="lines-outline" />
<br />
<label for="lines-outline-width" title="Outline Width">O. Width: </label>
<input type="text" value="3" id="lines-outline-width" />
</div>
</div>

<div class="edit-form">
<input id="refresh-polygons" type="button" value="Refresh" />
<h2>Polygons</h2>
<div class="edit-form-elem">
<label for="polygons-text">Text: </label>
<select id="polygons-text">
<option value="hide">Hide</option>
<option value="normal" selected="selected">Normal</option>
<option value="shorten">Shorten</option>
<option value="wrap">Wrap</option>
</select>
<br />
<label for="polygons-maxreso" title="Max Resolution Denominator">MaxReso.: </label>
<select id="polygons-maxreso">
<option value="38400">38,400</option>
<option value="19200">19,200</option>
<option value="9600">9,600</option>
<option value="4800">4,800</option>
<option value="2400">2,400</option>
<option value="1200" selected="selected">1,200</option>
<option value="600">600</option>
<option value="300">300</option>
<option value="150">150</option>
<option value="75">75</option>
<option value="32">32</option>
<option value="16">16</option>
<option value="8">8</option>
</select>
<br />
<label for="polygons-align">Align: </label>
<select id="polygons-align">
<option value="" selected="selected"></option>
<option value="center">Center</option>
<option value="end">End</option>
<option value="left">Left</option>
<option value="right">Right</option>
<option value="start">Start</option>
</select>
<br />
<label for="polygons-baseline">Baseline: </label>
<select id="polygons-baseline">
<option value="alphabetic">Alphabetic</option>
<option value="bottom">Bottom</option>
<option value="hanging">Hanging</option>
<option value="ideographic">Ideographic</option>
<option value="middle" selected="selected">Middle</option>
<option value="top">Top</option>
</select>
<br />
<label for="polygons-rotation">Rotation: </label>
<select id="polygons-rotation">
<option value="0"></option>
<option value="0.785398164">45°</option>
<option value="1.570796327">90°</option>
</select>
<br />
<label for="polygons-font">Font: </label>
<select id="polygons-font">
<option value="Arial">Arial</option>
<option value="'Courier New'">Courier New</option>
<option value="'Open Sans'">Open Sans</option>
<option value="Verdana" selected="selected">Verdana</option>
</select>
<br />
<label for="polygons-weight">Weight: </label>
<select id="polygons-weight">
<option value="bold" selected="selected">Bold</option>
<option value="normal">Normal</option>
</select>
<br />
<label for="polygons-placement">Placement: </label>
<select id="polygons-placement">
<option value="line">Line</option>
<option value="point" selected="selected">Point</option>
</select>
<br />
<label for="polygons-maxangle">Max Angle: </label>
<select id="polygons-maxangle">
<option value="0.7853981633974483" selected="selected">45°</option>
<option value="2.0943951023931953">120°</option>
<option value="6.283185307179586">360°</option>
</select>
<br />
<label for="polygons-overflow">Exceed Len: </label>
<select id="polygons-overflow">
<option value="true">True</option>
<option value="false" selected="selected">False</option>
</select>
<br />
<label for="polygons-size">Size: </label>
<input type="text" value="10px" id="polygons-size" />
<br />
<label for="polygons-height">Line height: </label>
<input type="text" value="1" id="polygons-height" />
<br />
<label for="polygons-offset-x">Offset X: </label>
<input type="text" value="0" id="polygons-offset-x" />
<br />
<label for="polygons-offset-y">Offset Y: </label>
<input type="text" value="0" id="polygons-offset-y" />
<br />
<label for="polygons-color">Color: </label>
<input type="text" value="blue" id="polygons-color" />
<br />
<label for="polygons-outline" title="Outline Color">O. Color: </label>
<input type="text" value="#ffffff" id="polygons-outline" />
<br />
<label for="polygons-outline-width" title="Outline Width">O. Width: </label>
<input type="text" value="3" id="polygons-outline-width" />
</div>
</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>