Leafletjs Tutorial
Leafletjs Tutorial
Leaflet
Audience
This tutorial is meant for all those readers who would like to learn Leaflet.js API. After
completing this tutorial, you would be able to integrate Leaflet.js JavaScript API on your
webpage.
Prerequisites
Before proceeding with this tutorial, you should be familiar with JavaScript and the concepts
of object-oriented programming.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent of
the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
Leaflet
Table of Contents
About the Tutorial ...................................................................................................................................... i
Audience ..................................................................................................................................................... i
Prerequisites ............................................................................................................................................... i
Marker Options........................................................................................................................................ 13
Polyline .................................................................................................................................................... 20
Polygon .................................................................................................................................................... 23
Rectangle ................................................................................................................................................. 25
Circle ........................................................................................................................................................ 28
Multi-Polyline .......................................................................................................................................... 31
ii
Leaflet
7. LEAFLET – OVERLAYS............................................................................................................ 47
8. LEAFLET – CONTROLS........................................................................................................... 49
Zoom ....................................................................................................................................................... 49
Attribution ............................................................................................................................................... 51
Scale ........................................................................................................................................................ 54
iii
Leaflet
1. Leaflet – Getting Started
What is Leaflet.js
Leaflet.js is an open-source library using which we can deploy simple, interactive, lightweight
web maps.
Leaflet JavaScript library allows you to use layers such as Tile layers, WMS, Markers,
Popups, Vector layers (polylines, polygons, circles, etc.), Image overlays and
GeoJSON.
You can interact with the Leaflet maps by dragging the map, zooming (by double click
or, wheel scroll), using keyboard, using event handling, and by dragging the markers.
Leaflet supports browsers such as Chrome, Firefox, Safari 5+, Opera 12+, IE 7–11 on
desktop and, browsers like Safari, Android, Chrome, Firefox for mobiles.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
...........
</body>
</html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
4
Leaflet
</head>
<head>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
</head>
Create a mapOptions object (it is created just like a literal) and set values for the options
center and zoom, where
center – As a value to this option, you need to pass a LatLng object specifying the
location where we want to center the map. (Just specify the latitude and longitude
values within [] braces)
zoom – As a value to this option, you need to pass an integer representing the zoom
level of the map, as shown below.
var mapOptions = {
center: [17.385044, 78.486671],
zoom: 10
}
5
Leaflet
As a parameter to this option, you need to pass a String variable representing the
DOM id or an instance of the <div> element. Here, the <div> element is an HTML
container to hold the map.
Create a Map object by passing the id of the <div> element and mapOptions object created
in the previous step.
map.addLayer(layer);
Example
The following example shows how to load an open street map of Hyderabad city with a zoom
value of 10.
<!DOCTYPE html>
<html>
<head>
<title>Leaflet sample</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
</head>
6
Leaflet
<body>
<div id = "map" style = "width: 900px; height: 580px"></div>
<script>
// Creating map options
var mapOptions = {
center: [17.385044, 78.486671],
zoom: 10
}
7
Leaflet
The following table lists the URL’s and their respective sample maps of the layers provided by
Openstreetmap.
8
Leaflet
http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png
Mapnik
9
Leaflet
http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png
Black
And
White
10
Leaflet
http://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png
DE
http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png
France
11
Leaflet
http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png
Hot
http://tile.openstreetmap.bzh/br/{z}/{x}/{y}.png
BZH
12
Leaflet
13