Software:Leaflet

From HandWiki
Short description: JavaScript library for web mapping applications
Leaflet
Leaflet logo.svg
Original author(s)Volodymyr Agafonkin
Initial releaseMay 13, 2011 (2011-05-13)
Written inJavaScript
PlatformSee Browser support
TypeJavaScript library
LicenseBSD-2-Clause[1]
Websiteleafletjs.com

Leaflet is an open source JavaScript library used to build web mapping applications. First released in 2011,[2] it supports most mobile and desktop platforms, supporting HTML5 and CSS3. Among its users are FourSquare, Pinterest and Flickr.

Leaflet allows developers without a GIS background to very easily display tiled web maps hosted on a public server, with optional tiled overlays. It can load feature data from GeoJSON files, style it and create interactive layers, such as markers with popups when clicked.

It is developed by Volodymyr Agafonkin, who joined Mapbox in 2013.[3]

Use

A basic demo using Leaflet.

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.

<html>
<head>
  <title>Leaflet Map Example</title>
  <!-- Link to Leaflet CSS file -->
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
  <!-- Link to Leaflet JavaScript file -->
  <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
  <style>
    #map {
      height: 250px;
	  width: 400px;
	  border: 1px solid gray;
    }
  </style>
</head>
<body>
  <div id="map"></div>
  <script>
    // Initialize the map
    var map = L.map('map').setView([51.505, -0.09], 13);

    // Add the tile layer (you can choose a different map style by changing the URL)
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
		attribution: '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
	}).addTo(map);

    // Add a circle overlay with a specific radius and color
    var circle = L.circle([51.508, -0.11], {
      color: 'red',
      radius: 500 // Radius in meters
    }).addTo(map);

    // Add a marker with a popup
    var marker = L.marker([51.5, -0.09]).addTo(map)
      .bindPopup('<b>Hello World!</b><br/> I am a popup.');

  </script>
</body>
</html>

In this code sample, the Leaflet library itself is accessible through the variable L.

Features

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.

Elements

The major Leaflet object types are:[4]

  • Raster types (TileLayer and ImageOverlay)
  • Vector types (Path, Polygon, and specific types such as Circle)
  • Grouped types (LayerGroup, FeatureGroup and GeoJSON)
  • Controls (Zoom, Layers, etc.)

There is also a variety of utility classes such as interfaces for managing projections, transformations and interacting with the DOM.

Support for GIS formats

Leaflet has core support for multiple GIS standard formats, with others supported in plugins.

Standard Support
GeoJSON Good, core support through the geoJson function[5]
KML, CSV, WKT, TopoJSON, GPX Supported in Leaflet-Omnivore plugin[6]
WMS Core support through the TileLayer.WMS[7] subtype
WFS Not supported, although third-party plugins exist.[8]
GML Not supported.[9]

Browser support

Leaflet 0.7 supports Chrome, Firefox, Safari 5+, Opera 12+ and IE 7–11.[10]

Examples of useful features

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:

let geoJson = L.geoJSON(geoJsonData, {weight:2,
        onEachFeature: getFeature,
        style: getStyle
    }).addTo(map);
    
    const getFeature= (feature, layer) =>{
        if (!feature.properties.name) return
        layer.bindTooltip(feature.properties.cityName); 
        layer.bindPopup(
            `
            <ul>
                <li>Name: ${feature.properties.cityName}</li>
                <li>Population: ${feature.properties.population}</li>
            </ul>
            `
    )

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.

Comparison with other libraries

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).[11] It has a smaller code footprint than OpenLayers (around 123 KB[12] vs 423 KB[13]) 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)[14] and native support for projections other than Google Web Mercator (EPSG 3857).[15]

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.[citation needed] 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.[16]

