{"id":60646,"date":"2022-06-29T10:30:55","date_gmt":"2022-06-29T08:30:55","guid":{"rendered":"https:\/\/jesusyesares.com\/?p=60646"},"modified":"2026-11-27T18:22:58","modified_gmt":"2026-11-27T17:22:58","slug":"what-is-leaflet-javascript-library","status":"publish","type":"post","link":"https:\/\/jesusyesares.com\/en\/visualizacion-datos\/what-is-leaflet-javascript-library\/","title":{"rendered":"What is Leaflet?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Leaflet<\/strong> is a <strong>JavaScript<\/strong> library for creating <strong>interactive maps<\/strong> very easily. It&#8217;s <strong>open source<\/strong> and <strong>mobile-friendly<\/strong>, which in plain English means it works very well on phones.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Leaflet<\/strong> was created in 2011 by <a aria-label=\"Volodymyr Agafonkin (opens in a new tab)\" href=\"https:\/\/agafonkin.com\/\" target=\"_blank\" rel=\"noopener\" class=\"ek-link\">Volodymyr Agafonkin<\/a>, a Ukrainian developer who is currently living through the war in his country, as the website reminds us when we visit it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Leaflet<\/strong> lives at <a aria-label=\"https:\/\/leafletjs.com\/ (opens in a new tab)\" href=\"https:\/\/leafletjs.com\/\" target=\"_blank\" rel=\"noopener\" class=\"ek-link\">https:\/\/leafletjs.com\/<\/a>, and it has <strong>extensive API documentation<\/strong>, 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&#8217;t do much with this mapping library, but when you see real examples, you quickly realize it&#8217;s very powerful. Here are a few of them.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/ukraine.bellingcat.com\/\" target=\"_blank\" rel=\"noopener\" class=\"ek-link\">Civilian Harm in Ukraine (bellingcat.com)<\/a><\/li>\n\n\n\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/liveuamap.com\/\" target=\"_blank\" rel=\"noopener\" class=\"ek-link\">Ukraine Interactive map &#8211; Ukraine Latest news on live map &#8211; liveuamap.com<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/tomik23.github.io\/leaflet-examples\/#01.simple-map\" target=\"_blank\" aria-label=\"Many examples of common and not-so-common things you can do (tomik23.github.io) (opens in a new tab)\" rel=\"noopener\" class=\"ek-link\">Many examples of common and not-so-common things you can do (tomik23.github.io)<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In short, you only need to look at the list of companies shown on the home page itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How does Leaflet work?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Getting started with a map in Leaflet is relatively easy. You just need to follow the <strong>quick start tutorial<\/strong> on the website, but <strong>since it assumes we know a few things, I&#8217;ll break it down a bit more here<\/strong> so there&#8217;s no doubt at all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let me say up front that we&#8217;ll create a map in a bare-bones HTML file. We&#8217;re <strong>not going to talk about WordPress<\/strong> or anything like that here; we&#8217;ll go back to basics to understand how it works, and then everyone can apply it wherever and however they like.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, as I said, we start from this simple HTML file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n\t&lt;meta charset=&quot;UTF-8&quot;&gt;\n\t&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;\n\t&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n\t&lt;title&gt;What is Leaflet&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\t\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">As you can see, it&#8217;s simple: it has the HTML5 <em>doctype<\/em> tag, the tag that defines the page language, the <em>head<\/em> tags, where various parameters of the page are defined and inside which we&#8217;ll need to load Leaflet, and finally the <em>body<\/em> tags, where we&#8217;ll build and display the map.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As simple as that.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Loading the Leaflet library.<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>You can download the library and host it yourself<\/strong>. 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).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But let&#8217;s follow the example its author gives us, so <strong>we&#8217;ll load it directly from the web<\/strong>. To do this, inside the <em>head<\/em> tags, just below the <em>meta<\/em> tags, we&#8217;ll add <strong>the link to the CSS file that comes with Leaflet.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll enter the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n&lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/unpkg.com\/leaflet@1.8.0\/dist\/leaflet.css&quot;\n   integrity=&quot;sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe\/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr\/Fs0W40kiNHxM9vyTtQ==&quot;\n   crossorigin=&quot;&quot;\/&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Next, and this is very important (the docs warn us about it), <strong>the following line loads the JavaScript library itself<\/strong>, after the CSS stylesheet has been loaded. Otherwise it won&#8217;t work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Well, we do as we&#8217;re told.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n &lt;script src=&quot;https:\/\/unpkg.com\/leaflet@1.8.0\/dist\/leaflet.js&quot;\n   integrity=&quot;sha512-BB3hKbKWOc9Ez\/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8\/KABdlkX68nAhlwcDFLGPCQ==&quot;\n   crossorigin=&quot;&quot;&gt;&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">With that, our page is ready to use Leaflet. But we still won&#8217;t see anything on it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: as you can see in the code, <strong>it loads the files for a specific version of the library<\/strong>. At the time of writing, that&#8217;s version 1.8.0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Drawing the map<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Right, let&#8217;s get to what you really want to see. Let&#8217;s draw a map once and for all. To do this, we go inside the <em>body<\/em> tags, which is where we put everything we want to show on the page (in case you don&#8217;t know HTML).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll draw the map inside a <em>div<\/em> tag with the id <strong>map<\/strong>, or whatever we like, but to keep things simple, let&#8217;s call it <strong>map<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n&lt;div id=&quot;map&quot;&gt;&lt;\/div&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">But this is still almost nothing, an invisible horizontal line on our page, so <strong>we need to give it some dimensions<\/strong>. We&#8217;ll do that by <strong>applying CSS styles<\/strong>. To do this, we go back inside the <em>head<\/em> tags and, just before the <em>title<\/em> tag, add some <em>style<\/em> tags.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inside them we&#8217;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:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n&lt;style&gt;\n    #map {\n        width: 800px;\n        height: 500px;\n    }\n&lt;\/style&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">And you&#8217;ll say: <strong>I still can&#8217;t see anything<\/strong>. That&#8217;s because we need to initialize the map and tell it what we want to see. To do this, below the <em>div<\/em> tag where we&#8217;ll show the map, we&#8217;ll add some <em>script<\/em> tags, which is where we&#8217;ll actually give the instructions to &#8220;draw&#8221; our map.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The first thing is to initialize the map and tell it which part of the world we want it to show<\/strong>. I want the map to show <strong>Granada<\/strong>. For that, I need to know the geographic coordinates of the place I want to show. I&#8217;m going to go with the obvious clich\u00e9 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&#8217;s geographic coordinates. Clicking on them copies them to the clipboard. They are: 37.17704280619901, -3.5915198660469008<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"704\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" src=\"http:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/menu-contextual-google-maps-1024x704.jpeg\" alt=\"Context menu with the geographic coordinates in Google Maps\" class=\"wp-image-1654\" style=\"width:512px;height:352px\" srcset=\"https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/menu-contextual-google-maps-1024x704.jpeg 1024w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/menu-contextual-google-maps-300x206.jpeg 300w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/menu-contextual-google-maps-768x528.jpeg 768w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/menu-contextual-google-maps.jpeg 1370w\" fetchpriority=\"high\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The first number is the <strong>latitude<\/strong>, that is, the horizontal lines on maps, and the second number is the <strong>longitude<\/strong>, the vertical lines on the map. The first is positive, which means it&#8217;s a little over 37\u00b0 north of the equator, while the second, being negative, means it&#8217;s about 3.6\u00b0 west of the famous Greenwich meridian, the prime meridian.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The line we&#8217;ll add inside the <em>script<\/em> tags is this one:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; light: false; title: ; toolbar: true; notranslate\" title=\"\">\nvar map = L.map(&#039;map&#039;).setView(&#x5B;37.17704280619901, -3.5915198660469008], 13);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">What we&#8217;ve done is define the <strong>map<\/strong> variable and <strong>initialize the map with the L object<\/strong> (for Leaflet) (cue a seal clapping in the background). This basically draws the canvas inside the <em>div<\/em>, 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 <strong>setView<\/strong> method, where we add, in square brackets, the coordinates and the zoom level we want to apply to the map.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Right now you may only be seeing a grey box. That&#8217;s because <strong>we haven&#8217;t told it which base map we want to add<\/strong>. For that we&#8217;ll use the <strong>tileLayer<\/strong> method.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"641\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" src=\"http:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-vacio-1024x641.jpeg\" alt=\"Empty map\" class=\"wp-image-1651\" srcset=\"https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-vacio-1024x641.jpeg 1024w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-vacio-300x188.jpeg 300w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-vacio-768x481.jpeg 768w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-vacio-1536x962.jpeg 1536w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-vacio.jpeg 1792w\" fetchpriority=\"low\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here we&#8217;ll paste this code directly:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; light: false; title: ; toolbar: true; notranslate\" title=\"\">\nL.tileLayer(&#039;https:\/\/{s}.tile.openstreetmap.org\/{z}\/{x}\/{y}.png&#039;, {\n    maxZoom: 19,\n    attribution: &#039;\u00a9 OpenStreetMap&#039;\n}).addTo(map);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We won&#8217;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The result is this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"658\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" src=\"http:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-bajo-1024x658.jpeg\" alt=\"Map zoomed out\" class=\"wp-image-1653\" srcset=\"https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-bajo-1024x658.jpeg 1024w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-bajo-300x193.jpeg 300w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-bajo-768x494.jpeg 768w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-bajo-1536x987.jpeg 1536w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-bajo.jpeg 1662w\" fetchpriority=\"low\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As we can see, we&#8217;re quite far from the Alhambra; you can barely make it out. <strong>We can change the zoom level<\/strong> in the line where we set the map view.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; light: false; title: ; toolbar: true; notranslate\" title=\"\">\nvar map = L.map(&#039;map&#039;).setView(&#x5B;37.17704280619901, -3.5915198660469008], 17);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">As you can see, things have changed a lot.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"648\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" src=\"http:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-alto-1024x648.jpeg\" alt=\"Map zoomed in\" class=\"wp-image-1652\" srcset=\"https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-alto-1024x648.jpeg 1024w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-alto-300x190.jpeg 300w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-alto-768x486.jpeg 768w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-alto-1536x972.jpeg 1536w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/mapa-zoom-alto.jpeg 1668w\" fetchpriority=\"low\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So now we have our map drawn, and it wasn&#8217;t that hard. If you look, the complete code ends up like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n\t&lt;meta charset=&quot;UTF-8&quot;&gt;\n\t&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;\n\t&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n\t&lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/unpkg.com\/leaflet@1.8.0\/dist\/leaflet.css&quot;\n\tintegrity=&quot;sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe\/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr\/Fs0W40kiNHxM9vyTtQ==&quot;\n\tcrossorigin=&quot;&quot;\/&gt;\n\t&lt;script src=&quot;https:\/\/unpkg.com\/leaflet@1.8.0\/dist\/leaflet.js&quot;\n   integrity=&quot;sha512-BB3hKbKWOc9Ez\/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8\/KABdlkX68nAhlwcDFLGPCQ==&quot;\n   crossorigin=&quot;&quot;&gt;&lt;\/script&gt;\n   &lt;style&gt;\n\t\t#map {\n\t\t\twidth: 800px;\n\t\t\theight: 500px;\n\t\t}\n   &lt;\/style&gt;\n\t&lt;title&gt;What is Leaflet&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\t&lt;div id=&quot;map&quot;&gt;&lt;\/div&gt;\n\t&lt;script&gt;\n\t\tvar map = L.map(&#039;map&#039;).setView(&#x5B;37.17704280619901, -3.5915198660469008], 17);\n\t\tL.tileLayer(&#039;https:\/\/{s}.tile.openstreetmap.org\/{z}\/{x}\/{y}.png&#039;, {\n\t\t\tmaxZoom: 19,\n\t\t\tattribution: &#039;\u00a9 OpenStreetMap&#039;\n\t\t}).addTo(map);\n\t&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If you look closely, at the end of the <strong>tileLayer<\/strong> method the <strong>addTo<\/strong> command runs, which tells it to add that layer to the <em>map<\/em> object we created when declaring the variable of the same name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can we do this a bit more elegantly?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. I had to explain it that way, but I don&#8217;t like spaghetti code. So let&#8217;s tidy it up and <strong>make it a bit more modular<\/strong> to make it more elegant. To do this, we&#8217;ll move the CSS changes into a stylesheet called <strong>style.css<\/strong> and load it externally. The code will look like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n&lt;head&gt;\n\t&lt;meta charset=&quot;UTF-8&quot;&gt;\n\t&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;\n\t&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n\t&lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/unpkg.com\/leaflet@1.8.0\/dist\/leaflet.css&quot;\n\tintegrity=&quot;sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe\/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr\/Fs0W40kiNHxM9vyTtQ==&quot;\n\tcrossorigin=&quot;&quot;\/&gt;\n\t&lt;script src=&quot;https:\/\/unpkg.com\/leaflet@1.8.0\/dist\/leaflet.js&quot;\n   integrity=&quot;sha512-BB3hKbKWOc9Ez\/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8\/KABdlkX68nAhlwcDFLGPCQ==&quot;\n   crossorigin=&quot;&quot;&gt;&lt;\/script&gt;\n   &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;\n\t&lt;title&gt;What is Leaflet&lt;\/title&gt;\n&lt;\/head&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll also extract all the JavaScript code we used to shape the map, put it in a <strong>.js<\/strong> file and link it properly from the <strong>index.html<\/strong> file, leaving the <strong>body<\/strong> like this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n&lt;body&gt;\n\t&lt;div id=&quot;map&quot;&gt;&lt;\/div&gt;\n\t&lt;script src=&quot;map.js&quot;&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">As you can see, <strong>index.html<\/strong> ends up nice and clean. And the <strong>style.css<\/strong> and <strong>map.js<\/strong> files will look like this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n\/* style.css *\/\n#map {\n\twidth: 800px;\n\theight: 500px;\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n\/\/ map.js\nvar map = L.map(&#039;map&#039;).setView(&#x5B;37.17704280619901, -3.5915198660469008], 17);\n\t\tL.tileLayer(&#039;https:\/\/{s}.tile.openstreetmap.org\/{z}\/{x}\/{y}.png&#039;, {\n\t\t\tmaxZoom: 19,\n\t\t\tattribution: &#039;\u00a9 OpenStreetMap&#039;\n\t\t}).addTo(map);\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">What else can I do with Leaflet?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Obviously, <strong>Leaflet doesn&#8217;t stop at showing a map and that&#8217;s it<\/strong>. Its power lies precisely in the fact that we can add all kinds of markers (using the <strong>marker<\/strong> method), circles, polygons, small popup labels, etc. And since it&#8217;s JavaScript, <strong>we can make these elements interact based on events triggered by the user<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-left\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"406\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" src=\"http:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/poligonos-popup-leaflet.jpeg\" alt=\"Polygons, markers and popups.\" class=\"wp-image-1661\" srcset=\"https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/poligonos-popup-leaflet.jpeg 600w, https:\/\/jesusyesares.com\/wp-content\/uploads\/2022\/06\/poligonos-popup-leaflet-300x203.jpeg 300w\" fetchpriority=\"low\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">What&#8217;s more, Leaflet has loads of <a href=\"https:\/\/leafletjs.com\/plugins.html\" target=\"_blank\" aria-label=\"plugins (opens in a new tab)\" rel=\"noopener\" class=\"ek-link\">plugins<\/a> that let you do many things such as <strong>searches<\/strong>, <strong>geometry editing<\/strong>, <strong>animations<\/strong>, <strong>heat maps<\/strong>, <strong>geoprocessing<\/strong> (as in a <a href=\"https:\/\/wpcarto.com\/que-es\/un-gis\/\" class=\"ek-link\">GIS<\/a>), etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, it&#8217;s a very powerful tool that we&#8217;ll be talking about at length on this blog, and I recommend you look into it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tell me, do you have experience with <strong>Leaflet<\/strong>? Share with me projects you&#8217;ve built with this tool.<\/p>\n<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:52,&quot;href&quot;:&quot;https:\\\/\\\/agafonkin.com&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20260114125808\\\/https:\\\/\\\/agafonkin.com\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-08-24 18:52:59&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-31 05:47:32&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-11-05 18:00:55&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-11-05T18:00:55+00:00&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:53,&quot;href&quot;:&quot;https:\\\/\\\/leafletjs.com&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20260212091732\\\/https:\\\/\\\/leafletjs.com\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-08-24 18:52:59&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-08-31 06:06:10&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-11-05 18:00:55&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-11-05T18:00:55+00:00&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:54,&quot;href&quot;:&quot;https:\\\/\\\/ukraine.bellingcat.com&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20260211023228\\\/https:\\\/\\\/ukraine.bellingcat.com\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-08-24 18:52:59&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-08-31 06:14:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-11-05 18:00:56&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-11-05T18:00:56+00:00&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:55,&quot;href&quot;:&quot;https:\\\/\\\/liveuamap.com&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20260219120616\\\/https:\\\/\\\/liveuamap.com\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-08-24 18:52:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-08-31 06:14:42&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-11-05 18:00:56&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-11-05T18:00:56+00:00&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:56,&quot;href&quot;:&quot;https:\\\/\\\/tomik23.github.io\\\/leaflet-examples\\\/#01.simple-map&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20221204144918\\\/https:\\\/\\\/tomik23.github.io\\\/leaflet-examples\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-08-24 18:52:59&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-08-31 06:14:41&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-11-05 18:00:58&quot;,&quot;http_code&quot;:404}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-11-05T18:00:58+00:00&quot;,&quot;http_code&quot;:404},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:57,&quot;href&quot;:&quot;https:\\\/\\\/leafletjs.com\\\/plugins.html&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20260218171205\\\/https:\\\/\\\/leafletjs.com\\\/plugins.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-08-25 19:46:31&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-08-31 06:14:42&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-11-05 18:01:04&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-11-05T18:01:04+00:00&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:58,&quot;href&quot;:&quot;https:\\\/\\\/wpcarto.com\\\/que-es\\\/un-gis&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;}]\"><\/span>","protected":false},"excerpt":{"rendered":"<p>What is Leaflet? It&#8217;s an open-source library for building web mapping applications. Here&#8217;s how it works.<\/p>\n","protected":false},"author":2,"featured_media":60651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_ayudawp_aiss_exclude":false,"_ayudawp_aiss_exclude_summary":false,"_ayudawp_aiss_summary":"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. 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. 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.","_ayudawp_aiss_summary_provider":"extractive","_ayudawp_aiss_summary_hash":"78ec990ec40de24c26743a5c6cbf7b39999ca8c0","footnotes":""},"categories":[338],"tags":[],"class_list":["post-60646","post","type-post","status-publish","format-standard","has-post-thumbnail","category-visualizacion-datos"],"openstation_lock":null,"openstation_contributors":[],"openstation_attached_media":[60651,1654,1651,1653,1652,1661],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/posts\/60646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/comments?post=60646"}],"version-history":[{"count":1,"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/posts\/60646\/revisions"}],"predecessor-version":[{"id":60666,"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/posts\/60646\/revisions\/60666"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/media\/60651"}],"wp:attachment":[{"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/media?parent=60646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/categories?post=60646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jesusyesares.com\/en\/wp-json\/wp\/v2\/tags?post=60646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}