Book Image

OpenLayers 3: Beginner's Guide

By : Thomas Gratier, Paul Spencer
Book Image

OpenLayers 3: Beginner's Guide

By: Thomas Gratier, Paul Spencer

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

Time for action – downloading OpenLayers


Let's download the OpenLayers library. After you're done, you should have the OpenLayers library files set up on your computer. Perform the following steps:

  1. Go to the OpenLayers website (http://openlayers.org), go to the Download part to follow the link and download the v3.0.0.zip version of the OpenLayers v3.0.0. It's the green button on the bottom left of the Github release page for v3.0.0.

  2. Extract the file you just downloaded. When you extract it, you'll end up with a folder called v3.0.0 (or whatever your version is).

  3. Open the v3.0.0 folder. Once inside, you'll see a lot of folders, but the ones we are concerned with right now are the folders named build and css, as seen in the following screenshot:

  4. Create an ol3 folder within an assets folder contained within a new folder ol3_samples in your home directory (or C:\ on Windows). Copy the previous build folder from v3.0.0 and rename it as js into the new ol3 directory and also copy the css folder into the same ol3 folder.

  5. Create a new folder called sandbox into the ol3_samples directory. You can name the folder whatever you like, but we'll refer to it as the sandbox folder. Your new folder structure should look similar to the following screenshot:

What just happened?

We just installed OpenLayers 3 by copying over different pre-built, compressed JavaScript files containing the entire OpenLayers 3 library code (from the build directory) and the associated assets (style sheets). To use OpenLayers, you'll need at a minimum, the ol.js file (the other .js files will be needed during the book but are not always required) and the ol.css file. For best practice, we already gave you some tips to structure the code like separate assets, such as css and js, or separate library code from your own code.

If you open the ol.js file, you'll notice it is nearly unreadable. This is because this is a minified and obfuscated version, which basically means extra white space and unnecessary characters have been stripped out to cut down on the file size and some variables have been shortened whenever possible. While it is no longer readable, it is a bit smaller and thus requires less time to download. If you want to look at the uncompressed source code, you can view it by looking in the ol-debug.js file within the js folder of the ol3 directory.

You can, as we'll see in the last chapter of this book, build your own custom configurations of the library, including only the things you need. But for now, we'll just use the entire library. Now that we have our OpenLayers library files ready to use, let's make use of them!

Making our first map

The process for creating a map with OpenLayers requires, at a minimum, the following things:

  • Include the OpenLayers library files

  • Creating an HTML element that the map will appear in

  • Creating a layer object from a ol.layer.* class

  • Creating a map object from the ol.Map class by adding a layer

  • Creating a view from the ol.View class to set for the Map class (defining the area where the map will initially be displayed)

Now, we're finally ready to create our first map!