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="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[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 Leaflet.map(mapContainerId)
method. In the below example, a latitude and longitude are set as a default with a default zoom level of 13.
var map = L.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:
"http://{s}.domain.com/foo/{z}/{x}/{y}.png"
We can now add our tilelayer, along with attribution info and maximum possible zoom level, and add it to the map:
L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
{
attribution: 'Tiles by <a href="http://mapc.org">MAPC</a>, Data by <a href="http://mass.gov/mgis">MassGIS</a>',
maxZoom: 17,
minZoom: 5
}).addTo(map);
Note: Map initialization and loading the tile layer need to occur after the inclusion of leaflet.js
and the map container div
element.