Table of Contents for
OpenLayers 3 : Beginner's Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition OpenLayers 3 : Beginner's Guide by Erik Hazzard Published by Packt Publishing, 2015
  1. Cover
  2. Table of Contents
  3. OpenLayers 3 Beginner's Guide
  4. OpenLayers 3 Beginner's Guide
  5. Credits
  6. About the Authors
  7. About the Reviewers
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Sections
  13. Time for action – heading
  14. Conventions
  15. Reader feedback
  16. Customer support
  17. 1. Getting Started with OpenLayers
  18. Advantages of using OpenLayers
  19. What, technically, is OpenLayers?
  20. Anatomy of a web mapping application
  21. Connecting to Google, Bing Maps, and other mapping APIs
  22. Time for action – downloading OpenLayers
  23. Time for action – creating your first map
  24. Where to go for help
  25. OpenLayers issues
  26. OpenLayers source code repository
  27. Getting live news from RSS and social networks
  28. Summary
  29. 2. Key Concepts in OpenLayers
  30. Time for action – creating a map
  31. Time for action – using the JavaScript console
  32. Time for action – overlaying information
  33. OpenLayers' super classes
  34. Key-Value Observing with the Object class
  35. Time for action – using bindTo
  36. Working with collections
  37. Summary
  38. 3. Charting the Map Class
  39. Time for action – creating a map
  40. Map renderers
  41. Time for action – rendering a masterpiece
  42. Map properties
  43. Time for action – target practice
  44. Map methods
  45. Time for action – creating animated maps
  46. Events
  47. Views
  48. Time for action – linking two views
  49. Summary
  50. 4. Interacting with Raster Data Source
  51. Layers in OpenLayers 3
  52. Common operations on layers
  53. Time for action – changing layer properties
  54. Tiled versus untiled layers
  55. Types of raster sources
  56. Tiled images' layers and their sources
  57. Time for action – creating a Stamen layer
  58. Time for action – creating a Bing Maps layer
  59. Time for action – creating tiles and adding Zoomify layer
  60. Image layers and their sources
  61. Using Spherical Mercator raster data with other layers
  62. Time For action – playing with various sources and layers together
  63. Time For action – applying Zoomify sample knowledge to a single raw image
  64. Summary
  65. 5. Using Vector Layers
  66. Time for action – creating a vector layer
  67. How the vector layer works
  68. The vector layer class
  69. Vector sources
  70. Time for action – using the cluster source
  71. Time for action – creating a loader function
  72. Time for action – working with the TileVector source
  73. Time for action – a drag and drop viewer for vector files
  74. Features and geometries
  75. Time for action – geometries in action
  76. Time for action – interacting with features
  77. Summary
  78. 6. Styling Vector Layers
  79. Time for action – basic styling
  80. The style class
  81. Time for action – using the icon style
  82. Have a go hero – using the circle style
  83. Multiple styles
  84. Time for action – using multiple styles
  85. Style functions
  86. Time for action – using properties to style features
  87. Interactive styles
  88. Time for action – creating interactive styles
  89. Summary
  90. 7. Wrapping Our Heads Around Projections
  91. Time for action – using different projection codes
  92. Time for action – determining coordinates
  93. OpenLayers projection class
  94. Transforming coordinates
  95. Time for action – coordinate transforms
  96. Time for action – setting up Proj4js.org
  97. Time for action – reprojecting extent
  98. Time for action – using custom projection with WMS sources
  99. Time for action – reprojecting geometries in vector layers
  100. Summary
  101. 8. Interacting with Your Map
  102. Time for action – converting your local or national authorities data into web mapping formats
  103. Time for action – testing the use cases for ol.interaction.Select
  104. Time for action – more options with ol.interaction.Select
  105. Introducing methods to get information from your map
  106. Time for action – understanding the forEachFeatureAtPixel method
  107. Time for action – understanding the getGetFeatureInfoUrl method
  108. Adding a pop-up on your map
  109. Time for action – introducing ol.Overlay with a static example
  110. Time for action – using ol.Overlay dynamically with layers information
  111. Time for action – using ol.interaction.Draw to share new information on the Web
  112. Time for action – using ol.interaction.Modify to update drawing
  113. Understanding interactions and their architecture
  114. Time for action – configuring default interactions
  115. Discovering the other interactions
  116. Time for action – using ol.interaction.DragRotateAndZoom
  117. Time for action – making rectangle export to GeoJSON with ol.interaction.DragBox
  118. Summary
  119. 9. Taking Control of Controls
  120. Adding controls to your map
  121. Time for action – starting with the default controls
  122. Controls overview
  123. Time for action – changing the default attribution styles
  124. Time for action – finding your mouse position
  125. Time for action – configuring ZoomToExtent and manipulate controls
  126. Creating a custom control
  127. Time for action – extending ol.control.Control to make your own control
  128. Summary
  129. 10. OpenLayers Goes Mobile
  130. Using a web server
  131. Time for action – go mobile!
  132. The Geolocation class
  133. Time for action – location, location, location
  134. The DeviceOrientation class
  135. Time for action – a sense of direction
  136. Debugging mobile web applications
  137. Debugging on iOS
  138. Debugging on Android
  139. Going offline
  140. Time for action – MANIFEST destiny
  141. Going native with web applications
  142. Time for action – track me
  143. Summary
  144. 11. Creating Web Map Apps
  145. Using geospatial data from Flickr
  146. Time for action – getting Flickr data
  147. A simple application
  148. Time for Action – adding data to your map
  149. Styling the features
  150. Time for action – creating a style function
  151. Creating a thumbnail style
  152. Time for action – switching to JSON data
  153. Time for action – creating a thumbnail style
  154. Turning our example into an application
  155. Time for action – adding the select interaction
  156. Time for action – handling selection events
  157. Time for action – displaying photo information
  158. Using real time data
  159. Time for action – getting dynamic data
  160. Wrapping up the application
  161. Time for action – adding dynamic tags to your map
  162. Deploying an application
  163. Creating custom builds
  164. Creating a combined build
  165. Time for action – creating a combined build
  166. Creating a separate build
  167. Time for action – creating a separate build
  168. Summary
  169. A. Object-oriented Programming – Introduction and Concepts
  170. Going further
  171. B. More details on Closure Tools and Code Optimization Techniques
  172. Introducing Closure Library, yet another JavaScript library
  173. Time for action – first steps with Closure Library
  174. Making custom build for optimizing performance
  175. Time for action – playing with Closure Compiler
  176. Applying your knowledge to the OpenLayers case
  177. Time for action - running official examples with the internal OpenLayers toolkit
  178. Time for action - building your custom OpenLayers library
  179. Syntax and styles
  180. Time for action – using Closure Linter to fix JavaScript
  181. Summary
  182. C. Squashing Bugs with Web Debuggers
  183. Time for action – opening Chrome Developer Tools
  184. Explaining Chrome Developer debugging controls
  185. Time for action – using DOM manipulation with OpenStreetMap map images
  186. Time for action – using breakpoints to explore your code
  187. Time for action – playing with zoom button and map copyrights
  188. Using the Console panel
  189. Time for action – executing code in the Console
  190. Time for action – creating object literals
  191. Time for action – interacting with a map
  192. Improving Chrome and Developer Tools with extensions
  193. Debugging in other browsers
  194. Summary
  195. D. Pop Quiz Answers
  196. Chapter 5, Using Vector Layers
  197. Chapter 7, Wrapping Our Heads Around Projections
  198. Chapter 8, Interacting with Your Map
  199. Chapter 9, Taking Control of Controls
  200. Chapter 10, OpenLayers Goes Mobile
  201. Appendix B, More details on Closure Tools and Code Optimization Techniques
  202. Appendix C, Squashing Bugs with Web Debuggers
  203. Index

