Skip to content
Make with

Coming from Leaflet

Both Leaflet and MapLibre are permissive open-source libraries. Moving from Leaflet to MapLibre GL JS trades a lightweight DOM/SVG raster renderer for a WebGL2 vector engine with smooth zoom, 3D tilt, and GPU-styled data layers.

What changes in your code

From Leaflet 1.9 to MapLibre GL JS 6, the version this site runs. The two biggest habits to adjust are flipping [lat, lng] to [lng, lat] and splitting data sources from GPU style layers.

  1. 01Loading and initializing a map

    Leaflet creates a map and adds a raster tile layer separately. MapLibre GL JS v6 loads a style JSON (which bundles its own vector sources, fonts, icons, and attribution) and uses named ESM imports. With a bundler, call setWorkerUrl once before creating the first map.

    Leaflet

    import L from 'leaflet';
    import 'leaflet/dist/leaflet.css';
    
    const map = L.map('map').setView([48.8566, 2.3522], 12);
    
    L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '© OpenStreetMap contributors'
    }).addTo(map);

    MapLibre GL JS

    import { Map, setWorkerUrl } from 'maplibre-gl';
    import 'maplibre-gl/dist/maplibre-gl.css';
    import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url';
    
    setWorkerUrl(workerUrl);
    
    const map = new Map({
      container: 'map',
      style: 'https://tiles.openfreemap.org/styles/liberty',
      center: [2.3522, 48.8566],
      zoom: 12
    });
  2. 02Coordinate order: [lat, lng] vs [lng, lat]

    This is the most common migration bug. Leaflet uses [lat, lng] in setView, marker, and bounds (even though GeoJSON inside L.geoJSON uses [lng, lat]). MapLibre uses [lng, lat] everywhere; passing { lng, lat } objects avoids order mistakes while porting code.

    Leaflet

    // Leaflet methods put latitude first
    const paris = [48.8566, 2.3522];
    
    map.setView(paris, 12);
    L.marker(paris).addTo(map);

    MapLibre GL JS

    // MapLibre arrays put longitude first, matching GeoJSON
    const paris = [2.3522, 48.8566];
    // Object form also works: { lng: 2.3522, lat: 48.8566 }
    
    map.jumpTo({ center: paris, zoom: 12 });
    new Marker().setLngLat(paris).addTo(map);
  3. 03Raster XYZ tiles vs vector styles

    Vector styles stay sharp at any fractional zoom, rotate labels upright, and let you restyle layers in the browser. If you have custom raster XYZ imagery or WMS endpoints from a Leaflet app, MapLibre renders them as raster sources too.

    Leaflet

    L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
      maxZoom: 19,
      attribution: '© OpenStreetMap contributors'
    }).addTo(map);

    MapLibre GL JS

    // Vector basemap: pass a style URL directly
    const map = new Map({
      container: 'map',
      style: 'https://tiles.openfreemap.org/styles/liberty'
    });
    
    // Or keep existing XYZ raster tiles in a raster source
    map.on('load', () => {
      map.addSource('my-raster', {
        type: 'raster',
        tiles: ['https://example.com/{z}/{x}/{y}.png'],
        tileSize: 256,
        attribution: '© Example'
      });
      map.addLayer({ id: 'my-raster', type: 'raster', source: 'my-raster' });
    });
  4. 04Markers and popups

    DOM markers work the same way for a handful of pins (pass a custom element via new Marker({ element })). Once you reach hundreds or thousands of markers, move them into a GeoJSON source with a circle or symbol layer so WebGL renders them without DOM overhead.

    Leaflet

    L.marker([48.8566, 2.3522])
      .addTo(map)
      .bindPopup('<h3>Paris</h3><p>Capital of France</p>');

    MapLibre GL JS

    import { Marker, Popup } from 'maplibre-gl';
    
    const popup = new Popup({ offset: 25 })
      .setHTML('<h3>Paris</h3><p>Capital of France</p>');
    
    new Marker()
      .setLngLat([2.3522, 48.8566])
      .setPopup(popup)
      .addTo(map);
  5. 05GeoJSON layers and styling

    Instead of passing a JavaScript style callback per feature, MapLibre splits data (addSource) from styling (addLayer) and evaluates style expressions on the GPU. Polygons with borders thicker than 1 px use a companion line layer sharing the same source.

    Leaflet

    L.geoJSON(zones, {
      style: (feature) => ({
        fillColor: feature.properties.active ? '#2563eb' : '#94a3b8',
        fillOpacity: 0.35,
        color: '#1d4ed8',
        weight: 2
      })
    }).addTo(map);

    MapLibre GL JS

    map.on('load', () => {
      map.addSource('zones', { type: 'geojson', data: zones });
    
      map.addLayer({
        id: 'zones-fill',
        type: 'fill',
        source: 'zones',
        paint: {
          'fill-color': ['case', ['get', 'active'], '#2563eb', '#94a3b8'],
          'fill-opacity': 0.35
        }
      });
    
      map.addLayer({
        id: 'zones-outline',
        type: 'line',
        source: 'zones',
        paint: { 'line-color': '#1d4ed8', 'line-width': 2 }
      });
    });
  6. 06Click and hover events

    Rather than attaching one listener per feature in onEachFeature, register a single layer-scoped listener on the map. MapLibre queries rendered GPU features under the pointer and passes them in e.features.

    Leaflet

    L.geoJSON(zones, {
      onEachFeature: (feature, layer) => {
        layer.on('click', () => {
          console.log(feature.properties.name);
        });
      }
    }).addTo(map);

    MapLibre GL JS

    map.on('click', 'zones-fill', (e) => {
      console.log(e.features?.[0]?.properties.name);
    });
    
    map.on('mouseenter', 'zones-fill', () => {
      map.getCanvas().style.cursor = 'pointer';
    });
    
    map.on('mouseleave', 'zones-fill', () => {
      map.getCanvas().style.cursor = '';
    });
  7. 07Camera, bounds, and 3D tilt

    MapLibre camera methods take an options object and support fractional zoom, rotation (bearing), and 3D tilt (pitch). Remember to flip fitBounds corners from [[south, west], [north, east]] to [[west, south], [east, north]].

    Leaflet

    map.flyTo([48.8566, 2.3522], 13);
    
    map.fitBounds(
      [[48.815, 2.224], [48.902, 2.469]],
      { padding: [40, 40] }
    );

    MapLibre GL JS

    map.flyTo({ center: [2.3522, 48.8566], zoom: 13, pitch: 45, bearing: 20 });
    
    map.fitBounds(
      [[2.224, 48.815], [2.469, 48.902]],
      { padding: 40 }
    );
  8. 08Controls and layer toggles

    MapLibre ships NavigationControl, ScaleControl, GeolocateControl, and FullscreenControl, but has no built-in L.control.layers panel. Toggle layers with setLayoutProperty, or add a layer-switcher plugin such as maplibre-gl-layer-control.

    Leaflet

    L.control.scale().addTo(map);
    L.control.layers({ Streets: osm }, { Zones: zonesLayer }).addTo(map);

    MapLibre GL JS

    import { NavigationControl, ScaleControl } from 'maplibre-gl';
    
    map.addControl(new NavigationControl());
    map.addControl(new ScaleControl());
    
    // Toggle a layer's visibility in code
    map.setLayoutProperty('zones-fill', 'visibility', 'none');

