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

Views

The map object is the central component of an OpenLayers web application. It is a central place to add and remove things such as layers and controls, and bind them all together. The remaining chapters in the book will introduce you to these other things, but one of those bits is really very closely tied to the map object, and that is the view object. The view object provides the map with the information it needs to decide what location and level of detail—or zoom level—you are looking at. A view also has a projection (which we discussed in Chapter 2, Key Concepts in OpenLayers) that determines the geospatial reference system of the map.

The view Class

OpenLayers currently provides a single View class, ol.View. This class represents a simple 2D view, which can be manipulated through three key properties: center, resolution, and rotation. We will create a new instance of ol.View in the same way that we create a map object, like the following:

var view = new ol.View(ViewOptions);

We've used views in all our examples because a view is a mandatory parameter when creating a new map instance, but we haven't discussed it in detail yet. Let's dive in.

View options

The options you can use when creating a new ol.View instance are as follows:

Property

Type

Description

center

ol.Coordinate | undefined

This is the initial center for the view. The coordinate system for the center is specified with the projection option. The default value is undefined, and if this property is not set, then layer sources will not fetch data (no map will render).

constrainRotation

boolean | number

This option controls how the view will constrain rotation. False indicates no constraint. True indicates that the view should snap to 0 rotation when the rotation is close to zero, but otherwise, not constraint rotation. If this option is set to a number, then rotation is constrained to that many values. For instance, a value of 4 constrains to 0, 90, 180, and 270.

enableRotation

boolean

This controls whether the view can be rotated at all; the default is true.

extent

ol.Extent | undefined

This establishes a constraint for the center of the view such that it always lies within this extent. If not provided (the default), the view's center is not constrained.

maxResolution

number | undefined

This is the maximum resolution in projection units per pixel that the map supports. This property, combined with minResolution, maxZoom, minZoom, and zoomFactor, determines the zoom levels and resolutions that the map supports. See the section after this table for a complete description of how these properties work together.

minResolution

number|undefined

This is the minimum resolution in projection units per pixel that the map supports.

maxZoom

number | undefined

This is the maximum zoom level that the map supports.

minZoom

number|undefined

This is the minimum zoom level that the map supports.

projection

ol.proj.ProjectionLike

This is the projection of the view; default is EPSG:3857 (Spherical Mercator).

resolution

number | undefined

This is the initial resolution for the view. The units are projection units per pixel (for example, meters per pixel).

resolutions

Array.<number> | undefined

These are resolutions to determine the resolution constraint. If set, the maxResolution, minResolution, maxZoom , minZoom and zoomFactor options are ignored.

rotation

number | undefined

This is the initial rotation for the view in radians (positive rotation clockwise).

zoom

number | undefined

This is the zoom level used to calculate the initial resolution for the view. The initial resolution is determined using the constrainResolution method.

zoomFactor

number | undefined

This is the zoom factor used to determine the resolution constraint. It is used together with maxResolution minResolution, maxZoom, and minZoom. The default value is 2.

Understanding resolution

It is important to understand what is meant by the term resolution and how the various view options related to resolution work together.

In the context of OpenLayers, the term resolution means the number of projection units per pixel. A projection determines how the real world, which is a sphere, is represented in 2D space. The definition of a projection includes a number of things, including a 2D coordinate system and an algorithm for converting real-world locations (latitude and longitude) to and from the projection's coordinate system. The projection's coordinate system is defined in a particular set of units, such as meters, feet, or even decimal degrees. The projection's coordinate system has a bounding box defined as values in the projection's unit system. When we say projection units per pixel, we mean a value expressed in the unit system of the projection.

The most zoomed out state of the map will render the bounds of the projection into a 256 x 256 pixel tile. This is considered the maximum resolution and can be calculated as the width of the projection's bounds divided by 256. When the map is at its most zoomed in, the minimum resolution can be calculated as the width of the projection's bounds divided by the number of tiles wide times 256. We yet don't know how many tiles are required to draw the map at the most zoomed in level; so, this has to be known or computed in some way.