Creating custom builds

OpenLayers provides an easy way to create a custom build of the library to include in your page that contains only the things you need. Throughout this book, we've been including a file called ol.js (or ol-debug.js), a file that contains all the functionality (classes, functions, and so on) that OpenLayers provides. So far, this has been great—we've been developing up to this point, so we want to be sure that we have access to all the classes that we may use.

Benefits of serving small files

Well, the ol.js file we've been using is around 391 KB, and the debug version is 3.5 MB! Both are quite large files to load, especially since they contain only JavaScript code. Even though large file sizes aren't as much of an issue as they were years ago, when everyone was on slower connections, a large JavaScript file in a production environment is something we want to avoid if at all possible. This is especially true for mobile environments, where users pay for data plans and data speeds can be highly variable.

We want all our files in a production environment to be as small as they can absolutely be. This will allow users to download the files faster (since there is less to download), which decreases the page's loading time (saves on bandwidth expenses). Faster page loads (even if the speed is only perceived) will greatly enhance the user's experience. Fortunately, we can greatly reduce the size of the OpenLayers library file with a build tool provided by OpenLayers.

Two approaches to optimization

To optimize your application code for production, there are two approaches you can take:

  • Combined: The build tool combines all your JavaScript files into a single file containing both the OpenLayers 3 code and your application code.
  • Separate: The build tool creates a custom version of the OpenLayers library code with only the parts you need. Your application code is kept separate.

The following table shows the advantages and disadvantages of each approach:

Approach

Advantages

Disadvantages

Combined compilation

  • Maximum optimization of the compiled JavaScript code.
  • More complicated to annotate your code with comments that allow closure compiler to optimize your code.
  • Requires externs, particularly when using third-party libraries.
  • Closure compiler doesn't always work well with third-party code. In particular, it doesn't work well with module loaders.

