A typical use of Leaflet involves binding a Leaflet "map" element to an HTML element such as a div. Layers and markers are then added to the map element.
In this code sample, the Leaflet library itself is accessible through the variable L.
Leaflet supports Web Map Service (WMS) layers, GeoJSON layers, Vector layers and Tile layers natively. Many other types of layers are supported via plugins.
Like other web map libraries, the basic display model implemented by Leaflet is one basemap, plus zero or more translucent overlays, with zero or more vector objects displayed on top.
The major Leaflet object types are:9
There is also a variety of utility classes such as interfaces for managing projections, transformations and interacting with the DOM.
Leaflet has core support for multiple GIS standard formats, with others supported in plugins.
Leaflet 0.7 supports Chrome, Firefox, Safari 5+, Opera 12+ and IE 7–11.15
Leaflet's onEachFeature is quite handy when dealing with, for example, geojson data. The function contains two parameters: "feature" and "layer". "feature" allows us to access each object inside the geojson and "layer" allows us to add popups, tooltips etc.
An example in javascript is given below:
It is also possible to add "async" keyword to getFeature function in order to use promises such as fetch(). We can utilise properties in each object of geojson to create customised jsonqueries and get, for example, city specific information and display them using layer.bindTooltip, layer.bindPopup etc.
Leaflet is directly comparable with OpenLayers, as both are open source, client-side only JavaScript libraries. The library as a whole is much smaller, around 7,000 lines of code compared to OpenLayers' 230,000 (as of 2015).16 It has a smaller code footprint than OpenLayers (around 123 KB17 vs 423 KB18) due partly to its modular structure. The code base is newer, and takes advantage of recent features of JavaScript, plus HTML5 and CSS3. However, Leaflet lacks features OpenLayers supports, such as Web Feature Service (WFS)19 and native support for projections other than Google Web Mercator (EPSG 3857).20
It is also comparable to the proprietary, closed source Google Maps API (debuting in 2005) and Bing Maps API, both of which incorporate a significant server-side component to provide services such as geocoding, routing, search and integration with features such as Google Earth. Google Maps API provides speed and simplicity, but is not flexible, and can only be used to access Google Maps services. The new DataLayer part of Google's API does allow external data sources to be displayed, however.21
Leaflet began life in 2010 as "Web Maps API", a JavaScript library for the CloudMade mapping provider, where Agafonkin worked at the time. In May 2011, CloudMade announced the first release of Leaflet, built from scratch but using parts of the old API code.22
In March 2022, the developer urged action on the Russian invasion of Ukraine on the Leaflet website.29
Lovelace, Robin. "Testing web map APIs - Google vs OpenLayers vs Leaflet". Archived from the original on 2017-11-03. Retrieved 2018-11-03. https://web.archive.org/web/20171103225922/http://robinlovelace.net/software/2014/03/05/webmap-test.html ↩
MacWright, Tom (2014-08-06). "Leaflet Creator Vladimir Agafonkin Joins MapBox". Retrieved 2018-11-03. https://www.mapbox.com/blog/vladimir-agafonkin-joins-mapbox/ ↩
Agafonkin, V. (2024, July 28). An open-source JavaScript library for mobile-friendly interactive maps. https://leafletjs.com/index.html https://leafletjs.com/index.html ↩
"Leaflet API reference". Retrieved 2018-11-03. https://leafletjs.com/reference-1.3.4.html ↩
"Using GeoJSON with Leaflet". Retrieved 2018-11-03. https://leafletjs.com/examples/geojson/ ↩
"leaflet-omnivore". October 5, 2021 – via GitHub. https://github.com/mapbox/leaflet-omnivore ↩
"TileLayer.WMS". Retrieved 2018-11-03. https://leafletjs.com/reference-1.3.4.html#tilelayer-wms ↩
"Leaflet with WFS Example". July 19, 2019 – via GitHub. https://github.com/Georepublic/leaflet-wfs ↩
"Support for GML". GitHub. 2012-06-23. Retrieved 2018-11-03. https://github.com/Leaflet/Leaflet/issues/547 ↩
"Features". Retrieved 2018-11-03. https://leafletjs.com/#features ↩
"OpenHub.net comparison between OpenLayers and Leaflet". OpenHub.net. Archived from the original on 2014-08-08. Retrieved 2018-11-03. https://web.archive.org/web/20140808124119/https://www.openhub.net/p/compare?project_0=OpenLayers&project_1=Leaflet ↩
"Leaflet frontpage". Leaflet - An Open-Source JavaScript Library for Mobile-Friendly Interactive Maps. Retrieved 2018-11-03. https://leafletjs.com/ ↩
"OpenLayers 3.4.0 compressed source code". OpenLayers.org. Archived from the original on 2016-11-29. Retrieved 2018-11-03. https://web.archive.org/web/20161129010528/http://openlayers.org/en/v3.4.0/build/ol.js ↩
Various plugins providing WFS-support are listed on https://leafletjs.com/plugins.html https://leafletjs.com/plugins.html ↩
"Projection". Retrieved 2018-11-03. https://leafletjs.com/reference-1.3.4.html#projection ↩
"Data Layer". Google Maps Platform. Google Inc. Retrieved 2018-11-03. https://developers.google.com/maps/documentation/javascript/datalayer ↩
"Announcing Leaflet: a Modern Open Source JavaScript Library for Interactive Maps". CloudMade. 2011-05-13. Archived from the original on 2014-08-11. Retrieved 2018-11-03. https://archive.today/20140811222442/http://blog.cloudmade.com/2011/05/13/announcing-leaflet-a-modern-open-source-javascript-library-for-interactive-maps/ ↩
Agafonkin, Vladimir (2013-01-17). "Leaflet 0.5 Released". Retrieved 2018-11-03. https://leafletjs.com/2013/01/17/leaflet-0-5-released.html ↩
Agafonkin, Vladimir (2013-06-26). "Leaflet 0.6 Released, Code Sprint in DC with MapBox". Retrieved 2018-11-03. https://leafletjs.com/2013/06/26/leaflet-0-6-released-dc-code-sprint-mapbox.html ↩
Agafonkin, Vladimir (2013-11-18). "Leaflet 0.7 Release, MapBox and Plans for Future". Retrieved 2018-11-03. https://leafletjs.com/2013/11/18/leaflet-0-7-released-plans-for-future.html ↩
Agafonkin, Vladimir (2016-09-27). "Meet Leaflet 1.0". Retrieved 2018-11-03. https://leafletjs.com/2016/09/27/leaflet-1.0-final.html ↩
Leaflet 1.8 released in the middle of war ↩
v1.9.0 https://github.com/Leaflet/Leaflet/releases/tag/v1.9.0 ↩
"Leaflet - a JavaScript library for interactive maps". 2022-03-21. Archived from the original on 21 March 2022. Retrieved 2022-03-22. https://web.archive.org/web/20220321033923/https://leafletjs.com/ ↩