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 – using Closure Linter to fix JavaScript


We will retrieve one file from the previous OpenLayers library Version 2.12.

For this, download the file Map.js from https://raw.github.com/openlayers/openlayers/release-2.12/lib/OpenLayers/Map.js.

Type the following command:

gjslint Map.js

You will get 605 errors with messages like below:

 ----- FILE  :  /home/thomas/Map.js -----
Line 1,  E:0001: Extra space at end of line
Line 7,  E:0200: Invalid JsDoc tag: requires
Line 266, E:0131: Single-quoted string preferred over double-quoted string.
Line 982, E:0120: Binary operator should go on previous line "+"
Line 1106, E:0002: Missing space before "("
Line 1131, E:0002: Missing space after "function"
Line 1089, E:0110: Line too long (83 characters).
Found 605 errors, including 0 new errors, in 1 files (0 files OK).

Some of the errors reported by GJsLint may be auto-fixable using the fixjsstyle script. Please double-check any changes it makes and report any bugs. The script can be run by executing:

fixjsstyle Map.js

So now, make a copy of the Map.js file to save it in an eventual case where the fixer would suffer from a bug. Then, execute the following recommended line:

fixjsstyle Map.js 

You will see:

fixjsstyle Map.js Fixed 582 errors in /home/thomas/Map.js
WARNING: Line 1089 of /home/thomas/Map.js is now longer than 80 characters.

Relaunch the gjslint again:

gjslint Map.js

You now have only 23 errors. Really good compared to the previous 605 errors. You can ignore, in this case, errors like:

  • E:0200: Invalid JsDoc tag: This requires OpenLayers 2 code; do not use Closure so requires tag is not available

  • E:0110: Line too long (n characters): Sometimes, you need more than 80 lines in your code so ignore it, in particular for an existing code like the one used

Be aware, that you can use options to comply more or less to the language but can't customize everything. You are also encouraged to play with the options. To discover them, execute the following command:

gjslint -–help

For example, try the --strict options that check also your file indentation and you get 1867 errors or use the recursive option -r to apply check on a every JavaScript files in a directory.

We use a practical approach, but to understand the adopted code style, we invite you to go to the Google JavaScript Guide at http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml, in particular, if you don't want to fix all your code at the end of your project.

We gave you some information about the raw use of the Linter, but like for other cases, you already can check your code in the default OpenLayers development environment. Just launch ./build.py lint or build.cmd lint, depending on your OS.

Although syntax and styles are considered as good practices. Good practices are not rules; you choose to respect them more or less. Never forget that consistency is important, particularly when you need to share your code with other people. We reviewed how to do it with Closure Linter but this is not the only way. Let's broaden your vision.

Coding styles alternatives and tools

It is always easier to use the same tools for all your JavaScript, but we advise that you be curious about other JavaScript coding styles for improving your knowledge and choose the coding style that fits you.

We recommend that you not only look at https://github.com/rwldrn/idiomatic.js but also at big library projects.

For example, check out the following links:

To enforce your own coding style and share it, the most valuables tools are as follows:

Although those tools are interesting, the errors you receive when using them on your files are not always clear. So, we advise that you look at http://jslinterrors.com.