Book Image

OpenLayers 3: Beginner's Guide

By : Thomas Gratier, Paul Spencer, Erik Hazzard
Book Image

OpenLayers 3: Beginner's Guide

By: Thomas Gratier, Paul Spencer, Erik Hazzard

Overview of this book

<p>This book is a practical, hands-on guide that provides you with all the information you need to get started with mapping using the OpenLayers 3 library.</p> <p>The book starts off by showing you how to create a simple map. Through the course of the book, we will review each component needed to make a map in OpenLayers 3, and you will end up with a full-fledged web map application. You will learn the key role of each OpenLayers 3 component in making a map, and important mapping principles such as projections and layers. You will create your own data files and connect to backend servers for mapping. A key part of this book will also be dedicated to building a mapping application for mobile devices and its specific components.</p>
Table of Contents (22 chapters)
OpenLayers 3 Beginner's Guide
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Web mapping is the process of designing, implementing, generating, and delivering maps on the Web and its products. OpenLayers is a powerful, community-driven, open source, pure JavaScript web mapping library. With this, you can easily create your own web map mashup using a myriad of map backends. Interested in knowing more about OpenLayers?

This book is going to help you learn OpenLayers from scratch. OpenLayers 3 Beginner's Guide will walk you through the OpenLayers library in the easiest and most efficient way possible. The core components of OpenLayers are covered in detail, with examples, structured so that you can easily refer back to them later.

The book starts off by showing you how to create a simple map and introduces you to some basic JavaScript programming concepts and tools. You will also find useful resources to learn more about HTML and CSS. Through the course of this book, we will review each component needed to make a map in OpenLayers 3, and you will end with a full-fledged web map application.

You will learn some context to help you understand the key role of each OpenLayers 3 component in making a map. You will also learn important mapping principles such as projections and layers. Maps require sources of data as well; so, you will see how to create your own data files and connect to backend servers for mapping. A key part of this book will also be dedicated to building a mapping application for mobile devices and its specific components.

With OpenLayers 3 Beginner's Guide, you will learn how to create your own map applications independently, without being stuck at the first stage of learning. You will acquire the information you need to develop your skills and knowledge of the OpenLayers 3 library.

What this book covers

Chapter 1, Getting Started with OpenLayers will introduce you to OpenLayers 3 and will help you to learn some basic mapping principles. You will see how to get ready for development in OpenLayers and create your first map.

Chapter 2, Key Concepts in OpenLayers will introduce the main components of the OpenLayers library and illustrate how they are related. We will introduce some key concepts, including events and observable properties, and learn some basic debugging techniques.

Chapter 3, Charting the Map Class will describe two of the core components, the Map and View classes, in detail. We will learn about the properties, methods, and events of both classes and apply them in practical examples.

Chapter 4, Interacting with Raster Data Source will introduce the concept of layers and focus on raster layers. We will explain the difference between tiled and untiled layers and learn how to use OpenLayers to visualize any type of image, even non-geospatial ones.

Chapter 5, Using Vector Layers will introduce vector layers and the related source, format, feature, and geometry classes. We will learn the properties, methods, and events associated with each, and how to use them to load a variety of vector data into an OpenLayers map.

Chapter 6, Styling Vector Layers will expand on our knowledge of vector layers by learning how to apply both static and dynamic styles to them. Through hands-on examples, we'll learn how to modify styles interactively in response to user interaction.

Chapter 7, Wrapping Our Heads Around Projections will cover the basic concepts behind map projections and their characteristics. We will cover projection support within OpenLayers by introducing the Proj4js library and applying it to map, vector, and raster layers.

Chapter 8, Interacting with Your Map will dive into the concept of interactions and introduce the default interactions. After covering the available interactions in detail, we will finish with an example showing how to use interactions to draw a rectangle.

Chapter 9, Taking Control of Controls will demonstrate the use of controls and introduce the default controls provided by OpenLayers. We will also review each of the controls in more detail and learn how to make a custom control.

