Skip to main content
GIS-Schools GIS-Schools
Professional article

How to Display High-Performance Vector Tiles from GeoJSON in Leaflet

In this tutorial, we’ll learn how to render large GeoJSON datasets on a Leaflet map using vector tiles generated dynamically via geojson-vt. This approach offers faster performance, smoother panning/zooming, and canvas rendering for complex layers.

βœ… Why This Matters

Loading raw GeoJSON directly into Leaflet using L.geoJSON() can get painfully slow with thousands of features. geojson-vt chunks your data into canvas-rendered vector tiles — just like Mapbox or MapLibre do — but entirely client-side.

Think of it as slicing a pizza into tiles… Leaflet only loads the slice you're eating (viewing).


Unsupported embedded media was blocked.


Download Source Code: Click Here

Watch This Tutorial in Video: Click Here


 

🧰 Prerequisites

  • Basic HTML and JS knowledge

  • GeoJSON data file

  • Leaflet (v1.7+ recommended)

  • jQuery (for document ready)

  • Bootstrap (optional, for layout)

  • geojson-vt library

  • leaflet-geojson-vt plugin (custom) 

πŸ—‚οΈ Folder Structure

project/
β”‚
β”œβ”€β”€ index.html
β”œβ”€β”€ data/
β”‚   └── data.js            # Contains your GeoJSON as a JS variable
β”œβ”€β”€ js/
β”‚   β”œβ”€β”€ source_code.js     # Leaflet init + geojson-vt logic
β”‚   └── leaflet-geojson-vt.js  # Plugin that draws vector tiles on canvas
β”œβ”€β”€ lib/
β”‚   └── leaflet/
β”‚       β”œβ”€β”€ leaflet.js
β”‚       β”œβ”€β”€ leaflet.css
β”‚       β”œβ”€β”€ leaflet.ajax.min.js
β”‚       └── jquery-3.5.1.js
└── lib/bootstrap/css/bootstrap.css

 

πŸ”Œ Step-by-Step Implementation

1. HTML Skeleton (index.html)

Loads Leaflet, Bootstrap, and JS files.

 

 

Key imports:

 

2. GeoJSON Source (data/data.js)

You must define a global data variable:

var data = {
  "type": "FeatureCollection",
  "features": [
    // your features here
  ]
};


3. Vector Tile Plugin (leaflet-geojson-vt.js)

This custom plugin extends L.GridLayer and draws vector tiles using canvas.

Key logic:

createTile: function (coords) {
    const tile = L.DomUtil.create("canvas", "leaflet-tile");
    ...
    const features = this.tileIndex.getTile(coords.z, coords.x, coords.y)?.features || [];
    features.forEach(f => this.drawFeature(ctx, f));
    return tile;
}

4. Map Setup (js/source_code.js)

myMap = L.map('map_div', {
    center:[38.91454,-77.02171],
    zoom:12,
    zoomControl: false
});

lyrOSM = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(myMap);

var vtLayer = L.geoJson.vt(data, {
    maxZoom: 20,
    tolerance: 3,
    style: {
        fillColor: "#F2FF00",
        color: "#1EB300",
    }
}).addTo(myMap);

⚠️ Common Gotchas

  • No geometry appears? Make sure your GeoJSON data variable is loaded before the script runs.

  • Tile flickering or overlapping? Set canvas tile size consistently and manage CSS z-index.

  • Too many features per tile? Adjust maxZoom, tolerance, and enable debug: 1 in options.

πŸ“ˆ Performance Tips

  • Convert large shapefiles to GeoJSON offline before using.

  • Use simplify-geojson or reduce feature count for better zoom responsiveness.

  • Explore hybrid client-server approaches later with pg_tileserv or tilemaker.

πŸ’‘ Next Enhancements

  • Load GeoJSON from server via AJAX

  • Add dynamic styling by feature properties

  • Add legend and filter controls

  • Use maplibre-gl for true vector rendering later

πŸ”š Conclusion

By using geojson-vt With Leaflet and a bit of canvas wizardry, you can render huge GeoJSON datasets on the fly — perfect for public dashboards, mobile-first maps, or offline-first apps.

Frequently Asked Questions

Q: What are Vector Tiles in Leaflet?

A: Vector tiles are small chunks of GeoJSON rendered as canvas tiles, allowing fast map performance in Leaflet even with large datasets.

Q: Why use geojson-vt instead of L.geoJSON?

A: geojson-vt converts GeoJSON to tiled format, enabling smooth zoom and pan without crashing the browser, unlike the basic L.geoJSON approach.

Q: Can I load GeoJSON from a URL instead of embedding it?

A: Yes, you can load external GeoJSON via AJAX and feed it to geojson-vt dynamically using Leaflet AJAX plugin.

Muhammad Sohail
Written by

Muhammad Sohail

Web Developer

I am a highly skilled GIS Specialist with a proven track record of delivering high-quality GIS solutions to clients. My expertise in GIS software development, geodatabase design and management, data analysis and visualization, remote sensing, GIS training and support, and GIS consulting. With my technical skills, critical thinking, and business acumen, I am confident in my ability to provide excellent GIS services to clients and deliver results that exceed their expectations.

Discussion

Comments

0

No comments yet. Start the discussion.

Join the discussion

Leave a reply

Your email address will not be published. Required fields are marked.