Atlaslog
Atlaslog / Examples / OpenLayers / Webgl Points Layer
OpenLayers · Runnable Example

Webgl Points Layer

Runnable OpenLayers example: Webgl Points Layer. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-webgl-points-layer.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-webgl-points-layer.html
<!--
  示例:OpenLayers WebGL 点图层
  要点:WebGLPoints 用表达式控制半径与颜色,数万个点也能保持流畅
  依赖: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>WebGL 点图层</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">
  <label>样式
    <select id="style">
      <option value="circles">圆点</option>
      <option value="triangles">三角</option>
      <option value="latitude">按纬度着色</option>
    </select>
  </label>
  <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 cities = ['北京', '天津', '石家庄', '太原', '济南', '郑州', '沈阳', '呼和浩特',
    '南京', '上海', '杭州', '合肥', '武汉', '长沙', '西安', '兰州', '成都', '重庆',
    '贵阳', '昆明', '广州', '南宁', '福州', '南昌'];

  var features = [];
  for (var i = 0; i < 240; ++i) {
    var base = cities[i % cities.length];
    var lon = 110 + (i % 12) * 2.6 + (Math.random() - 0.5) * 2;
    var lat = 24 + Math.floor(i / 12) * 1.9 + (Math.random() - 0.5) * 1.4;
    features.push(new ol.Feature({
      geometry: new ol.geom.Point(ol.proj.fromLonLat([lon, lat])),
      name: base,
      population: 50000 + Math.round(Math.random() * 1900000),
      latitude: lat,
      hover: 0
    }));
  }

  var source = new ol.source.Vector({
    features: features,
    wrapX: false
  });

  var styles = {
    circles: {
      'circle-radius': ['interpolate', ['linear'], ['get', 'population'],
        50000, 3, 2000000, 12],
      'circle-fill-color': ['match', ['get', 'hover'], 1, '#ff3f3f', '#006688'],
      'circle-opacity': 0.85,
      'circle-stroke-color': '#ffffff',
      'circle-stroke-width': 1
    },
    triangles: {
      'shape-points': 3,
      'shape-radius': ['interpolate', ['linear'], ['get', 'population'],
        50000, 4, 2000000, 14],
      'shape-fill-color': ['interpolate', ['linear'], ['get', 'population'],
        50000, '#5aca5b', 2000000, '#ff6a19'],
      'shape-rotate-with-view': true
    },
    latitude: {
      'circle-radius': ['interpolate', ['linear'], ['get', 'population'],
        50000, 3, 2000000, 11],
      'circle-fill-color': ['interpolate', ['linear'], ['get', 'latitude'],
        20, '#ff14c3', 30, '#ff621d', 40, '#ffed02', 50, '#00ff67'],
      'circle-opacity': 0.9
    }
  };

  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'})})],
    view: new ol.View({
      center: ol.proj.fromLonLat([113.5, 34.5]),
      zoom: 4
    })
  });

  var pointsLayer = null;
  var selected = null;
  var literalStyle = null;

  function useStyle(name) {
    literalStyle = styles[name];
    var previous = pointsLayer;
    pointsLayer = new ol.layer.WebGLPoints({
      source: source,
      style: literalStyle
    });
    map.addLayer(pointsLayer);
    if (previous) {
      map.removeLayer(previous);
      previous.dispose();
    }
  }

  map.on('pointermove', function (evt) {
    if (selected) {
      selected.set('hover', 0);
      selected = null;
    }
    map.forEachFeatureAtPixel(evt.pixel, function (feature) {
      feature.set('hover', 1);
      selected = feature;
      document.getElementById('info').textContent =
        feature.get('name') + ' · ' + feature.get('population') + ' 人';
      return true;
    });
  });

  useStyle('circles');

  document.getElementById('style').addEventListener('change', function (evt) {
    useStyle(evt.target.value);
  });

  function animate() {
    map.render();
    window.requestAnimationFrame(animate);
  }
  animate();

})();
</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.

More OpenLayers examples

OpenLayers · EXAMPLEWebgl Sea Level OpenLayers · EXAMPLEWebgl Shaded Relief OpenLayers · EXAMPLEWebgl Tiles