Chapter 10, OpenLayers Goes Mobile will teach us to take advantage of mobile-specific features such as Geolocation and Device Orientation. We will also learn how to debug mobile web applications and look at some mobile-specific browser features that can be useful for geospatial applications.

Chapter 11, Creating Web Map Apps will build a complete application from scratch and learn how to use the OpenLayers build system to create a production-ready application from our code.

Appendix A, Object-oriented Programming – Introduction and Concepts covers the main concepts of Object-oriented Programming (OOP). After, we will discover how to reuse them exploring the OpenLayers API documentation with OOP in mind.

Appendix B, More details on Closure Tools and Code Optimization Techniques will cover more details on Closure Tools and code optimization techniques. This appendix introduces Closure Tools, a set of tools that OpenLayers 3 library relies on. It provides an understanding on how to use the Closure Library and Closure Compiler with a focus on compressing OpenLayers code files. We will finish with a review of styles and syntax for good coding practices.

Appendix C, Squashing Bugs with Web Debuggers provides JavaScript beginners with an in-depth review of browser developer tools. We will review Chrome Developer Tools, additional extensions and finish with debugging tools in other browsers such as IE and Firefox.

What you need for this book

The main thing you'll need for this book is a computer and text editor. Your operating system will come with a text editor, any will do, but you will likely find that a text editor focused on developer needs will be most useful. There are many excellent developer-oriented text editors available for various operating systems, see https://github.com/showcases/text-editors and https://en.wikipedia.org/wiki/List_of_text_editors for some options. An Internet connection will be required to view the maps, and you'll also need a recent version of a modern web browser, such as Firefox, Google Chrome, Safari, Opera, or Internet Explorer (version 9 or higher).

For some advanced uses cases, you will also need Python, NodeJS, Java, and Git (a source code control management software). Installation instructions are provided for these additional tools.

No knowledge of Geographic Information Systems (GIS) is required, nor is extensive JavaScript experience. A basic understanding of JavaScript syntax and HTML/CSS will greatly aid in understanding the material, but is not required.

Who this book is for

This book is for anyone who has an interest in using maps on their website, from hobbyists to professional web developers. OpenLayers provides a powerful, but easy-to-use, pure JavaScript and HTML (no third-party plug-ins involved) toolkit to quickly make cross-browser web maps. A basic understanding of JavaScript will be helpful, but there is no prior knowledge required to use this book. If you've never worked with maps before, this book will introduce you to some common mapping topics and gently guide you through the OpenLayers 3 library. If you're an experienced application developer, this book will also serve as a reference to the core components of OpenLayers 3.

Sections

In this book, you will find several headings that appear frequently (Time for action, What just happened?, Pop quiz, and Have a go hero).

To give clear instructions on how to complete a procedure or task, we use these sections as follows:

Time for action – heading

  1. Action 1

  2. Action 2

  3. Action 3

Instructions often need some extra explanation to ensure they make sense, so they are followed with these sections:

What just happened?

This section explains the working of the tasks or instructions that you have just completed.

You will also find some other learning aids in the book, for example:

Pop quiz – heading

These are short multiple-choice questions intended to help you test your own understanding.

Have a go hero – heading

These are practical challenges that give you ideas to experiment with what you have learned.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, folder names, filenames, file extensions, pathnames, URLs, and user input are shown as follows: "You can name the folder whatever you like, but we'll refer to it as the sandbox folder."

A block of code is set as follows:

var map = new ol.Map({
  target: 'map',
  layers: [layer],
  view: view
});

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

var map = new ol.Map({
 target: 'map',
 view: view,
 layers: [layer],
 renderer: 'dom'
});

Any command-line input or output is written as follows:

weinre –boundHost <your ip address>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "You can do this in several ways but the easiest is to load your page into a web browser and look at the Network tab".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: http://www.packtpub.com/sites/default/files/downloads/B02497_ColorImages.pdf

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.