Atlaslog
Atlaslog / Examples / OpenLayers / Webgl Vector Tiles
OpenLayers · Runnable Example

Webgl Vector Tiles

Runnable OpenLayers example: Webgl Vector Tiles. 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-vector-tiles.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-webgl-vector-tiles.html
<!--
  示例:OpenLayers WebGL 矢量瓦片
  要点:页面内按瓦片网格生成要素,交给 WebGLVector 批量绘制并按属性着色
  依赖: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">
  <span>页面内瓦片要素 + WebGLVector</span>
  <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 tileGrid = ol.tilegrid.createXYZ({tileSize: 256, maxZoom: 18});
  var zoom = 11;
  var center = ol.proj.fromLonLat([116.391, 39.907]);
  var centerTile = tileGrid.getTileCoordForCoordAndZ(center, zoom);
  var palette = [
    ['rgba(31, 95, 208, 0.55)', '#1f5fd0', 2],
    ['rgba(224, 106, 0, 0.55)', '#e06a00', 3],
    ['rgba(46, 125, 50, 0.55)', '#2e7d32', 1]
  ];

  var features = [];
  var seq = 1;

  for (var dx = -2; dx <= 2; ++dx) {
    for (var dy = -2; dy <= 2; ++dy) {
      var tileCoord = [zoom, centerTile[1] + dx, centerTile[2] + dy];
      var extent = tileGrid.getTileCoordExtent(tileCoord);
      var w = extent[2] - extent[0];
      var h = extent[3] - extent[1];
      var tileId = tileCoord[0] * 1000000 + tileCoord[1] * 1000 + tileCoord[2];
      var tileLabel = tileCoord[0] + '/' + tileCoord[1] + '/' + tileCoord[2];

      function box(x1, y1, x2, y2) {
        return [
          [extent[0] + x1 * w, extent[1] + y1 * h],
          [extent[0] + x2 * w, extent[1] + y1 * h],
          [extent[0] + x2 * w, extent[1] + y2 * h],
          [extent[0] + x1 * w, extent[1] + y2 * h],
          [extent[0] + x1 * w, extent[1] + y1 * h]
        ];
      }

      for (var i = 0; i < 3; ++i) {
        var style = palette[i];
        var feature = new ol.Feature({
          geometry: new ol.geom.Polygon([box(
            0.08 + i * 0.30, 0.16 + (i % 2) * 0.18,
            0.32 + i * 0.30, 0.62 + (i % 2) * 0.18
          )]),
          fillColor: style[0],
          strokeColor: style[1],
          strokeWidth: style[2],
          tile: tileLabel,
          tileId: tileId
        });
        feature.setId(seq++);
        features.push(feature);
      }
    }
  }

  var vectorLayer = new ol.layer.WebGLVector({
    source: new ol.source.Vector({features: features}),
    variables: {highlightedId: -1},
    style: {
      'fill-color': ['get', 'fillColor'],
      'stroke-color': [
        'case',
        ['==', ['var', 'highlightedId'], ['get', 'tileId']],
        'white',
        ['get', 'strokeColor']
      ],
      'stroke-width': [
        'case',
        ['==', ['var', 'highlightedId'], ['get', 'tileId']],
        4,
        ['get', 'strokeWidth']
      ]
    }
  });

  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'}), opacity: 0.4}),
      vectorLayer
    ],
    view: new ol.View({
      center: center,
      zoom: zoom
    })
  });

  var highlightedId = -1;

  map.on('pointermove', function (evt) {
    if (evt.dragging) {
      return;
    }
    var feature = map.forEachFeatureAtPixel(evt.pixel, function (candidate) {
      return candidate;
    });
    var id = feature ? feature.get('tileId') : -1;
    if (id !== highlightedId) {
      highlightedId = id;
      vectorLayer.updateStyleVariables({highlightedId: highlightedId});
    }
    document.getElementById('info').textContent = feature
      ? '瓦片 ' + feature.get('tile')
      : '—';
  });

})();
</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 · EXAMPLEWind OpenLayers · EXAMPLEWind Arrows OpenLayers · EXAMPLEWkb