Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@maplibre/maplibre-gl-leaflet

maplibre39.2kISC0.1.0TypeScript support: included

Supports adding Maplibre GL Web to a Leaflet Map as a layer

maplibre, gl, leaflet, leafletjs, maps

readme

MapLibre GL Leaflet

This is a binding from MapLibre GL JS to the familiar Leaflet API. It was originally developed for Mapbox (https://github.com/mapbox/mapbox-gl-leaflet) and was migrated to MapLibre after Mapbox changed its license.

Code example

var map = L.map("map").setView([38.912753, -77.032194], 15);
L.marker([38.912753, -77.032194])
  .bindPopup("Hello <b>Leaflet GL</b>!<br>Whoa, it works!")
  .addTo(map)
  .openPopup();
var gl = L.maplibreGL({
  style: "mapbox://styles/mapbox/bright-v8",
}).addTo(map);

Note that you can use any vector tile source useable by maplibre-gl. For instance, you can use OSM2VectorTiles with:

var gl = L.maplibreGL({
  style:
    "https://api.maptiler.com/maps/topo/style.json?key=<YOUR_MAPTILER_API_KEY>",
}).addTo(map);

Once you have created the leaflet layer, the maplibre-gl map object can be accessed using

gl.getMaplibreMap()....
// add a source to the maplibre-gl layer
gl.getMaplibreMap().addSource({...})

Live examples

Basic example

Cluster example

Map events example

Code for these examples is hosted in the examples folder

Installation

Add a script tag referencing maplibre-gl-leaflet after adding leaflet and maplibre-gl-js in your website:

<!-- Leaflet -->
<link
  rel="stylesheet"
  href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css"
/>
<script src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js"></script>

<!-- Maplibre GL -->
<link
  href="https://unpkg.com/maplibre-gl@2.2.1/dist/maplibre-gl.css"
  rel="stylesheet"
/>
<script src="https://unpkg.com/maplibre-gl@2.2.1/dist/maplibre-gl.js"></script>

<script src="https://unpkg.com/@maplibre/maplibre-gl-leaflet@0.0.20/leaflet-maplibre-gl.js"></script>

Motivation

This project makes it possible to easily add a maplibre-gl-js layer in your Leaflet map. When using maplibre-gl-leaflet, you won't be able to use some of the maplibre-gl-js features. Here are the main differences between a "pure" maplibre-gl-js map and a Leaflet map using maplibre-gl-leaflet:

  • No rotation / bearing / pitch support
  • Slower performances: When using maplibre-gl-leaflet, maplibre-gl-js is set as not interactive. Leaflet receives the touch/mouse events and updates the maplibre-gl-js map behind the scenes. Because maplibre-gl-js doesn't redraw as fast as Leaflet, the map can seem slower.

On the bright side, the maplibre-gl-leaflet binding will allow you to use all the leaflet features and plugins.

If you only need the maplibre-gl-js features (adding a map with a mapbox-style, adding a GeoJSON, etc.), you are probably better off using it directly.

API Reference

API Reference

Bug Reports & Feature Requests

Please use the issue tracker to report any bugs or file feature requests. You can fork this jsfiddle template to reproduce a bug, then share the URL of your fork in the GitHub issue.

License

ISC © MapLibre © Mapbox

changelog

Change Log

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

0.1.0

Fixed - 2025-03-24

  • Fix #64 to allow this library to work with MapLibre GL JS v5.0.0. (#66)

0.0.22 - 2024-07-08

Fixed

  • Fix #29 - added missing guard for empty object

0.0.20 - 2023-09-19

Added

  • Add mapbox-gl-js v3.x.x to allowed versions in peerDependencies.

Fixed

  • Fix #29 - layer is mis-aligned with map when panning the map off screen (#31)

0.0.19 - 2022-11-30

Fixed

  • round CSS translate values to avoid blurry tiles (#41).

0.0.18 - 2022-11-11

Fixed

  • import MapOptions instead of obsolete MapboxOptions for compatibility with neuer maplibre-gl versions (#25).

0.0.15 - 2021-07-08

Added

  • types definition

0.0.14 - 2020-11-24

Fixed

  • fix gl offset issue in low zoom level

0.0.13 - 2020-08-31

Added

  • .getPaneName() method

Fixed

  • Allow gl tiles to be added to a custom pane defined in options

0.0.12 - 2020-03-27

Fixed

  • accessToken is now optional

0.0.11 - 2019-11-04

Fixed

  • ensure gl map is added to leaflet TilePane

0.0.10 - 2019-09-16

Added

  • .getContainer(), .getSize(), getBounds() and getCanvas() methods.

Fixed

  • internal code changes to bring it closer to other overlay layers.

0.0.9 - 2019-09-02

Added

  • Added interactive option to make mapbox-gl map events handling possible.
  • added public accessor to mapbox-gl map object

0.0.8 - 2019-08-07

Added

  • Added a padding option to fix the grey backgrougd flickering around the edges of the map while panning/zooming
  • bumped the libraries in examples

0.0.7 - 2019-07-01

Fixed

  • Ensure no blank/gray area is displayed when zooming out.

0.0.6 - 2019-05-07

Fixed

  • .git directory removed from npm tarball.

0.0.5 - 2019-05-01

Added

  • leaflet and mapbox-gl-js are now declared as peerDependencies.

0.0.4 - 2019-02-27

Added

  • UMD wrapper
  • support for pane in constructor

Fixed

  • webkitTransitionEnd event crash

0.0.3 - 2017-04-18

Added

  • Improved support for older versions of mapbox-gl-js

0.0.2 - 2017-03-08

Added

  • Introduced support for Leaflet v1.0.x

0.7. - 2016-10-09

  • Compatibility release for Leaflet v0.7.x