Line Styles
Runnable OpenLayers example: Line Styles. 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-styles
核心逻辑:同一条折线用多组 Stroke 参数绘制,比较宽度、端点、拐角、虚线样式
依赖: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:16px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span>自上而下:宽度 1–7 px、round / miter 拐角、实线与虚线</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 specs = [
{ width: 1, color: '#1f5fd0', cap: 'butt', join: 'miter', dash: null, label: '1px butt miter' },
{ width: 2, color: '#1f5fd0', cap: 'round', join: 'round', dash: null, label: '2px round round' },
{ width: 4, color: '#c92a2a', cap: 'square', join: 'bevel', dash: null, label: '4px square bevel' },
{ width: 6, color: '#2f9e44', cap: 'round', join: 'miter', dash: [12, 6], label: '6px 虚线 12/6' },
{ width: 7, color: '#e8590c', cap: 'round', join: 'round', dash: [2, 8], label: '7px 点线 2/8' },
{ width: 5, color: '#7048e8', cap: 'butt', join: 'miter', dash: [20, 8, 4, 8], label: '5px 复合虚线' },
{ width: 3, color: '#0c8599', cap: 'round', join: 'round', dash: [18, 0], label: '3px 长划(虚线复位)' }
];
var features = [];
specs.forEach(function (spec, idx) {
var lat = 39.945 - idx * 0.009;
var coords = [
[116.320, lat - 0.002],
[116.360, lat + 0.003],
[116.400, lat - 0.003],
[116.440, lat + 0.003],
[116.470, lat - 0.001]
].map(function (c) { return ol.proj.fromLonLat(c); });
var feature = new ol.Feature({
geometry: new ol.geom.LineString(coords),
label: spec.label
});
feature.setStyle(new ol.style.Style({
stroke: new ol.style.Stroke({
color: spec.color,
width: spec.width,
lineCap: spec.cap,
lineJoin: spec.join,
lineDash: spec.dash || undefined
}),
text: new ol.style.Text({
text: spec.label,
placement: 'point',
font: '500 11px sans-serif',
offsetY: -10,
fill: new ol.style.Fill({ color: '#16181d' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 3 })
})
}));
features.push(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.55 }),
new ol.layer.Vector({ source: new ol.source.Vector({ features: features }) })
],
view: new ol.View({
center: ol.proj.fromLonLat([116.395, 39.915]),
zoom: 12,
minZoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false })
});
})();
</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.