History

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.[17]

  • 0.1: May 17, 2011
  • 0.2: June 18, 2011
  • 0.3: Feb 14, 2012
  • 0.4: Jul 30, 2012
  • 0.5: Jan 17, 2013
    • This release introduced Retina support and many usability and user experience improvements.[18]
  • 0.6: Jun 26, 2013
    • This release expanded the API's range of methods and events, improved usability, and added GeoJSON saving. It was completed in a 2-day code sprint supported by Mapbox.[19]
  • 0.7: Nov 22, 2013
    • This release focused on bug fixing, announcing that refactoring and potential backward incompatibilities would come soon.[20]
  • 1.0: Sep 27, 2016
    • This release contained over 400 changes compared to v0.7.7:[21]
      • Performance improvements in all aspects of the library and vector layers in particular.
      • Flyover animations (zooming and panning in a curve).
      • Fractional zoom level support.
      • Better tile loading algorithm with less flickering.
      • Custom pane management (including multiple vector layer panes and interleaving vectors and tile layers).
      • Better support for non-standard projections.
      • More accessibility features.
      • Improved documentation.
      • Stability improvements.
  • 1.1: Jun 27, 2017
    • This release adds video overlays and makes a transition to ECMAScript 6 modules.
  • 1.2: Oct 25, 2017
  • 1.3: Jan 15, 2018
  • 1.3.2: Jul 17, 2018
  • 1.3.3: Jul 18, 2018
  • 1.3.4: Aug 21, 2018
  • 1.4.0: Dec 30, 2018
  • 1.5.0 and 1.5.1 : May 8, 2019
  • 1.6.0: Nov 17, 2019
  • 1.7.1 : September 4, 2020
  • 1.8 : April 18, 2022 [22]
  • 1.9 : September 22, 2022 [23]

In March 2022, the developer urged action on the Russian invasion of Ukraine on the leafletjs website.[24]

References

  1. "License - Leaflet". Leaflet. https://github.com/Leaflet/Leaflet/blob/master/LICENSE. 
  2. Lovelace, Robin. "Testing web map APIs - Google vs OpenLayers vs Leaflet". http://robinlovelace.net/software/2014/03/05/webmap-test.html. 
  3. MacWright, Tom (2014-08-06). "Leaflet Creator Vladimir Agafonkin Joins MapBox". https://www.mapbox.com/blog/vladimir-agafonkin-joins-mapbox/. 
  4. "Leaflet API reference". https://leafletjs.com/reference-1.3.4.html. 
  5. "Using GeoJSON with Leaflet". https://leafletjs.com/examples/geojson/. 
  6. "leaflet-omnivore". October 5, 2021. https://github.com/mapbox/leaflet-omnivore. 
  7. "TileLayer.WMS". https://leafletjs.com/reference-1.3.4.html#tilelayer-wms. 
  8. "Leaflet with WFS Example". July 19, 2019. https://github.com/Georepublic/leaflet-wfs. 
  9. "Support for GML". 2012-06-23. https://github.com/Leaflet/Leaflet/issues/547. 
  10. "Features". https://leafletjs.com/#features. 
  11. "OpenHub.net comparison between OpenLayers and Leaflet". https://www.openhub.net/p/compare?project_0=OpenLayers&project_1=Leaflet. 
  12. "Leaflet frontpage". https://leafletjs.com/. 
  13. "OpenLayers 3.4.0 compressed source code". OpenLayers.org. http://openlayers.org/en/v3.4.0/build/ol.js. 
  14. Various plugins providing WFS-support are listed on https://leafletjs.com/plugins.html
  15. "Projection". https://leafletjs.com/reference-1.3.4.html#projection. 
  16. "Data Layer". Google Maps Platform. Google Inc.. https://developers.google.com/maps/documentation/javascript/datalayer. 
  17. "Announcing Leaflet: a Modern Open Source JavaScript Library for Interactive Maps". 2011-05-13. http://blog.cloudmade.com/2011/05/13/announcing-leaflet-a-modern-open-source-javascript-library-for-interactive-maps/. 
  18. Agafonkin, Vladimir (2013-01-17). "Leaflet 0.5 Released". https://leafletjs.com/2013/01/17/leaflet-0-5-released.html. 
  19. Agafonkin, Vladimir (2013-06-26). "Leaflet 0.6 Released, Code Sprint in DC with MapBox". https://leafletjs.com/2013/06/26/leaflet-0-6-released-dc-code-sprint-mapbox.html. 
  20. Agafonkin, Vladimir (2013-11-18). "Leaflet 0.7 Release, MapBox and Plans for Future". https://leafletjs.com/2013/11/18/leaflet-0-7-released-plans-for-future.html. 
  21. Agafonkin, Vladimir (2016-09-27). "Meet Leaflet 1.0.". https://leafletjs.com/2016/09/27/leaflet-1.0-final.html. 
  22. Leaflet 1.8 released in the middle of war
  23. v1.9.0
  24. "Leaflet - a JavaScript library for interactive maps". 2022-03-21. https://leafletjs.com/. 

External links