Display thousands of lines in Leaflet

Learn how to handle thousands of lines with Leaflet JavaScript library using the leaflet-maptilersdk plugin. No matter how many vertices your lines have, the performance of your application will not be affected.

This Leaflet example shows how to add a line layer with an outline glow blur symbol to the map using the polyline layer helper. Download the NY Subway lines sample data.

NPM module setup


npm install --save @maptiler/leaflet-maptilersdk leaflet
import { MaptilerLayer, MapStyle } from '@maptiler/leaflet-maptilersdk';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';

const map = L.map('map').setView([40.7396, -73.8931], 11);
const mtLayer = new MaptilerLayer({
  apiKey: 'YOUR_MAPTILER_API_KEY_HERE',
  style: MapStyle.DATAVIZ.DARK, // optional
}).addTo(map);

mtLayer.on("ready", () => {     
  
  // Leverage the MapTiler SDK layer helpers to easily add polyline / point / polygon / heatmap layers
  mtLayer.addPolyline({
    data: 'YOUR_MAPTILER_DATASET_ID_HERE',
    // --> Main line
    lineColor: "#fff",       // White
    lineOpacity: 0.7,        // Partially transparent to see the halo color through

    // --> Halo line
    outline: true,           // To create the electric halo, the outline is
    outlineColor: "#d83bff", // purple
    outlineOpacity: 0.5,     // half transparent
    outlineWidth: 15,        // quite large compared to the main line
    outlineBlur: 15,         // and blurry
  });

});

Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.

Learn more

Check out the Polyline Layer Helper documentation and look at the Polyline Helper examples to see the visualization possibilities.

Display thousands of polygons in Leaflet

Display thousands of polygons in Leaflet

Examples

Display thousands of polygons efficiently in Leaflet using maptilersdk.

Plotting thousands or millions of points in Leaflet

Plotting millions of points in Leaflet

Examples

Display millions of points efficiently in Leaflet using maptilersdk.

Was this helpful?