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

Line Layer

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

Source

demos/maplibre-line-layer.html
<!--
  示例:线图层
  核心逻辑:line 图层加 lineMetrics 源,用 line-progress 做渐变,另配虚线规划段与描边
  依赖: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>线图层</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 b{color:#16181d;font-weight:600}
  .bar input[type=range]{width:120px}
  .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="w" type="range" min="1" max="12" step="0.5" value="5"> <b id="wv">5.0</b></label>
  <label><input id="dash" 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;
  }

  var built = {
    type: 'Feature',
    properties: {},
    geometry: {
      type: 'LineString',
      coordinates: [
        [104.07, 30.67], [106.55, 29.56], [109.49, 30.28],
        [111.29, 30.69], [114.31, 30.59], [116.99, 29.71],
        [118.80, 32.06], [121.47, 31.23]
      ]
    }
  };

  var planned = {
    type: 'Feature',
    properties: {},
    geometry: {
      type: 'LineString',
      coordinates: [
        [118.80, 32.06], [120.15, 30.28], [121.47, 31.23]
      ]
    }
  };

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

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

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

    map.addSource('planned', { type: 'geojson', data: planned });

    /* 描边:先画宽的深色线做底 */
    map.addLayer({
      id: 'route-casing',
      type: 'line',
      source: 'route',
      layout: { 'line-cap': 'round', 'line-join': 'round' },
      paint: {
        'line-color': '#0d2a4a',
        'line-width': 9,
        'line-opacity': 0.35
      }
    });

    /* 主线段,颜色沿线的进度渐变 */
    map.addLayer({
      id: 'route-main',
      type: 'line',
      source: 'route',
      layout: { 'line-cap': 'round', 'line-join': 'round' },
      paint: {
        'line-width': 5,
        'line-gradient': [
          'interpolate', ['linear'], ['line-progress'],
          0, '#1f5fd0',
          0.5, '#2f9e8f',
          1, '#e8590c'
        ]
      }
    });

    map.addLayer({
      id: 'route-planned',
      type: 'line',
      source: 'planned',
      layout: { 'line-cap': 'butt', 'line-join': 'round' },
      paint: {
        'line-color': '#8a6d3b',
        'line-width': 3,
        'line-dasharray': [2, 1.5]
      }
    });
  });

  document.getElementById('w').addEventListener('input', function () {
    var v = parseFloat(this.value);
    document.getElementById('wv').textContent = v.toFixed(1);
    if (map.getLayer('route-main')) {
      map.setPaintProperty('route-main', 'line-width', v);
      map.setPaintProperty('route-casing', 'line-width', v + 4);
    }
  });

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

  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 · EXAMPLELive Update MapLibre GL JS · EXAMPLEMap Export MapLibre GL JS · EXAMPLEMarker