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

GeoJSON Points

Runnable MapLibre GL JS example: GeoJSON Points. 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-points.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-geojson-points.html
<!--
  示例:绘制 GeoJSON 点
  核心逻辑:circle 图层的半径、颜色都由属性驱动,再用 symbol 图层补文字
  数据:页面内构造的 14 个站点,带类型和客流强度
  依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!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:14px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar b{color:#16181d;font-weight:600}
  .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">
  <span>站点 <b>14</b></span>
  <label><input id="t" type="checkbox" checked> 显示名称</label>
  <span>大小读 <b>ridership</b>,颜色读 <b>type</b></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;
  }

  var raw = [
    ['人民广场', 121.47, 31.23, '地铁', 88],
    ['陆家嘴', 121.50, 31.24, '地铁', 76],
    ['虹桥站', 121.32, 31.20, '枢纽', 92],
    ['西直门', 116.35, 39.94, '枢纽', 85],
    ['国贸', 116.46, 39.91, '地铁', 73],
    ['中关村', 116.31, 39.98, '公交', 51],
    ['体育西路', 113.32, 23.13, '地铁', 79],
    ['广州南站', 113.27, 22.99, '枢纽', 90],
    ['福田口岸', 114.06, 22.52, '枢纽', 68],
    ['天府广场', 104.06, 30.66, '地铁', 62],
    ['春熙路', 104.08, 30.65, '公交', 44],
    ['钟楼', 108.94, 34.26, '公交', 39],
    ['江汉路', 114.29, 30.58, '地铁', 55],
    ['西湖文化广场', 120.16, 30.28, '公交', 41]
  ];

  var geojson = {
    type: 'FeatureCollection',
    features: raw.map(function (r) {
      return {
        type: 'Feature',
        properties: { name: r[0], type: r[3], ridership: r[4] },
        geometry: { type: 'Point', coordinates: [r[1], r[2]] }
      };
    })
  };

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

  map.addControl(new maplibregl.NavigationControl(), 'top-right');

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

    map.addLayer({
      id: 'stations-dot',
      type: 'circle',
      source: 'stations',
      paint: {
        'circle-radius': ['interpolate', ['linear'], ['get', 'ridership'], 30, 5, 95, 14],
        'circle-color': [
          'match', ['get', 'type'],
          '枢纽', '#f03e3e',
          '地铁', '#2e86de',
          '#37b24d'
        ],
        'circle-opacity': 0.85,
        'circle-stroke-width': 2,
        'circle-stroke-color': '#ffffff'
      }
    });

    map.addLayer({
      id: 'stations-label',
      type: 'symbol',
      source: 'stations',
      layout: {
        'text-field': ['get', 'name'],
        'text-font': ['Noto Sans Regular'],
        'text-size': 12,
        'text-offset': [0, 1.2],
        'text-anchor': 'top'
      },
      paint: {
        'text-color': '#1f2d3d',
        'text-halo-color': '#ffffff',
        'text-halo-width': 1.4
      }
    });
  });

  document.getElementById('t').addEventListener('change', function () {
    if (map.getLayer('stations-label')) {
      map.setLayoutProperty('stations-label', 'visibility', this.checked ? 'visible' : 'none');
    }
  });

  map.on('mousemove', 'stations-dot', function (e) {
    var p = e.features[0].properties;
    map.getCanvas().style.cursor = 'pointer';
    map.getCanvas().title = p.name + ' · ' + p.type + ' · 客流 ' + p.ridership;
  });

  map.on('mouseleave', 'stations-dot', function () {
    map.getCanvas().style.cursor = '';
    map.getCanvas().title = '';
  });

  map.on('error', function (e) {
    if (e.error) console.warn('地图错误:' + e.error.message);
  });
})();
</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 · EXAMPLEGeoJSON Polygon MapLibre GL JS · EXAMPLEHeatmap MapLibre GL JS · EXAMPLEHover Effect