Heatmap Gradient
Runnable OpenLayers example: Heatmap Gradient. 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
<!--
示例:热力渐变色
要点:gradient 数组定义色带,用两个热力层切换而不重建数据源
依赖: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}
.bar select{padding:3px 6px;border:1px solid #cfd4dc;border-radius:4px;font-size:12px}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>色带
<select id="scheme">
<option value="cool">冷色带(蓝绿黄红)</option>
<option value="warm">暖色带(紫橙)</option>
</select>
</label>
<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 seed = 4402;
function rand() {
seed = (seed * 1664525 + 1013904223) % 4294967296;
return seed / 4294967296;
}
var features = [];
for (var i = 0; i < 90; i++) {
/* 两个热力中心 + 均匀噪声 */
var cx = i % 2 === 0 ? 116.395 : 116.425;
var lon = cx + (rand() - 0.5) * 0.05;
var lat = 39.905 + (rand() - 0.5) * 0.04;
features.push(new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([lon, lat])),
value: 0.3 + rand() * 0.7
}));
}
var source = new ol.source.Vector({ features: features });
function weight(feature) {
return feature.get('value');
}
var coolLayer = new ol.layer.Heatmap({
source: source,
radius: 12,
blur: 16,
weight: weight,
gradient: ['#0b3d91', '#1f8a4c', '#f2c94c', '#e8590c', '#c0392b']
});
var warmLayer = new ol.layer.Heatmap({
source: source,
radius: 12,
blur: 16,
weight: weight,
gradient: ['#2b1055', '#7b2d8e', '#e8590c', '#ffb703'],
visible: false
});
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'}) }),
coolLayer,
warmLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([116.410, 39.905]),
zoom: 12
}),
controls: ol.control.defaults.defaults({ attribution: false })
});
document.getElementById('scheme').addEventListener('change', function () {
var warm = this.value === 'warm';
coolLayer.setVisible(!warm);
warmLayer.setVisible(warm);
});
})();
</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.