Introduction

The idea of writing a book on Leaflet came about pretty much as soon as I first used it. It seemed so easy to use and I went looking for some books to help me through some of the more advanced topics. Since they are fairly scarce on the subject, I thought it would be a neat project to write one!

This decision has been supported mainly by the success of publishing my first book on d3.js called D3 Tips and Tricks. When I started writing D3 Tips and Tricks at the end of 2012 I had a desire to put out some documentation, but the method was a bit sketchy. I was lucky to stumble upon Leanpub while I was putting information together and it ticked all the boxes I was looking for in terms of being able to publish easily and distribute for free while providing the ability for people to get updates to the book when it gets improved. I tend to be a bit evangelical about Leanpub but the bottom line is that they provide a great service for people who want to publish a book in a flexible way.

Full disclosure: I am a simple user of this extraordinary framework and when I say simple, I really mean that there has been a lot of learning by trial-and-error (emphasis on the errors which were entirely mine). So to get from the point of having no knowledge on how to use Leaflet whatsoever to the point where I could begin to code up something to display data in a way I wanted, I had to capture the information as I went. The really cool thing about this sort of process is that it doesn’t need to occur all at once. You can start with no knowledge whatsoever (or pretty close) and by standing on the shoulders of others work, you can add building blocks to improve what you’re seeing and then change the blocks to adapt and improve. Another point to make is there there is a considerable amount of cross-over between this book and D3 Tips and Tricks. So when you see portions that you think “Hey, that looks like it was cut and pasted from his other book”, There’s a good chance that you’ll be right and for good reason. Each book has common features and each should be able to be read in isolation.

The point to take away from all of this is that any online map is just a collection of lots of blocks of code, each block designed to carry out a specific function. Pick the blocks you want and implement them. Leaflet makes this easy and in particular their support for plugins actively encourages it. I found it was much simpler to work on one thing (block) at a time, and this helped greatly to reduce the uncertainty factor when things didn’t work as anticipated. I’m not going to pretend that everything I’ve done while trying to build maps employs the most elegant or efficient mechanism, but in the end, if it all works on the screen, I walk away happy :-). That’s not to say I have deliberately ignored any best practices – I just never knew what they were. Likewise, wherever possible, I have tried to make things as extensible as possible. You will find that I have typically eschewed a simple “Use this code” approach for more of a story telling exercise. This means that some explanations are longer and more flowery than might be to everyone’s liking, but there you go, try to be brave :-).

I’m sure most authors try to be as accessible as possible. I’d like to do the same, but be warned… There’s a good chance that if you ask me a technical question I may not know the answer. So please be gentle with your emails :-).

Email: d3noobmail+leaflet@gmail.com