Vector Labels
Runnable OpenLayers example: Vector Labels. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:OpenLayers 标签样式控制
要点:字号、颜色、沿线放置与溢出开关实时作用到点、线、面三类标签
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>标签样式控制</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:10px;align-items:center;flex-wrap:wrap;font-size:12px;color:#58606e}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
.bar select,.bar input[type=range]{font-size:12px}
.bar .val{font-family:ui-monospace,Consolas,monospace;color:#1f5fd0;min-width:44px}
.bar .sep{width:1px;height:16px;background:#d8dce2}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>字号 <input type="range" id="size" min="8" max="24" value="13"><span class="val" id="sizeVal">13</span></label>
<label>颜色 <input type="color" id="color" value="#1f2f3d"></label>
<label><input type="checkbox" id="placement"> 沿线放置</label>
<label><input type="checkbox" id="overflow" checked> 允许溢出</label>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net';
return;
}
var pointFeatures = [
['国贸', 116.461, 39.909],
['王府井', 116.411, 39.915],
['前门', 116.397, 39.899],
['鼓楼', 116.394, 39.941]
].map(function (item) {
return new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([item[1], item[2]])),
name: item[0]
});
});
var lineFeature = new ol.Feature({
geometry: new ol.geom.LineString([
ol.proj.fromLonLat([116.350, 39.900]),
ol.proj.fromLonLat([116.400, 39.906]),
ol.proj.fromLonLat([116.450, 39.912]),
ol.proj.fromLonLat([116.510, 39.915])
]),
name: '长安街东延线'
});
var polygonFeature = new ol.Feature({
geometry: new ol.geom.Polygon([[
ol.proj.fromLonLat([116.370, 39.925]),
ol.proj.fromLonLat([116.430, 39.925]),
ol.proj.fromLonLat([116.430, 39.955]),
ol.proj.fromLonLat([116.370, 39.955]),
ol.proj.fromLonLat([116.370, 39.925])
]]),
name: '中心城区样本面'
});
var controls = {
size: document.getElementById('size'),
color: document.getElementById('color'),
placement: document.getElementById('placement'),
overflow: document.getElementById('overflow')
};
function textStyle(feature) {
var size = parseInt(controls.size.value, 10);
return new ol.style.Text({
text: feature.get('name'),
font: '600 ' + size + 'px sans-serif',
fill: new ol.style.Fill({color: controls.color.value}),
stroke: new ol.style.Stroke({color: '#ffffff', width: 3}),
placement: controls.placement.checked ? 'line' : undefined,
overflow: controls.overflow.checked,
maxAngle: Math.PI / 6
});
}
var pointLayer = new ol.layer.Vector({
source: new ol.source.Vector({features: pointFeatures}),
style: function (feature) {
return new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({color: 'rgba(31, 95, 208, 0.25)'}),
stroke: new ol.style.Stroke({color: '#1f5fd0', width: 2})
}),
text: textStyle(feature)
});
}
});
var lineLayer = new ol.layer.Vector({
source: new ol.source.Vector({features: [lineFeature]}),
style: function (feature) {
return new ol.style.Style({
stroke: new ol.style.Stroke({color: '#2e7d32', width: 3}),
text: textStyle(feature)
});
}
});
var polygonLayer = new ol.layer.Vector({
source: new ol.source.Vector({features: [polygonFeature]}),
style: function (feature) {
return new ol.style.Style({
fill: new ol.style.Fill({color: 'rgba(255, 145, 0, 0.12)'}),
stroke: new ol.style.Stroke({color: '#e06a00', width: 1}),
text: textStyle(feature)
});
}
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}), opacity: 0.7}),
polygonLayer,
lineLayer,
pointLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([116.430, 39.918]),
zoom: 13
})
});
function refresh() {
document.getElementById('sizeVal').textContent = controls.size.value;
pointLayer.changed();
lineLayer.changed();
polygonLayer.changed();
}
['size', 'color', 'placement', 'overflow'].forEach(function (key) {
controls[key].addEventListener('input', refresh);
controls[key].addEventListener('change', refresh);
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.