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

GeoJSON Line

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

Source

demos/maplibre-geojson-line.html
<!--
  示例:GeoJSON 线
  核心逻辑:一份 FeatureCollection 同时提供线与节点,两个图层分别绘制
  依赖: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: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}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <span>节点 <b id="n">6</b> 个</span>
  <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;
  }

  var nodes = [
    { name: '北京', c: [116.391, 39.907] },
    { name: '南京', c: [118.797, 32.060] },
    { name: '上海', c: [121.474, 31.230] },
    { name: '厦门', c: [118.089, 24.479] },
    { name: '广州', c: [113.264, 23.129] },
    { name: '海口', c: [110.350, 20.020] }
  ];

  var features = [{
    type: 'Feature',
    properties: { kind: 'route', name: '东部沿海路线' },
    geometry: {
      type: 'LineString',
      coordinates: nodes.map(function (o) { return o.c; })
    }
  }];

  nodes.forEach(function (o) {
    features.push({
      type: 'Feature',
      properties: { kind: 'node', name: o.name },
      geometry: { type: 'Point', coordinates: o.c }
    });
  });

  var data = { type: 'FeatureCollection', features: features };

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

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

    map.addLayer({
      id: 'route-line',
      type: 'line',
      source: 'route',
      filter: ['==', ['geometry-type'], 'LineString'],
      layout: { 'line-cap': 'round', 'line-join': 'round' },
      paint: {
        'line-color': '#1f5fd0',
        'line-width': ['interpolate', ['linear'], ['zoom'], 2, 1.5, 6, 4],
        'line-dasharray': [2, 1.4]
      }
    });

    map.addLayer({
      id: 'route-nodes',
      type: 'circle',
      source: 'route',
      filter: ['==', ['geometry-type'], 'Point'],
      paint: {
        'circle-radius': 5,
        'circle-color': '#fff',
        'circle-stroke-color': '#1f5fd0',
        'circle-stroke-width': 2
      }
    });

    map.on('click', 'route-nodes', function (e) {
      var f = e.features[0];
      new maplibregl.Popup({ offset: 12 })
        .setLngLat(f.geometry.coordinates.slice())
        .setText(f.properties.name)
        .addTo(map);
    });

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

  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 · EXAMPLEGeoJSON Points MapLibre GL JS · EXAMPLEGeoJSON Polygon MapLibre GL JS · EXAMPLEHeatmap