Atlaslog
Atlaslog / Examples / OpenLayers / GeoJSON Vt
OpenLayers · Runnable Example

GeoJSON Vt

Runnable OpenLayers example: GeoJSON Vt. 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-geojson-vt.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-geojson-vt.html
<!--
  示例:GeoJSON 矢量切片 — OpenLayers geojson-vt
  核心逻辑:用格网要素模拟按视图分辨率裁剪的切片效果,VectorImage 渲染并在缩放后报告要素数
  依赖: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>GeoJSON 矢量切片</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;font-size:12px;color:#58606e;flex-wrap:wrap}
  .sw{display:inline-block;width:10px;height:10px;border-radius:2px;margin-right:4px}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <span><i class="sw" style="background:#2b8a3e"></i>&lt; 0.35</span>
  <span><i class="sw" style="background:#e3b505"></i>0.35 – 0.65</span>
  <span><i class="sw" style="background:#c92a2a"></i>&gt; 0.65</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 infoEl = document.getElementById('info');

  /* 60 × 60 个格网多边形,每个带一个 0–1 的指标值 */
  var SIZE = 60;
  var STEP = 0.004;
  var LON0 = 116.20;
  var LAT0 = 39.80;

  function valueAt(i, j) {
    var v = 0.5 +
      0.35 * Math.sin(i / 7.3) * Math.cos(j / 9.1) +
      0.15 * Math.sin((i + j) / 3.7);
    return Math.max(0, Math.min(1, v));
  }

  var features = [];
  for (var i = 0; i < SIZE; i++) {
    for (var j = 0; j < SIZE; j++) {
      var lon = LON0 + i * STEP;
      var lat = LAT0 + j * STEP;
      var ring = [
        [lon, lat], [lon + STEP, lat], [lon + STEP, lat + STEP],
        [lon, lat + STEP], [lon, lat]
      ].map(function (c) { return ol.proj.fromLonLat(c); });
      features.push(new ol.Feature({
        geometry: new ol.geom.Polygon([ring]),
        v: valueAt(i, j)
      }));
    }
  }

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

  function colorFor(v) {
    if (v < 0.35) return 'rgba(43,138,62,0.65)';
    if (v < 0.65) return 'rgba(227,181,5,0.65)';
    return 'rgba(201,42,42,0.65)';
  }

  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'}) }),
      new ol.layer.VectorImage({
        source: source,
        style: function (feature) {
          return new ol.style.Style({
            fill: new ol.style.Fill({ color: colorFor(feature.get('v')) }),
            stroke: new ol.style.Stroke({ color: 'rgba(255,255,255,0.25)', width: 0.6 })
          });
        },
        /* 低缩放级别用 2 倍分辨率出图,减少重绘压力 */
        renderBuffer: 120
      })
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([116.32, 39.92]),
      zoom: 12,
      minZoom: 2,
      maxZoom: 18
    }),
    controls: ol.control.defaults.defaults({ attribution: false })
  });

  function updateInfo() {
    var zoom = map.getView().getZoom();
    infoEl.textContent = '要素总数 ' + source.getFeatures().length +
      ' · 当前缩放 ' + zoom.toFixed(1);
  }

  map.on('moveend', updateInfo);
  updateInfo();
})();
</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 · EXAMPLEGeolocation OpenLayers · EXAMPLEGeolocation Orientation OpenLayers · EXAMPLEGeotiff Per Band Stats