Earthquake Clusters
Runnable OpenLayers example: Earthquake Clusters. 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
<!--
说明:地震点聚合
核心逻辑:Cluster 源的 features 属性取簇内数量与最大震级,样式按数量放大
依赖: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}
#info{margin-left:auto}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label><input type="checkbox" id="agg" checked> 聚合</label>
<span>400 个震中 · 震级 2.0 至 6.5</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').innerHTML =
'<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var fromLonLat = ol.proj.fromLonLat;
var rawSource = new ol.source.Vector();
var feats = [];
for (var i = 0; i < 400; i++) {
var lon = 100 + Math.random() * 22;
var lat = 24 + Math.random() * 16;
var mag = 2 + Math.random() * 4.5;
var f = new ol.Feature({ geometry: new ol.geom.Point(fromLonLat([lon, lat])) });
f.set('mag', Math.round(mag * 10) / 10);
feats.push(f);
}
rawSource.addFeatures(feats);
var clusterSource = new ol.source.Cluster({
distance: 42,
minDistance: 16,
source: rawSource
});
function magColor(mag) {
if (mag >= 5.5) return 'rgba(200,40,28,0.90)';
if (mag >= 4.5) return 'rgba(226,110,40,0.88)';
if (mag >= 3.5) return 'rgba(214,170,50,0.88)';
return 'rgba(40,120,220,0.86)';
}
function clusterStyle(feature) {
var members = feature.get('features');
var size = members.length;
var maxMag = 0;
members.forEach(function (f) { maxMag = Math.max(maxMag, f.get('mag')); });
var radius = Math.min(7 + Math.log2(size + 1) * 3.5, 26);
return new ol.style.Style({
image: new ol.style.Circle({
radius: radius,
fill: new ol.style.Fill({ color: magColor(maxMag) }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 2 })
}),
text: new ol.style.Text({
text: size > 1 ? String(size) : '',
font: '600 11px sans-serif',
fill: new ol.style.Fill({ color: '#ffffff' })
})
});
}
function pointStyle(feature) {
var mag = feature.get('mag');
return new ol.style.Style({
image: new ol.style.Circle({
radius: 3 + mag * 1.5,
fill: new ol.style.Fill({ color: magColor(mag) }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 1.2 })
})
});
}
var clusterLayer = new ol.layer.Vector({ source: clusterSource, style: clusterStyle });
var view = new ol.View({ center: fromLonLat([111, 32]), zoom: 4, minZoom: 2, maxZoom: 19 });
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'}) }), clusterLayer],
view: view,
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
var info = document.getElementById('info');
document.getElementById('agg').addEventListener('change', function () {
map.removeLayer(clusterLayer);
clusterLayer = this.checked
? new ol.layer.Vector({ source: clusterSource, style: clusterStyle })
: new ol.layer.Vector({ source: rawSource, style: pointStyle });
map.addLayer(clusterLayer);
info.textContent = this.checked ? '聚合显示' : '原始震中显示';
});
info.textContent = '聚合显示';
})();
</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.