OpenLayers and application code separate

  • Creates a custom build of OpenLayers 3 tailored to your needs.
  • Application code does not have to be type-annotated for the closure compiler.
  • Can use other JavaScript compression techniques (such as http://lisperator.net/uglifyjs/) for the application code and third-party libraries. This can often achieve nearly equivalent compression and avoid many of the pitfalls of the closure compiler with third-party libraries.
  • The build size is higher than the all-in-one compilation.
  • Defining the exports for a custom build can be difficult to get right.

Before going further, you will need to follow the installation process for Python, Java, and Node following the guide in Appendix B, More details on Closure Tools and Code Optimization Techniques. These tools are required to create custom builds of OpenLayers.

The standard release archive for OpenLayers does not contain the build tools we require. Additionally, the build tools provided with OpenLayers work only with code cloned from a Git repository. The instructions for this book are designed for version 3.0.0 of OpenLayers. To make sure that you are using the correct version, you can check out the v3.0.0 branch of the repository using the following command:

git checkout -b v3.0.0

If you have already run the OpenLayers build tools on a version other than v3.0.0, it is recommended to remove the build and node_modules folders and start again.

What does the compiler do?

We'll briefly cover how the closure compiler works to optimize your code, as we will need to understand it a little bit to use it properly. More detailed information can be found in Appendix B, More details on Closure Tools and Code Optimization Techniques.

The closure compiler optimizes code by applying a series of transformations to your code. Covering all of these is the topic for a whole book in itself, but for our purposes it does three basic things:

  • Safely rewrite your code into a more optimal form
  • Remove unused code
  • Rename objects, methods, and property names to shorter versions

Rewriting code

When the compiler rewrites your code, it will attempt to use language features that reduce the overall size of your code and execute more efficiently. This can include the following:

  • Removal of extraneous white space
  • Consolidation of variable declarations
  • Restructuring of loops

Generally, these changes are safe and do not change the logic or functionality of your code. They simply take advantage of language features to fit your code into less space.

Removing unused code

The compiler will also attempt to remove the so-called dead code. Dead code is any function that the compiler determines will not be called by the application. It is sometimes difficult for the compiler to automatically determine what code your application will use, especially if you are creating a separate build. In these cases, we need to tell the compiler what things we need to keep even though they might be considered dead code. This is generally done in two ways depending on our strategy. If we are creating a combined build, the compiler can generally determine what functions we will need automatically, but it doesn't know what files to include in the first place. To help the compiler, we add code to register these dependencies. If we are creating a separate build, the compiler doesn't have our code to analyze; so, we have to use a different technique. In this case, we will provide an explicit list of functions to keep in the compiled library.

We will cover both techniques shortly.

Renaming objects, functions, and properties

The final compiler technique we need to discuss is the renaming strategy. Generally, when we are writing code, we use descriptive names for functions and variables. This makes code much more readable to ourselves and others, and it's much easier to remember names that mean something when writing our code. However, these long names take up space—1 byte per character—and when they are used repeatedly, it can really add up. The JavaScript engine in a browser doesn't really care about our descriptive names though. A name like X1 is just as good as GeoJSONParser, and as long as it is used in the right places, we can save 11 bytes for every use of GeoJSONParser by replacing it with X1. Using short names is not practical for application developers, but it is something that can be done really well by a compiler.

Unfortunately, this is one of the biggest gotchas of using the Closure Compiler. It renames objects, functions, and properties very aggressively and makes them effectively unusable by external code. There are two main problems we need to be aware of concerning this renaming.

When creating a standalone build of OpenLayers, we need to make sure that the objects, functions, and parameters we need are not renamed. This is done using exports, which will be covered later in this chapter.

When creating a combined build of OpenLayers with our application code, we no longer have to worry about exports because our use of the OpenLayers objects, functions, and properties will be taken into consideration by the compiler and renamed appropriately. However, if we use other third-party libraries (for instance, jQuery), the closure compiler does not know about them and will rename our use of their objects, functions, and properties. There are two things we can do to tackle the second problem. First, we can provide special JavaScript files, called externs, which define the objects, functions, and properties of external libraries. The compiler can then correctly avoid renaming parts of our code that we need to preserve. The second, is to use string values in key places, because the compiler will never rename a string value. This technique relies on the ability for JavaScript code to reference object properties using array-like syntax. For instance, if we have a property name on an object foo, normally, we would reference it like the following:

foo.name = 'test';

The compiler might rewrite this code like the following:

XB.XC='test';

If foo is an object coming from some external library that relies on name, our code may break in unusual ways. To prevent this, we can use the array-like notation with a string value to avoid renaming:

foo['name'] = 'test';

We'll see an example of this in our next example.

Tip

Exports and externs are quite similar. The first is for choosing the library code you want to expose the third-party code, whereas the second, is to stop the default renaming behavior for the third-party code. To fully grasp the difference, you should refer to the official topic on Do Not Use Externs Instead of Exports! at https://developers.google.com/closure/compiler/docs/api-tutorial3#no.