Getting started with leaflet

Implementing Leaflet.js with HTML and JavaScript

To use Leaflet, load its stylesheet and JavaScript file to your page:

<link rel="stylesheet" href="/css/leaflet.css" />
<script src="/js/leaflet.js"></script>

These resources can be downloaded from a variety of locations such as Leaflet’s homepage or the Leaflet Github repository, or you can directly use CDN as,

<link rel="stylesheet" href="[email protected]/dist/leaflet.css" />
<script src="[email protected]/dist/leaflet.js" ></script>

You need a container for your map. It is common for developers to use a div with an id of “map” for this. Make sure to give your map div a height as well or the map might not show up.

<div id="map" style="height: 200px;"></div>

Initializing the map is done by creating a map object using the method. In the below example, a latitude and longitude are set as a default with a default zoom level of 13.

var map ='map').setView([42.35, -71.08], 13);

This creates our empty map, we should now provide a tile layer to act as our base map. A tilelayer is a service that provides map images in tiles, small images that are accessed by x, y and z parameters in a particular order (see below).

A tile layer URL might look like this, where {s}, {z}, {x} and {y} are placeholders that Leaflet will automatically change during operation:


We can now add our tilelayer, along with attribution info and maximum possible zoom level, and add it to the map:

  attribution: 'Tiles by <a href="">MAPC</a>, Data by <a href="">MassGIS</a>',
  maxZoom: 17,
  minZoom: 5

Note: Map initialization and loading the tile layer need to occur after the inclusion of leaflet.js and the map container div element.

Leaflet Quick Start

<!DOCTYPE html>

    <title>My Leaflet Map</title>
    <link rel="stylesheet" href="//[email protected]/dist/leaflet.css" />
    <style type="text/css">
      #map { 
        height: 500px; 

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

    <script src="//[email protected]/dist/leaflet.js"></script>    
      var map ='map').setView([51.505, -0.09], 13);

      L.tileLayer('http://{s}{z}/{x}/{y}.png', {
          attribution: '&copy; <a href="">OpenStreetMap</a> contributors'
      L.marker([51.5, -0.09]).addTo(map)
          .bindPopup('A pretty CSS3 popup.<br> Easily customizable.')