原文链接及内容

运行界面

这个例子展示了如何在标签框中对齐文本。默认情况下,文本根据textAlign选项来对齐。但是,此选项也会根据textAlign设置来调整标签本身。要将标签放置与文本放置分离(在标签框中),请使用justify选项。

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
import Collection from 'ol/Collection.js';
import Feature from 'ol/Feature.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import Map from 'ol/Map.js';
import Point from 'ol/geom/Point.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';

const features = [
{
geometry: new Point([-8300000, 6095000]),
textAlign: 'left',
},
{
geometry: new Point([-8150000, 6095000]),
textAlign: 'center',
},
{
geometry: new Point([-8000000, 6095000]),
textAlign: 'right',
},
{
geometry: new Point([-8300000, 6025000]),
textAlign: 'left',
justify: 'center',
},
{
geometry: new Point([-8150000, 6025000]),
textAlign: 'center',
justify: 'center',
},
{
geometry: new Point([-8000000, 6025000]),
textAlign: 'right',
justify: 'center',
},
{
geometry: new Point([-8300000, 5955000]),
textAlign: 'left',
justify: 'left',
},
{
geometry: new Point([-8150000, 5955000]),
textAlign: 'center',
justify: 'left',
},
{
geometry: new Point([-8000000, 5955000]),
textAlign: 'right',
justify: 'left',
},
];

function createStyle({textAlign, justify}) {
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: new Text({
font: '16px sans-serif',
textAlign,
justify,
text:
`Justify text inside box\ntextAlign: ${textAlign}` +
(justify ? `\njustify: ${justify}` : ''),
fill: new Fill({
color: [255, 255, 255, 1],
}),
backgroundFill: new Fill({
color: [168, 50, 153, 0.6],
}),
padding: [2, 2, 2, 2],
}),
});
}

const vectorPoints = new VectorLayer({
source: new VectorSource({
features: new Collection(
features.map((featureOptions) => {
const feature = new Feature({
geometry: featureOptions.geometry,
});
feature.setStyle(createStyle(featureOptions));
return feature;
})
),
format: new GeoJSON(),
}),
});

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

界面布局文件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 Labels - Justify Text</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>
<!-- 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>