Open In App

Using Leaflet.js to show maps in a webpage

Maps are incredibly helpful for showing locations on a website. Use cases of maps include showing the location of an office address, which is a better option than just showing an image or a text address. It can also be used for marking points of interest in a tourist location so that the visitor can plan by looking at all the nearby areas.

Leaflet.js is a JavaScript library that makes it extremely easy to show maps on a webpage and interact with it. This guide would attempt to give a simple introduction to using Leaflet.js to display map and display the required area on the map.



HTML portion

We will start by declaring the HTML needed to display the map. Using leaflet.js requires us to import a CSS file for the map styling and the JavaScript file for the functionality of the map. The latest version of both can be found at ” https://leafletjs.com/download.html ”

We will start by defining the area where the map has to be displayed. We first create a div with an id of “map” so that it could be accessed by the script. We will then define the height and width of the map object. Otherwise, the map would not display. We can specify any dimension required here.




<!DOCTYPE html>
<html>
  
<head>
  <title>Leaflet.js Guide</title>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- Get the leaflet CSS file -->
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
integrity=
"sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
    crossorigin="" />
</head>
  
<body>
  <h1>My Leafletjs Map</h1>
  <!-- Specify the map and it's dimensions -->
  <div id="map" style="width: 960px; height: 500px"></div>
  
  <!-- Get the leaflet JavaScript file -->
    integrity=
"sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
    crossorigin=""></script>
</body>
  
</html>

Mobile Version
The map can be shown full screen on a mobile device, like a web application. This is possible by making the body element and the div containing the map fill both the height and width of the screen completely. This can be done by giving both these values as 100% or 100 view units.



JavaScript portion
We will be writing the JavaScript portion of the implementation here.




// Ask for current location and navigate to that area
map.locate({setView: true, maxZoom: 16});

Showing markers on the map

Leaflet can be used to mark points on the map. This is done using the marker() method. It accepts the coordinates where the marker would be shown.




// Show a market at the position of the Eiffel Tower
let eiffelMarker = L.marker([48.8584, 2.2945]).addTo(map);
 
// Bind popup to the marker with a popup
eiffelMarker.bindPopup("Eiffel Tower").openPopup();

We can also add a popup to the marker that would show the name of the marker by using the bindPopup() method. This popup can be opened by default using the openPopup() method.

Hence, we have successfully created a map using the Leaflet.js library and learnt to view a specific position of the world.

Final Output:

Complete Code:




<!DOCTYPE html>
<html>
<head>
<title>Leaflet.js Guide</title>
<meta charset="utf-8" />
<meta name="viewport" 
      content="width=device-width, initial-scale=1.0">
<!-- Get the leaflet CSS file -->
<link rel="stylesheet" href=
    integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
    crossorigin="" />
</head>
<body>
<h1>My Leafletjs Map</h1>
<!-- Specify the map and it's dimensions -->
<div id="map" style="width: 960px; height: 500px"></div>
  
<!-- Get the leaflet JavaScript file -->
    integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
    crossorigin=""></script>
<script>
    // Initialize the map
    const map = L.map('map')
  
    // Get the tile layer from OpenStreetMaps
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  
    // Specify the maximum zoom of the map
    maxZoom: 19,
  
    // Set the attribution for OpenStreetMaps
    attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
  
    // Set the view of the map
    // with the latitude, longitude and the zoom value
    map.setView([48.8584, 2.2945], 16);
      
    // Set the map view to the user's location
    // Uncomment below to set map according to user location
    // map.locate({setView: true, maxZoom: 16});
  
    // Show a market at the position of the Eiffel Tower
    let eiffelMarker = L.marker([48.8584, 2.2945]).addTo(map);
  
    // Bind popup to the marker with a popup
    eiffelMarker.bindPopup("Eiffel Tower").openPopup();
</script>
</body>
</html>

Output:


Article Tags :