Wind Arrows
Runnable OpenLayers example: Wind 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 风向箭头
要点:RegularShape 组合出箭杆与箭头,按风速缩放、按风向旋转
依赖: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:10px;align-items:center;flex-wrap:wrap;font-size:12px;color:#58606e}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
.bar select,.bar input[type=range]{font-size:12px}
.bar .val{font-family:ui-monospace,Consolas,monospace;color:#1f5fd0;min-width:44px}
.bar .sep{width:1px;height:16px;background:#d8dce2}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span>页面内气象观测数据</span>
<span class="sep"></span>
<span id="info">—</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').textContent = 'OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net';
return;
}
var reports = [
{city: '北京', lon: 116.391, lat: 39.907, speed: 12, deg: 315},
{city: '天津', lon: 117.200, lat: 39.084, speed: 9, deg: 20},
{city: '石家庄', lon: 114.514, lat: 38.042, speed: 6, deg: 160},
{city: '太原', lon: 112.549, lat: 37.857, speed: 14, deg: 280},
{city: '济南', lon: 117.121, lat: 36.651, speed: 11, deg: 45},
{city: '郑州', lon: 113.625, lat: 34.746, speed: 8, deg: 200},
{city: '沈阳', lon: 123.431, lat: 41.805, speed: 16, deg: 330},
{city: '呼和浩特', lon: 111.751, lat: 40.842, speed: 18, deg: 290},
{city: '南京', lon: 118.797, lat: 32.060, speed: 5, deg: 120},
{city: '上海', lon: 121.474, lat: 31.230, speed: 7, deg: 90},
{city: '武汉', lon: 114.305, lat: 30.593, speed: 10, deg: 30},
{city: '西安', lon: 108.940, lat: 34.341, speed: 13, deg: 250}
];
var shaft = new ol.style.RegularShape({
points: 2,
radius: 6,
stroke: new ol.style.Stroke({color: '#1f2b36', width: 2}),
rotateWithView: true
});
var head = new ol.style.RegularShape({
points: 3,
radius: 5,
fill: new ol.style.Fill({color: '#1f2b36'}),
rotateWithView: true
});
var styles = [new ol.style.Style({image: shaft}), new ol.style.Style({image: head})];
var features = reports.map(function (report) {
var feature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([report.lon, report.lat])),
wind: report,
city: report.city
});
return feature;
});
var source = new ol.source.Vector({features: features});
var vector = new ol.layer.Vector({
source: source,
style: function (feature) {
var wind = feature.get('wind');
var angle = ((wind.deg - 180) * Math.PI) / 180;
var scale = wind.speed / 10;
shaft.setScale([1, scale]);
shaft.setRotation(angle);
head.setDisplacement([0, head.getRadius() / 2 + shaft.getRadius() * scale]);
head.setRotation(angle);
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.7}),
vector
],
view: new ol.View({
center: ol.proj.fromLonLat([116.5, 36.5]),
zoom: 4
})
});
map.on('pointermove', function (evt) {
if (evt.dragging) {
return;
}
var feature = map.forEachFeatureAtPixel(evt.pixel, function (candidate) {
return candidate;
});
document.getElementById('info').textContent = feature
? feature.get('city') + ' · ' + feature.get('wind').speed +
' m/s · ' + feature.get('wind').deg + '°'
: '—';
});
})();
</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.