Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

web development

How to use Leaflet.js to integrate maps in JavaScript

Educative Answers Team

Leaflet.js is an open-source JavaScript library that helps you easily integrate maps in your web-application.

In this shot, we will look at how to set up a map and add different types of markers and shapes into a web application. The Leaflet library contains many more plugins that can be included in your project to add functionalities to your map. However, we won’t be talking about those additional functionalities in this shot.

The integration of the map is effortless. All you need to do is add the following two tags in your HTML file’s head tag. Make sure to write these two tags in the same order as shown below for it to work.

<link rel="stylesheet" href=""
<script src=""

Next, you need to create a div in your HTML file with a specific id. Make sure that this div has a defined height.

<div id="mapid"></div>

Now, we need to set up a map using the setView() function. This function takes two input parameters: the geographical coordinates and the zoom level.

After that, we need to add a tileLayer() and addTo() function to our map.

The code should look like:

var mymap ='mapid').setView([51.505, -0.09], 13);

L.tileLayer('http://{s}{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="">OpenStreetMap</a> contributors'

The following are codes used to add different types of markers and shapes to your map.

var marker = L.marker([51.5, -0.09]).addTo(mymap);

var circle =[51.508, -0.11], {
    color: 'red',
    fillColor: '#f03',
    fillOpacity: 0.5,
    radius: 500

var polygon = L.polygon([
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]

Below is a running example of a map integrated into a web-application using Leaflet.


web development
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring