Atlaslog
Atlaslog / Examples / MapLibre GL JS / GeoJSON Polygon
MapLibre GL JS · Runnable Example

GeoJSON Polygon

Runnable MapLibre GL JS example: GeoJSON Polygon. 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 MapLibre GL JS examples
/demos/maplibre-geojson-polygon.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-geojson-polygon.html
<!--
  示例:GeoJSON 面
  核心逻辑:两个面带属性色,fill 填色、line 描边共用同一数据源
  依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 免 Key 样式
-->
<!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/maplibre-gl@4/dist/maplibre-gl.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;color:#16181d}
  .bar{flex:0 0 auto;display:flex;gap:12px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <label><input id="edge" type="checkbox" checked> 显示边界线</label>
  <span>点击面查看名称</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
  'use strict';

  if (typeof maplibregl === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  function rect(west, south, east, north) {
    return [[west, south], [east, south], [east, north], [west, north], [west, south]];
  }

  var data = {
    type: 'FeatureCollection',
    features: [
      {
        type: 'Feature',
        properties: { name: '京津冀示意范围', color: '#1f5fd0' },
        geometry: { type: 'Polygon', coordinates: [rect(114.5, 38.0, 119.0, 41.5)] }
      },
      {
        type: 'Feature',
        properties: { name: '长三角示意范围', color: '#17694a' },
        geometry: { type: 'Polygon', coordinates: [rect(118.0, 29.5, 123.0, 32.5)] }
      }
    ]
  };

  var map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [117.5, 34.5],
    zoom: 3,
    attributionControl: {
      compact: true,
      customAttribution: 'MapLibre demo tiles'
    }
  });

  map.on('load', function () {
    map.addSource('regions', { type: 'geojson', data: data });

    map.addLayer({
      id: 'regions-fill',
      type: 'fill',
      source: 'regions',
      paint: {
        'fill-color': ['get', 'color'],
        'fill-opacity': 0.28
      }
    });

    map.addLayer({
      id: 'regions-line',
      type: 'line',
      source: 'regions',
      paint: {
        'line-color': ['get', 'color'],
        'line-width': 2
      }
    });

    map.on('click', 'regions-fill', function (e) {
      var f = e.features[0];
      new maplibregl.Popup({ offset: 8 })
        .setLngLat(e.lngLat)
        .setHTML('<b>' + f.properties.name + '</b><br>仅为演示用的矩形范围')
        .addTo(map);
    });

    map.on('mouseenter', 'regions-fill', function () {
      map.getCanvas().style.cursor = 'pointer';
    });
    map.on('mouseleave', 'regions-fill', function () {
      map.getCanvas().style.cursor = '';
    });
  });

  document.getElementById('edge').addEventListener('change', function () {
    if (!map.getLayer('regions-line')) return;
    map.setLayoutProperty('regions-line', 'visibility', this.checked ? 'visible' : 'none');
  });

  map.addControl(new maplibregl.NavigationControl(), 'top-right');
})();
</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 MapLibre GL JS examples

MapLibre GL JS · EXAMPLEHeatmap MapLibre GL JS · EXAMPLEHover Effect MapLibre GL JS · EXAMPLEImage Source