Leaflet plugins, and their MapLibre equivalents

Several popular Leaflet plugins (clustering, heatmaps, vector tiles, custom projections) are built into MapLibre GL JS core; others map directly to maintained MapLibre plugins.

Leaflet.draw / Leaflet-Geoman

Use Terra Draw (or @watergis/maplibre-gl-terradraw for a ready-made toolbar) or MapLibre-Geoman for drawing, editing, and measuring geometries.

Terra Drawmaplibre-geomanmaplibre-gl-terradraw

Leaflet.markercluster

Built into MapLibre GL JS: set cluster: true on any GeoJSON point source to cluster points in a web worker. Add @nazka/map-gl-js-spiderfy if you also want spiderfying for overlapping pins.

map-gl-js-spiderfy

Leaflet.heat

Built into MapLibre GL JS via the native GPU heatmap layer type, with zoom-dependent radius, weight, intensity, and color ramps.

Plugins

leaflet-control-geocoder

Pair @maplibre/maplibre-gl-geocoder with Nominatim, Photon, Pelias, or a hosted geocoding API.

maplibre-gl-geocoderGeocoding

Leaflet Routing Machine

Use @maplibre/maplibre-gl-directions on the client with an open-source routing engine such as OSRM, Valhalla, or GraphHopper, or a hosted routing API.

MapLibre GL DirectionsRouting

