Filter Points Webgl
Runnable OpenLayers example: Filter Points Webgl. 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
<!--
说明:按属性过滤点要素
核心逻辑:样式函数按分区与数值阈值返回 null 过滤,三千个点即时筛选
依赖: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:4px 10px;border:1px solid #c9d0da;border-radius:5px;
background:#fff;color:#1f2a37;cursor:pointer}
.bar button.on{background:#1f5fd0;border-color:#1f5fd0;color:#fff}
.bar input[type=range]{width:120px}
#info{margin-left:auto}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<button data-g="all" class="on">全部</button>
<button data-g="A">A 区</button>
<button data-g="B">B 区</button>
<button data-g="C">C 区</button>
<label>数值下限 <input type="range" id="thr" min="0" max="90" step="5" value="0"></label>
<label><input type="checkbox" id="on" checked> 启用过滤</label>
<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 groups = ['A', 'B', 'C'];
var features = [];
for (var i = 0; i < 3000; i++) {
var lon = 115.9 + Math.random() * 1.2;
var lat = 39.6 + Math.random() * 0.9;
var f = new ol.Feature({ geometry: new ol.geom.Point(fromLonLat([lon, lat])) });
f.set('group', groups[i % 3]);
f.set('value', Math.round(Math.random() * 100));
features.push(f);
}
var source = new ol.source.Vector({ features: features });
var group = 'all';
var threshold = 0;
var enabled = true;
var colors = { A: 'rgba(31,95,208,0.75)', B: 'rgba(226,110,40,0.75)', C: 'rgba(57,160,168,0.75)' };
var styleCache = {};
groups.forEach(function (g) {
styleCache[g] = new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({ color: colors[g] }),
stroke: new ol.style.Stroke({ color: 'rgba(255,255,255,0.8)', width: 1 })
})
});
});
function styleFn(feature) {
if (enabled) {
if (group !== 'all' && feature.get('group') !== group) return null;
if (feature.get('value') < threshold) return null;
}
return styleCache[feature.get('group')];
}
var layer = new ol.layer.Vector({ source: source, style: styleFn });
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: fromLonLat([116.5, 40.0]), zoom: 11, minZoom: 2, maxZoom: 19 }),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
var info = document.getElementById('info');
function countHit() {
var n = 0;
source.getFeatures().forEach(function (f) {
if (!enabled) { n++; return; }
if (group !== 'all' && f.get('group') !== group) return;
if (f.get('value') < threshold) return;
n++;
});
info.textContent = '命中 ' + n + ' / ' + source.getFeatures().length + ' 个点';
}
Array.prototype.forEach.call(document.querySelectorAll('.bar button[data-g]'), function (btn) {
btn.addEventListener('click', function () {
Array.prototype.forEach.call(document.querySelectorAll('.bar button[data-g]'), function (b) {
b.classList.remove('on');
});
btn.classList.add('on');
group = btn.getAttribute('data-g');
layer.changed();
countHit();
});
});
document.getElementById('thr').addEventListener('input', function () {
threshold = Number(this.value);
layer.changed();
countHit();
});
document.getElementById('on').addEventListener('change', function () {
enabled = this.checked;
layer.changed();
countHit();
});
countHit();
})();
</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.