Leaflet Tips and Tricks Sample PDF
Leaflet Tips and Tricks Sample PDF
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools
and many iterations to get reader feedback, pivot until you have the right book and build
traction once you do.
2013 - 2014 Malcolm Maclean
Contents
Start With a Simple Map . . . . . . . . . . . . . . .
HTML . . . . . . . . . . . . . . . . . . . . . . . .
JavaScript . . . . . . . . . . . . . . . . . . . . . .
Declaring the starting parameters for the map
Declaring the source for the map tiles . . . .
And theres your map! . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
3
5
5
6
7
The following is the full code listing of the file simple-map.html that we will be examining;
<!DOCTYPE html>
<html>
<head>
<title>Simple Leaflet Map</title>
<meta charset="utf-8" />
<link
rel="stylesheet"
href="http://cdn.leafletjs.com/leaflet-0.7/leaflet.css"
/>
</head>
<body>
<div id="map" style="width: 600px; height: 400px"></div>
<script
src="http://cdn.leafletjs.com/leaflet-0.7/leaflet.js">
</script>
<script>
var map = L.map('map').setView([-41.2858, 174.78682], 14);
mapLink =
'<a href="http://openstreetmap.org">OpenStreetMap</a>';
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © ' + mapLink,
maxZoom: 18,
}).addTo(map);
</script>
</body>
</html>
The output that it will produce on a web page will look like this;
You can access an electronic version from bl.ocks.org, GitHub there is a copy in the appendices
and there is a copy of all the files that appear in the book that can be downloaded (in a zip file)
when you download the book from Leanpub.
You can pan the map by pressing an holding the left mouse button and waving it about and you
can zoom in and out of the map by either clicking on the + or - buttons or using a scroll wheel.
As you can tell from the code listing, theres not much there, so what you can take from that is
that there is a significant amount of cleverness going on inside leaflet.js.
Once weve finished explaining the different parts of the code, well start looking at what we
need to add in and adjust so that we can incorporate other useful functions.
The two parts to the code that we in our example to consider are;
HTML
JavaScript
Technically we could also consider some CSS, but the way that this example loads our
styles is configured for simplicity, not flexibility as we will check out soon.
http://bl.ocks.org/d3noob/7644920
https://gist.github.com/d3noob/7644920
https://leanpub.com/leaflet-tips-and-tricks
HTML
Heres the HTML portions of the code;
<!DOCTYPE html>
<html>
<head>
<title>Simple Leaflet Map</title>
<meta charset="utf-8" />
<link
rel="stylesheet"
href="http://cdn.leafletjs.com/leaflet-0.7/leaflet.css"
/>
</head>
<body>
<div id="map" style="width: 600px; height: 400px"></div>
<script
src="http://cdn.leafletjs.com/leaflet-0.7/leaflet.js">
</script>
<script>
The D3 JavaScript code is here
</script>
</body>
</html>
Compare it with the full file. It obviously comprises most of the code for the web page which
should tell you two things.
1. The JavaScript portion that draws the map is really small (and therefore it should hopefully
be pretty easy to understand (Spoiler alert: It is)).
2. The heavy lifting done by leaflet.js must be pretty clever.
There are plenty of good options for adding additional HTML stuff into this very basic part for
the file, but for what were going to be doing, we really dont need to make things too difficult.
HTML works its magic by enclosing instructions for the browser in specific tags A tag will
begin with a descriptive word inside the greater-than and less-than signs and end with the same
thing except the closing tag includes a backslash. There are also tags that allow including the
contents inside a single tag that is closed off with a backslash. This appears to be connected with
XHTML and sometimes with browser variants. To be perfectly honest I have trouble keeping up
with it all.
For example, the title of our web page is Simple Leaflet Map. This is enclosed by an opening
title tag (<title>) and a closing title tag (</title>). So that the whole thing looks like this;
<title>Simple Leaflet Map</title>.
Each tag has a function and the browser will know how to execute the contents of a tag by virtue
of standards for web browsers that builders of browsers implement. Tags can also be nested so
hierarchies can be established to create complex instructions.
For example contained within our <head> tags;
<head>
<title>Simple Leaflet Map</title>
<meta charset="utf-8" />
<link
rel="stylesheet"
href="http://cdn.leafletjs.com/leaflet-0.7/leaflet.css"
/>
</head>
A <div> tag defines a division or a section in an HTML document. As well as creating a section
we use an ID selector (id="map") as a way of naming the division as an anchor point on an
HTML page. ID selectors can be defined as a unique identifier to an element. Which means
that we can name a position on our web page and then we can assign our map to that position.
Lastly for our division, we specify the size with a style declaration of width: 600px; height:
400px.
The second is
<script
src="http://cdn.leafletjs.com/leaflet-0.7/leaflet.js">
</script>
This tells the browser that we are loading a script (hence the <script> tags) and in this case,
the script is leaflet.js which we get from http://cdn.leafletjs.com/leaflet-0.7/. This is
using the same idea as we used for the css file. We are loading the leaflet.js script from the
original authorised location when the file is loaded. We could have loaded this from a local file
(stored in the js directory for instance), but again, in order to make the sample file a bit more
transportable (we dont need to ship it with a separate js file) we load it from an external source.
JavaScript
The JavaScript portion of our code looks like this;
var map = L.map('map').setView([-41.2858, 174.78682], 14);
mapLink =
'<a href="http://openstreetmap.org">OpenStreetMap</a>';
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © ' + mapLink,
maxZoom: 18,
}).addTo(map);
Firstly I have to apologise since it should look slightly simpler, but in order to make the code
appear ideal for the book I have made it slightly less simple (but only slightly). But the main
thing that we can take away from this piece of code is that theres not very much there. The
process of presenting a map on a web page has been rendered to a very few lines of code.
The heart of the process in the code above consists of two actions;
1. Declare the starting parameters for the map.
2. Declare the source of the map tiles.
Where the map object is instantiated given an appropriate div element or its id and (optional)
map state options.
In our example it is declared as a variable using var map =. The id is set as map (recall that we
declared a div with the id map in our HTML section).
The .setView method is used specifically to modify our map state. In this case setView sets the
geographical centre of the map ([-41.2858, 174.78682] is the latitude and longitude of the
centre point) and the zoom level (14).
If this all seems a bit rushed in terms of an explanation, never fear as we will go over as many
mysteries of maps as possible in other sections of the book.
that credit (and copyright acknowledgement) is provided to the tile provider as is reasonable. In
the example used here we place the words Map Data and then a copyright symbol (which is
produced by the text ©) followed by the variable mapLink which is declared slightly earlier
in the JavaScript code and is set to <a href="http://openstreetmap.org">OpenStreetMap</a>
which provides the text OpenStreetMap and a link to openstreetmap.org. The end result looks
like this;
Map Attribution