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

Time for action – creating object literals


We will introduce object literals and get acclimated with how to manipulate them now, so that we can better work with the OpenLayers code:

  1. Open up Chrome and the DevTools Console panel—again, it doesn't matter right now what page you're on.

  2. Type in the following code, and then execute it by pressing Enter:

    var my_parameters = {zoom: false, attribution: false, otherControl: 'myslider'};
    console.log(my_parameters);
  3. The preceding code should display, in the console log area, something similar to {zoom: false, attribution: false; otherControl: 'mySlider'}. In this case, the object is not complex but when the object is more complex, you will see an arrow before the object, and if you click on it, it will show all the information about the object you just created.

  4. Click on the Console panel to get back to it. In the input box, add the following code to the existing code and execute it:

    console.log(my_parameters.otherControl);
  5. You should see a line of output in the console area containing the string mySlider:

What just happened?

We just created what is called in JavaScript an anonymous object, or object literal. We previously discussed that objects are created from classes, in Appendix A, Object-oriented Programming – Introduction and Concepts and in JavaScript, we need to use the new keyword to instantiate an object from a function. However, here, you can see that there is no new keyword!

Object literals

The key concept here is that we are just creating a single object that does not derive from a class. Since object literals (anonymous objects) do not derive from a class, it is, essentially, an empty object. It contains only what we explicitly defined. The value associated with a key can be almost anything—a string, integer, function, array, or even another object literal.

We encountered object literals in Chapter 1, Getting Started with OpenLayers —they were the {key:value} pairs used to define the parameters and options of our layer and objects. The only difference is that we did not assign a variable to them; we simply passed them in when we created our layer object.

Object literals are extremely useful for a variety of tasks, and it is a great way to package information in an easy-to-use form. They are in the form of {key:value, key2:value2}. We can access any property of an object literal by using dot notation, in the form of my_object_literal.key. The key, like before, is the key part of the key:value pair. In the preceding code, we call console.log(my_parameters.otherControl); and the value of the key opacity is displayed in the console's log area. You will also encounter an alternative notation and brackets notation, to retrieve the same information. This form is quite useful because you can concatenate values keys in your object.

Using console.log(my_parameters['other' + 'Cont' + 'rol']); will do the same as the previous console.log call. It allows to loop on objects with a key that can change whereas the dot notation does not allow this.

Note

console.log(): The Chrome DevTools function console.log() is a function that will, essentially, display what you pass into it in the console log. You can pass in variables, strings, objects, anything, and it will display in the log. It comes in handy often, so getting familiar with it is a good idea.

We use object literals frequently when making our maps—so if they don't make much sense yet, don't worry. The basic idea to grasp, and the primary way we will use them, is that they are essentially key:value pairs. Before we end this chapter, let's do a quick example where we interact with an OpenLayers map using the Console panel.