Atlaslog
Atlaslog / Examples / deck.gl / GeoJSON Style
deck.gl · Runnable Example

GeoJSON Style

Runnable deck.gl example: GeoJSON Style. 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 deck.gl examples
/demos/deckgl-geojson-style.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/deckgl-geojson-style.html
<!--
  示例:GeoJSON 样式
  要点:GeoJsonLayer 用回调按要素属性给点线面配色与描边
  依赖:deck.gl 9.0 · 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>
<style>
  html,body{height:100%;margin:0;background:#0d1117}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
  .bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
  .bar b{color:#58a6ff;font-weight:600}
  .bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
  .bar input[type=checkbox]{accent-color:#4f9ef8}
  .bar input[type=range]{width:150px;accent-color:#4f9ef8}
  .bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #2f3947;background:#1d242e;color:#c9d3e0}
  .bar button:hover{background:#26303d}
  #cv{flex:1 1 auto;min-height:0;width:100%;display:block}

</style>
</head>
<body>
<div class="bar"><span>要素 <b id="n">—</b></span><span>面按等级配色 · 线加粗 · 点带描边</span></div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
  'use strict';

  if (typeof deck === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }
  var features = [];
  for (var i = 0; i < 4; i++) {
    var cx = 116.28 + i * 0.055;
    var cy = 39.90;
    features.push({
      type: 'Feature',
      properties: { kind: 'polygon', level: i + 1, name: '片区 ' + (i + 1) },
      geometry: {
        type: 'Polygon',
        coordinates: [[
          [cx - 0.02, cy - 0.03], [cx + 0.02, cy - 0.03],
          [cx + 0.025, cy + 0.03], [cx - 0.015, cy + 0.035], [cx - 0.02, cy - 0.03]
        ]]
      }
    });
  }
  for (var m = 0; m < 3; m++) {
    var y = 39.84 + m * 0.02;
    features.push({
      type: 'Feature',
      properties: { kind: 'line', level: m + 1, name: '通道 ' + (m + 1) },
      geometry: {
        type: 'LineString',
        coordinates: [[116.26, y], [116.33, y + 0.012], [116.40, y - 0.006], [116.47, y + 0.01]]
      }
    });
  }
  for (var p = 0; p < 5; p++) {
    features.push({
      type: 'Feature',
      properties: { kind: 'point', level: (p % 3) + 1, name: '站点 ' + (p + 1) },
      geometry: { type: 'Point', coordinates: [116.27 + p * 0.045, 39.97] }
    });
  }

  document.getElementById('n').textContent = features.length;

  var deckgl = new deck.Deck({
    canvas: 'cv',
    width: '100%',
    height: '100%',
    initialViewState: { longitude: 116.37, latitude: 39.90, zoom: 10.6, pitch: 0, bearing: 0 },
    controller: true,
    layers: [
      new deck.GeoJsonLayer({
        id: 'mixed',
        data: { type: 'FeatureCollection', features: features },
        filled: true,
        getFillColor: function (f) {
          if (f.properties.kind !== 'polygon') return [0, 0, 0, 0];
          var lv = f.properties.level;
          return [Math.round(50 + lv * 45), Math.round(180 - lv * 22), Math.round(200 - lv * 26), 175];
        },
        getLineColor: function (f) {
          return f.properties.kind === 'line' ? [255, 198, 92, 230] : [226, 238, 250, 210];
        },
        getLineWidth: function (f) { return f.properties.kind === 'line' ? 3 : 1; },
        lineWidthUnits: 'pixels',
        lineWidthMinPixels: 1,
        getPointRadius: function (f) { return 4 + f.properties.level * 2; },
        pointRadiusUnits: 'pixels',
        getPointColor: [255, 255, 255, 230],
        stroked: true,
        pickable: true
      })
    ],
    getTooltip: function (info) {
      if (!info.object) return null;
      return info.object.properties.name;
    }
  });
  window.__deck = deckgl;
})();
</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 deck.gl examples

deck.gl · EXAMPLEGreat Circle deck.gl · EXAMPLEGrid deck.gl · EXAMPLEGrid Extruded