Style Expressions
Runnable OpenLayers example: Style Expressions. 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 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 select{font-size:12px;padding:2px 4px}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>表达式
<select id="expr">
<option value="pop">半径 ∝ 人口</option>
<option value="rate">颜色按增速分级</option>
<option value="mixed">半径 + 颜色双变量</option>
</select>
</label>
<span>共 <b id="n">0</b> 个城市</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 CITIES = [
['北京', 116.407, 39.904, 2189, 1.2],
['上海', 121.474, 31.230, 2487, 0.8],
['广州', 113.264, 23.129, 1868, 2.1],
['深圳', 114.058, 22.543, 1756, 3.4],
['成都', 104.067, 30.573, 1334, 4.8],
['武汉', 114.306, 30.593, 1108, 3.0],
['西安', 108.940, 34.342, 1295, 5.2],
['杭州', 120.155, 30.274, 1194, 3.9],
['南京', 118.797, 32.060, 931, 1.7],
['郑州', 113.625, 34.747, 1260, 2.6],
['长沙', 112.939, 28.228, 839, 4.1],
['昆明', 102.833, 24.880, 555, 3.3]
];
var feats = CITIES.map(function (c) {
return new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([c[1], c[2]])),
name: c[0],
pop: c[3],
rate: c[4]
});
});
var expr = 'pop';
function colorForRate(rate) {
if (rate >= 4) return '#b42318';
if (rate >= 3) return '#d9822b';
if (rate >= 2) return '#1f5fd0';
return '#2f8f5b';
}
var layer = new ol.layer.Vector({
source: new ol.source.Vector({ features: feats }),
style: function (f) {
var pop = f.get('pop');
var rate = f.get('rate');
var radius = 4 + Math.sqrt(pop) / 14;
var fill = '#1f5fd0';
if (expr === 'rate') {
radius = 7;
fill = colorForRate(rate);
} else if (expr === 'mixed') {
fill = colorForRate(rate);
}
return new ol.style.Style({
image: new ol.style.Circle({
radius: radius,
fill: new ol.style.Fill({ color: fill }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 1.5 })
}),
text: new ol.style.Text({
text: f.get('name'),
offsetY: -radius - 6,
font: '500 12px sans-serif',
fill: new ol.style.Fill({ color: '#16181d' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 3 })
})
});
}
});
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([112.5, 32.0]), zoom: 4 }),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
document.getElementById('n').textContent = feats.length;
document.getElementById('expr').addEventListener('change', function () {
expr = this.value;
layer.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.