L.control.layers / Leaflet-Panel-Layers

Use maplibre-gl-layer-control for layer toggles and opacity sliders, or @watergis/maplibre-gl-legend for an automatic style legend.

maplibre-gl-layer-controlmaplibre-gl-legend

Proj4Leaflet

MapLibre GL JS 6.15+ includes built-in custom planar CRS support via addProjection() with proj4 for national grids, polar maps, and flat image coordinates.

Custom projections

Leaflet.EasyPrint / leaflet-image

Use @watergis/maplibre-gl-export for high-DPI PNG and PDF export, or @versatiles/maplibre-svg-export for vector SVG output.

maplibre-gl-exportMapLibre SVG Export

protomaps-leaflet / Leaflet.VectorGrid

Vector tiles are native in MapLibre. Add the pmtiles protocol once to serve single-file PMTiles archives directly from static object storage.

PMTilesTiling tools

How the two libraries compare

Both are free, permissive open-source libraries built for different trade-offs: Leaflet optimizes for minimal footprint and simplicity without WebGL, while MapLibre optimizes for vector tiles, large datasets, 3D, and cross-platform native parity.

Features of Leaflet 1.9 and MapLibre GL JS v6 compared
FeatureWhereDetails
Rendering engineIn both, differentlyLeaflet renders raster tiles as HTML images and vectors via SVG or 2D Canvas without WebGL. MapLibre GL JS renders vector and raster layers on the GPU with WebGL2.
Bundle size & browser supportLeaflet advantageLeaflet 1.9 is ~40 KB gzipped (plus ~2.4 KB CSS) and runs even where WebGL is disabled. MapLibre GL JS 6.20 is ~302 KB gzipped plus a ~160 KB gzipped worker and requires WebGL2.
Vector tiles & dynamic stylingMapLibre onlyMapLibre renders vector tiles (MVT and MLT) natively with zoom-interpolated expressions, crisp text at any angle, and live style edits; Leaflet requires third-party plugins for vector tiles.
3D camera, globe, terrain, and blend modesMapLibre onlyMapLibre includes camera pitch and bearing, 3D building extrusions, globe projection, raster-DEM terrain, and CSS layer blend modes (GL JS 6.20). Leaflet is strictly 2D.
Large vector datasetsIn both, differentlyLeaflet SVG layers slow down past a few thousand features (Canvas stretches further). MapLibre tiles GeoJSON in a Web Worker and draws tens of thousands of features smoothly on the GPU.
Clustering and heatmapsIn bothLeaflet handles both through popular plugins (Leaflet.markercluster, Leaflet.heat). MapLibre builds point clustering and GPU heatmaps into core.
Custom map projectionsIn bothLeaflet supports custom CRSs through L.CRS and Proj4Leaflet. MapLibre GL JS 6.15+ adds experimental custom planar projections via addProjection() and proj4.
Native mobile SDKsMapLibre onlyLeaflet is web-only (mobile apps wrap it in a WebView). MapLibre Native renders the exact same style JSON natively on iOS (Metal) and Android (Vulkan or OpenGL ES).
Open-source licenseIn bothBoth are permissive open source: Leaflet is BSD-2-Clause and MapLibre GL JS is BSD-3-Clause, with no API keys or usage fees for either library.

From Leaflet and MapLibre docs, checked November 2026.

Choosing a migration path

You do not have to rewrite a complex Leaflet map in one shot, and not every map needs WebGL.

Bridge gradually with maplibre-gl-leaflet

If you have a large Leaflet app with custom plugins you cannot rewrite at once, @maplibre/maplibre-gl-leaflet mounts a MapLibre GL JS vector basemap inside your existing L.map instance so you can keep Leaflet overlays while migrating layer by layer.

MapLibre GL Leaflet →

Switch directly for vector basemaps and big data

For apps with heavy GeoJSON layers, smooth zooming, dark mode, 3D terrain, or shared web-and-mobile styling, moving directly to MapLibre GL JS removes the two-renderer overhead and unlocks the full style specification.

Compare basemaps →

When Leaflet is still the simpler choice

If your page only needs a small 2D raster map with a few pins, must stay around 40 KB gzipped, or runs in environments where WebGL2 is unavailable or disabled, staying on Leaflet is often the right call.

Ready to try it?

Put a MapLibre map on screen in a few lines, try the live Leaflet bridge demo, or compare Mapbox and Google Maps migrations.

Sources (checked November 2026)