Line Arrows
Runnable OpenLayers example: Line Arrows. 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 line-arrows
核心逻辑:样式函数按线段中点生成三角符号,旋转角取该段方向,与线样式一起返回
依赖: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:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar label{display:flex;gap:6px;align-items:center}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>箭头大小 <input type="range" id="size" min="4" max="12" value="7"></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 (typeof ol === 'undefined') {
document.getElementById('map').innerHTML =
'<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var arrowSize = 7;
/* 三条带拐弯的路径 */
var paths = [
[[116.330, 39.900], [116.365, 39.920], [116.400, 39.905], [116.435, 39.920]],
[[116.340, 39.885], [116.380, 39.895], [116.420, 39.880], [116.460, 39.892]],
[[116.355, 39.935], [116.395, 39.950], [116.430, 39.938]]
];
var features = paths.map(function (coords, idx) {
return new ol.Feature({
geometry: new ol.geom.LineString(coords.map(function (c) {
return ol.proj.fromLonLat(c);
})),
idx: idx
});
});
var lineStyle = new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#1f5fd0', width: 3 })
});
function arrowStyles(feature) {
var geom = feature.getGeometry();
var coords = geom.getCoordinates();
var styles = [lineStyle];
for (var i = 1; i < coords.length; i++) {
var a = coords[i - 1];
var b = coords[i];
var mid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
var angle = Math.atan2(b[1] - a[1], b[0] - a[0]);
styles.push(new ol.style.Style({
geometry: new ol.geom.Point(mid),
image: new ol.style.RegularShape({
points: 3,
radius: arrowSize,
radius2: 0,
rotation: angle + Math.PI / 2,
fill: new ol.style.Fill({ color: '#c92a2a' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 1 })
})
}));
}
return styles;
}
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.6 }),
new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: arrowStyles
})
],
view: new ol.View({
center: ol.proj.fromLonLat([116.395, 39.915]),
zoom: 12,
minZoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false })
});
document.getElementById('size').addEventListener('input', function () {
arrowSize = Number(this.value);
features.forEach(function (f) { f.setStyle(arrowStyles(f)); });
map.getLayers().item(1).getSource().changed();
});
})();
</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.