Leaflet is a JavaScript library for creating interactive maps very easily. It’s open source and mobile-friendly, which in plain English means it works very well on phones.
Leaflet was created in 2011 by Volodymyr Agafonkin, a Ukrainian developer who is currently living through the war in his country, as the website reminds us when we visit it.
Leaflet lives at https://leafletjs.com/, and it has extensive API documentation, with many tutorials and loads of plugins that let you do lots of very cool things. When you visit the site and look at its examples, it may seem limited, as if you can’t do much with this mapping library, but when you see real examples, you quickly realize it’s very powerful. Here are a few of them.
- Civilian Harm in Ukraine (bellingcat.com)
- Ukraine Interactive map – Ukraine Latest news on live map – liveuamap.com
- Many examples of common and not-so-common things you can do (tomik23.github.io)
In short, you only need to look at the list of companies shown on the home page itself.
How does Leaflet work?
Getting started with a map in Leaflet is relatively easy. You just need to follow the quick start tutorial on the website, but since it assumes we know a few things, I’ll break it down a bit more here so there’s no doubt at all.
Let me say up front that we’ll create a map in a bare-bones HTML file. We’re not going to talk about WordPress or anything like that here; we’ll go back to basics to understand how it works, and then everyone can apply it wherever and however they like.
So, as I said, we start from this simple HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>What is Leaflet</title>
</head>
<body>
</body>
</html>
As you can see, it’s simple: it has the HTML5 doctype tag, the tag that defines the page language, the head tags, where various parameters of the page are defined and inside which we’ll need to load Leaflet, and finally the body tags, where we’ll build and display the map.
As simple as that.
Loading the Leaflet library.
You can download the library and host it yourself. That would be advisable, for example, if we were creating a WordPress plugin, to avoid external requests. That said, the JS library is very lightweight (just 42 KB).
But let’s follow the example its author gives us, so we’ll load it directly from the web. To do this, inside the head tags, just below the meta tags, we’ll add the link to the CSS file that comes with Leaflet.
We’ll enter the following code:
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css"
integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ=="
crossorigin=""/>
Next, and this is very important (the docs warn us about it), the following line loads the JavaScript library itself, after the CSS stylesheet has been loaded. Otherwise it won’t work.
Well, we do as we’re told.
<script src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js"
integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
crossorigin=""></script>
With that, our page is ready to use Leaflet. But we still won’t see anything on it.
Note: as you can see in the code, it loads the files for a specific version of the library. At the time of writing, that’s version 1.8.0.
Drawing the map
Right, let’s get to what you really want to see. Let’s draw a map once and for all. To do this, we go inside the body tags, which is where we put everything we want to show on the page (in case you don’t know HTML).
We’ll draw the map inside a div tag with the id map, or whatever we like, but to keep things simple, let’s call it map.
<div id="map"></div>
But this is still almost nothing, an invisible horizontal line on our page, so we need to give it some dimensions. We’ll do that by applying CSS styles. To do this, we go back inside the head tags and, just before the title tag, add some style tags.
Inside them we’ll add the dimensions of the map box, which in our example will be 800px wide by 500px high. So the code will look like this:
<style>
#map {
width: 800px;
height: 500px;
}
</style>
And you’ll say: I still can’t see anything. That’s because we need to initialize the map and tell it what we want to see. To do this, below the div tag where we’ll show the map, we’ll add some script tags, which is where we’ll actually give the instructions to “draw” our map.
The first thing is to initialize the map and tell it which part of the world we want it to show. I want the map to show Granada. For that, I need to know the geographic coordinates of the place I want to show. I’m going to go with the obvious cliché and show the Alhambra on my map. If I go to Google Maps and right-click on it, a context menu appears and the first thing I see are the monument’s geographic coordinates. Clicking on them copies them to the clipboard. They are: 37.17704280619901, -3.5915198660469008

The first number is the latitude, that is, the horizontal lines on maps, and the second number is the longitude, the vertical lines on the map. The first is positive, which means it’s a little over 37° north of the equator, while the second, being negative, means it’s about 3.6° west of the famous Greenwich meridian, the prime meridian.
The line we’ll add inside the script tags is this one:
var map = L.map('map').setView([37.17704280619901, -3.5915198660469008], 13);
What we’ve done is define the map variable and initialize the map with the L object (for Leaflet) (cue a seal clapping in the background). This basically draws the canvas inside the div, and to tell it where we want the center of the map (the point that will sit in the middle of the map), we use the setView method, where we add, in square brackets, the coordinates and the zoom level we want to apply to the map.
Right now you may only be seeing a grey box. That’s because we haven’t told it which base map we want to add. For that we’ll use the tileLayer method.

Here we’ll paste this code directly:
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap'
}).addTo(map);
We won’t go into much depth, but basically it specifies the URL where the tiles live (with certain parameters), the maximum zoom allowed and the attribution, which is the text you see in the corner of some maps.
The result is this:

As we can see, we’re quite far from the Alhambra; you can barely make it out. We can change the zoom level in the line where we set the map view.
var map = L.map('map').setView([37.17704280619901, -3.5915198660469008], 17);
As you can see, things have changed a lot.

So now we have our map drawn, and it wasn’t that hard. If you look, the complete code ends up like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css"
integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js"
integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
crossorigin=""></script>
<style>
#map {
width: 800px;
height: 500px;
}
</style>
<title>What is Leaflet</title>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map').setView([37.17704280619901, -3.5915198660469008], 17);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap'
}).addTo(map);
</script>
</body>
</html>
If you look closely, at the end of the tileLayer method the addTo command runs, which tells it to add that layer to the map object we created when declaring the variable of the same name.
Can we do this a bit more elegantly?
Yes. I had to explain it that way, but I don’t like spaghetti code. So let’s tidy it up and make it a bit more modular to make it more elegant. To do this, we’ll move the CSS changes into a stylesheet called style.css and load it externally. The code will look like this:
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css"
integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js"
integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
crossorigin=""></script>
<link rel="stylesheet" href="style.css">
<title>What is Leaflet</title>
</head>
We’ll also extract all the JavaScript code we used to shape the map, put it in a .js file and link it properly from the index.html file, leaving the body like this.
<body>
<div id="map"></div>
<script src="map.js"></script>
</body>
As you can see, index.html ends up nice and clean. And the style.css and map.js files will look like this.
/* style.css */
#map {
width: 800px;
height: 500px;
}
// map.js
var map = L.map('map').setView([37.17704280619901, -3.5915198660469008], 17);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap'
}).addTo(map);
What else can I do with Leaflet?
Obviously, Leaflet doesn’t stop at showing a map and that’s it. Its power lies precisely in the fact that we can add all kinds of markers (using the marker method), circles, polygons, small popup labels, etc. And since it’s JavaScript, we can make these elements interact based on events triggered by the user.

What’s more, Leaflet has loads of plugins that let you do many things such as searches, geometry editing, animations, heat maps, geoprocessing (as in a GIS), etc.
As you can see, it’s a very powerful tool that we’ll be talking about at length on this blog, and I recommend you look into it.
Tell me, do you have experience with Leaflet? Share with me projects you’ve built with this tool.

Leave a Reply