Vector Style
Runnable OpenLayers example: Vector Style. 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
<!--
示例:矢量样式化
要点:一个数据源混合点线面,样式函数按 kind 属性分发不同符号
依赖: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}
.legend{display:flex;gap:6px;align-items:center}
.dot{width:10px;height:10px;border-radius:50%;display:inline-block}
.seg{width:18px;height:3px;display:inline-block}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span class="legend"><i class="dot" style="background:#d0453a"></i> 站点</span>
<span class="legend"><i class="seg" style="background:#1f5fd0"></i> 路线</span>
<span class="legend"><i class="dot" style="background:#2f9e6e;border-radius:2px"></i> 片区</span>
<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 fromLonLat = ol.proj.fromLonLat;
var route = new ol.Feature({
kind: 'route',
name: '巡查路线',
geometry: new ol.geom.LineString([
fromLonLat([116.310, 39.905]),
fromLonLat([116.355, 39.930]),
fromLonLat([116.400, 39.915]),
fromLonLat([116.442, 39.935]),
fromLonLat([116.485, 39.908])
])
});
var zone = new ol.Feature({
kind: 'zone',
name: '重点片区',
geometry: new ol.geom.Polygon([[
fromLonLat([116.380, 39.955]),
fromLonLat([116.470, 39.955]),
fromLonLat([116.470, 39.900]),
fromLonLat([116.380, 39.900]),
fromLonLat([116.380, 39.955])
]])
});
var sites = [
[116.310, 39.905, '西单'],
[116.400, 39.915, '王府井'],
[116.485, 39.908, '国贸']
].map(function (s) {
return new ol.Feature({
kind: 'site',
name: s[2],
geometry: new ol.geom.Point(fromLonLat([s[0], s[1]]))
});
});
var source = new ol.source.Vector({
features: [zone, route].concat(sites)
});
/* 样式函数:按 kind 分发,返回数组表示叠加多个符号层 */
function styleFunction(feature) {
var kind = feature.get('kind');
if (kind === 'zone') {
return [
new ol.style.Style({
fill: new ol.style.Fill({ color: 'rgba(47,158,110,0.20)' }),
stroke: new ol.style.Stroke({ color: '#2f9e6e', width: 2 })
}),
new ol.style.Style({
text: new ol.style.Text({
text: feature.get('name'),
font: '600 12px sans-serif',
fill: new ol.style.Fill({ color: '#1d6b4a' })
})
})
];
}
if (kind === 'route') {
return new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#1f5fd0', width: 3, lineDash: [10, 6] })
});
}
return [
new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({ color: '#d0453a' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 2 })
})
}),
new ol.style.Style({
text: new ol.style.Text({
text: feature.get('name'),
offsetY: 16,
font: '500 12px sans-serif',
fill: new ol.style.Fill({ color: '#16181d' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 3 })
})
})
];
}
var vectorLayer = new ol.layer.Vector({
source: source,
style: styleFunction
});
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'}) }),
vectorLayer
],
view: new ol.View({
center: fromLonLat([116.400, 39.915]),
zoom: 12
})
});
})();
</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.