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 – executing code in the Console


We will do some basic JavaScript coding via the Chrome DevTools console; specifically, just calling a built-in the alert() function to display an alert.

  1. Open up Chrome. It doesn't matter at this point which website (if any) you go to, since we will be writing a standalone code.

  2. Open up Chrome DevTools by going through the menu. Go to the Console panel.

  3. Now, at the bottom of your screen, you'll see an area where you can enter code, designated by >. Clicking anywhere after this will allow you to enter the code.

  4. Type in the following code, and then hit Enter:

    alert('Packt publishing loves open source. You want to write? See http://authors.packtpub.com');
  5. You should see an alert box pop-up with the text 'Packt publishing loves open source. You want to write? See http://authors.packtpub.com' (or whatever string you passed into the alert function). After the code is executed, it will appear in the log above the input line:

What just happened?

We've just executed some JavaScript code without having to edit and save any files. Although we did a simple alert, we are really not limited by what we can do. Anything that we can save in a JavaScript file, we can also enter in the Console.

You'll also notice that the same code that we typed in appeared in the log area. We'll also get an error message if any errors occur in the code—go ahead and try it! Instead of typing alert('My alert'); type something like fakealert('Boom');. This will give you a reference error, since nowhere is the fakealert()function defined —alert(); on the contrary, it is a built-in function, so we can call it from any page.

That's pretty much all there is to it! The rest just builds on these principles. Let's go ahead and do just one more thing, something that is slightly more involved, before jumping into manipulating an OpenLayers page.