Street Labels
Runnable OpenLayers example: Street 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
<!--
示例:街道沿线标注
要点:LineString 配 Text 的 placement 与 rotation,让文字顺着线条走
依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
-->
<!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;color:#16181d}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label><input type="checkbox" id="rot" checked> 文字随线旋转</label>
<label><input type="checkbox" id="fill" checked> 沿整条线重复</label>
<span>拖动地图看标注跟随线段方向</span>
</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 (!window.ol) {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
return;
}
var ROADS = [
['长安街', [[116.300, 39.9075], [116.360, 39.9072], [116.430, 39.9078], [116.500, 39.9082]]],
['中轴路', [[116.391, 39.860], [116.393, 39.900], [116.396, 39.940], [116.398, 39.990]]],
['朝阳北路', [[116.420, 39.925], [116.470, 39.928], [116.520, 39.930], [116.560, 39.932]]],
['西直门外大街', [[116.300, 39.941], [116.340, 39.940], [116.375, 39.938]]]
];
var feats = ROADS.map(function (r) {
return new ol.Feature({
geometry: new ol.geom.LineString(
r[1].map(function (c) { return ol.proj.fromLonLat(c); })
),
name: r[0]
});
});
var rotate = true;
var repeat = true;
var layer = new ol.layer.Vector({
source: new ol.source.Vector({ features: feats })
});
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'}) }), layer],
view: new ol.View({ center: ol.proj.fromLonLat([116.40, 39.925]), zoom: 12 }),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
function refresh() {
layer.setStyle(function (f) {
return new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#bfc6d1', width: 7, lineCap: 'round' }),
text: new ol.style.Text({
text: f.get('name'),
font: '500 12px "PingFang SC", sans-serif',
placement: 'line',
overflow: true,
repeat: repeat ? 240 : 0,
rotateWithView: rotate,
fill: new ol.style.Fill({ color: '#3b4250' }),
stroke: new ol.style.Stroke({ color: 'rgba(255,255,255,.9)', width: 4 })
})
});
});
}
function bind(id, setter) {
document.getElementById(id).addEventListener('change', function () {
setter(this.checked);
refresh();
});
}
bind('rot', function (v) { rotate = v; });
bind('fill', function (v) { repeat = v; });
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.