The most zoomed out state of the map is, by convention, called ZoomLevel 0. When a user zooms in or out, they change to the next zoom level. Zooming effectively changes the number of tiles used to represent the projection's bounds. The default zoom factor is 2, which means that zooming in to the next zoom level doubles the number of tiles in the map (in each dimension) and halves the resolution. If ZoomLevel 0 is 1 tile (wide and high), then ZoomLevel 1 is 4 tiles (2 wide and 2 high). ZoomLevel 3 is 16 tiles (4 wide and 4 high). In fact, the number of tiles required for the width and height of any zoom level can be easily calculated as numTiles = 2 ^ zoom. Each zoom level has a resolution defined by the projection's bounds divided by the zoom level's width in tiles. For any given zoom level z, the resolution can be computed as resolution = projection width / (2 ^ z). Likewise, the zoom level equivalent to any resolution can be computed by rearranging the algorithm to z = log(projection width / resolution)/log(2).

When a view is configured, it needs to know a maximum and minimum resolution and a zoom factor for determining the resolutions between the maximum and minimum for zooming. The View class provides several options that are used together to determine the resolution constraints. Since zoom level and resolution can be computed from each other, the constraints can be determined in terms of resolution (maxResolution and minResolution), zoom levels (minZoom is equivalent to maxResolution, maxZoom is equivalent to minResolution), or some combination of these. OpenLayers uses the resolution options in preference to the zoom options if both are provided.

In practice, it is usually sufficient to define just the maxZoom or minResolution because minZoom defaults to 0 (the maximum resolution computed from the projection bounds).

View KVO properties

As with the Map class, the View class has a number of KVO properties that have accessor methods (get and set), property binding, and events as discussed earlier:

Name

Type

Description

center

ol.Coordinate

This is the center of the view, with units determined by the projection.

resolution

number

This is the resolution of the view. The units are projection units per pixel (for example, meters per pixel).

rotation

number

This is the rotation of the view in radians (positive rotation clockwise).

View methods

The View class has several methods in addition to its properties:

Method

Parameters

Description

calculateExtent(size)

sizeol.Size

This calculates the extent in projection units for the given size, based on the current resolution and center.

centerOn( coordinate )

coordinate – ol.Coordinate

This centers the view at the geographic coordinate provided.

constrainCenter( center )

coordinate – ol.Coordinate

Given a center coordinate, this applies any constraints and returns the resulting coordinate.

constrainResolution(resolution, delta, direction)

resolutionnumber | undefined

deltanumber

directionnumber

Given a resolution, this applies any resolution constraints and returns the resulting resolution.

constrainRotation(rotation, delta)

rotationnumber

delta - number

Given a rotation value, this applies any rotation constraint and returns the resulting rotation.

fitExtent(extent, size)

extentol.Extent

size – ol.Size

This fits the given extent based on the given map size.

fitGeometry(geometry, size, options)

geometry – ol.Geometry

size – ol.Size

options - Object

This zooms the map to show the extent of a geometry in a given box size. The options object may contain any of the following:

  • padding: This is an array of four numbers to use as extra padding around the geometry. The order to apply the padding values is top, right, bottom, then left. Default is all 0s (no padding).
  • constrainResolution: This is a Boolean value indicating whether the resolution after zooming should be constrained to the nearest zoom level. The default value is false.
  • nearest: This is a Boolean value used when constrainResolution is true, which indicates whether the nearest resolution should be chosen or not. If this is false, then the next most zoomed out resolution will be used (ensuring the geometry's extent will definitely be visible). If this is true, then the closest zoom level will be used, which might mean that the geometry's extent is not entirely contained after zooming. The default value is false.
  • minResolution: This is the minimum resolution to zoom to when fitting a geometry, which can be used to prevent zooming in really far on small geometries. The default value is 0, which means zoom in as far as possible.

getCenter()

 

This returns the center of the view.

getProjection()

 

This returns the projection of the view.

getResolutionForExtent(extent, size)

extent - ol.Extent

size – ol.Size

This computes the resolution of a given extent and map size.

getZoom()

 

This returns the zoom level of the view.

rotate(rotation, opt_anchor)

rotation – number

opt_anchor – ol.Coordinate

This rotates the view around the specified coordinate, or the center of the map if an anchor is not provided.

setZoom(zoom)

zoom - number

This sets the zoom level of the view.