Load Events
Runnable OpenLayers example: Load Events. 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 load-events
核心逻辑:监听瓦片与矢量源的 loadstart/loadend 事件,维护计数并驱动加载指示器
依赖: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:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar button{font:inherit;padding:3px 9px;border:1px solid #ccd2da;background:#fff;
border-radius:3px;cursor:pointer;color:#2b3140}
#map{flex:1 1 auto;min-height:0;background:#eef0f3;position:relative}
.spinner{position:absolute;right:16px;top:16px;width:32px;height:32px;
border:3px solid rgba(31,95,208,0.25);border-top-color:#1f5fd0;border-radius:50%;
animation:spin 0.9s linear infinite;display:none}
.spinner.on{display:block}
@keyframes spin{to{transform:rotate(360deg)}}
</style>
</head>
<body>
<div class="bar">
<button id="reload">重新加载矢量数据</button>
<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').innerHTML =
'<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var infoEl = document.getElementById('info');
var spinner = document.createElement('div');
spinner.className = 'spinner';
var tileSource = new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'});
var pending = 0;
var totalLoads = 0;
tileSource.on('tileloadstart', function () {
pending++;
totalLoads++;
spinner.classList.add('on');
render();
});
function done() {
pending = Math.max(0, pending - 1);
if (pending === 0) spinner.classList.remove('on');
render();
}
tileSource.on('tileloadend', done);
tileSource.on('tileloaderror', done);
/* 自定义矢量加载器:延迟 800ms 后放入要素,可重复触发 */
var vectorSource = new ol.source.Vector({
loader: function (extent, resolution, projection, success, failure) {
spinner.classList.add('on');
setTimeout(function () {
try {
var features = [];
for (var i = 0; i < 40; i++) {
var lon = 116.30 + Math.random() * 0.22;
var lat = 39.86 + Math.random() * 0.09;
features.push(new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([lon, lat]))
}));
}
vectorSource.addFeatures(features);
if (success) success(features);
} catch (e) {
if (failure) failure();
}
spinner.classList.remove('on');
render();
}, 800);
},
strategy: ol.loadingstrategy.bbox
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: tileSource }),
new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({ color: '#2f9e44' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 1 })
})
})
})
],
view: new ol.View({
center: ol.proj.fromLonLat([116.391, 39.907]),
zoom: 13,
minZoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false })
});
map.getViewport().appendChild(spinner);
function render() {
infoEl.textContent = '瓦片请求 ' + totalLoads +
' · 进行中 ' + pending +
' · 矢量要素 ' + vectorSource.getFeatures().length;
}
vectorSource.on('featuresloadend', render);
document.getElementById('reload').addEventListener('click', function () {
vectorSource.clear();
vectorSource.refresh();
render();
});
render();
})();
</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.