Skip to content
Edward Mac Gillavry edited this page Aug 9, 2017 · 40 revisions

In Leaflet step 1 we will set up the basics to show a standard background map with Leaflet.

Setting up the basics

▶️ Open your text editor.

▶️ Create an empty file to make your basic HTML page. Copy the following into your file:

<!DOCTYPE html>
  <html>
    <head>
      <title> My title </title>
    </head>
  <body>
    <H1>Example</H1>
  </body>
</html>

▶️ Save the file in yourDirectory and call the file index.html.

▶️ Change the title to “My first Leaflet map".

▶️ Place a <div> in the <body>

<div id="map"></div>

The <div> is a kind of frame where our map will come! This frame does not contain anything yet. We will have to give it some dimensions in order to show content. We will specify the width and height with CSS, so the <div> can be seen. Later, we will give content to the <div> with JavaScript. To be able to do that, we will need its id to be able to reference it.

▶️ Open a new file and save this as main.css in yourDirectory.

With CSS we will style our <div> object. In the main.css we will reference to the <div> with a "#map" selector which indicates a object with the id="map" in the HTML. Now set the rule height(and optional width).

▶️ Copy this into your CSS file:

#map { 
  height: 500px; 
  width:100%;
} 

▶️ Change the amount of pixels and/or percentage to the map size you prefer.

To link our CSS file with our HTML objects we will have to put a link in our HTML file.

▶️ Go back to your index.html file and put the link to your CSS file in the <head>.

<link rel="stylesheet" href="main.css"/>

Now we will add the JavaScrip Library Leaflet.js.

▶️ Go to http://leafletjs.com/download.html to use the Hosted Version of Leaflet.

▶️ Scroll down and copy the link of the newest leaflet library release:

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

▶️ Place the leaflet.css Link in the <head> of your html file.

▶️ Place the leaflet.js script library in the <body>.

JS JavaScript libraries are often placed in the head. However, it is best to place them as far as possible to the bottom of the body. This results in a faster page load because parsing the script doesn't block the loading of the rest of the page.

Now you will have:

<!doctype html>
<html>
  <head>
    <title>My first Leaflet map</title>  
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
    <link rel="stylesheet" href="main.css"/>
  </head>
  <body>
    <H1>example</H1>
    <div id="map"></div>
    <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
  </body>
</html>

The basics are done!

Adding a base layer

For a real map you need a base layer. This is the reference of your map application and consists of multiple PNG images, so-called 'tiles', which are quick to load!

▶️ Put the following script in the body and save your index.html file.

<script>
  //initialize the map         
  var map = L.map('map').setView([42.3600825, -71.0588801], 12);
  
  //Create baselayer - tiles         
  var backgroundMap = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',{
    attribution: '<a href="http://openstreetmap.org">OpenStreetMap</a>contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
    maxZoom: 19
    }
  );
  
  backgroundMap.addTo(map);
</script>

ℹ️ var map = L.map("map") initializes the map variable and links it to our <div id="map"></div>. This will link the map content to the <div> content.

ℹ️ setView() centres the map ([latitude, longitude], zoom level). The projection is Google Mercator.

ℹ️ Next we add our base-layer tiles. L.tileLayer('http://...') graps one from the internet.

ℹ️ backgroundMap.addTo() adds the tile layer to the map.

Now you have made your first web map!

▶️ Open your index.html file in your browser!

Do you see your map? Great!

‼️ you do not see a map?

Open the debugger

  • Click with your right mouse button, choose : Inspect Element
  • Or Press F12

The debugger shows you the content of your page. But also logs any errors or comments! Go to the tab Web Console to see if it reports anything useful for you.

Do you get:

ReferenceError: L is not defined

Then shuffle around the order in your script. Put the leaflet.js script above your custom script!

Customizing

Practice with different tiles, coordinates and zoom levels to make your own base map.

▶️ Try to zoom your map to the place where you live or work!

▶️ Looking for a specific place to centre on? Find your coordinates here: mapcoordinates.net

▶️ There are many different tile providers! Just copy their example code, replacing your own code starting from L.tileLayer (... ); Try out some different styles.

Another free tile provider is maps.stamen.com. They even provide 3 varieties:

Or have a look at this Leaflet provider preview application.

➡️ Continue to Leaflet Step 2