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

Tiled images' layers and their sources

All tiled layers share some common properties. They inherit from the ol.source.TileImage. Those common properties are quite useful for other ol.source.* that inherit from it, for example, in some cases to put a logo or give attributions (also called credits) for the maps data and/or tiles. You have to be careful in the API docs online as these properties are considered unstable. You have to uncheck the Stable Only checkbox on the top banner. Unstable doesn't mean that you don't have to use those properties but that they may change with future OpenLayers releases. It's very useful for application developers to see what they may need to use or migrate in the future. See the following table for further information:

Name

Type

Description

attributions

Array.<ol.Attribution> | undefined

This gives the source attributions. It's a way to give credits for geographical data and/or tiles providers using an ol.Attribution object. You can provide an array to put multiple attributions.

crossOrigin

null | string | undefined

This property sets configuration for remote access using CORS (Cross Origin Resource Sharing). CORS for security purpose sets rules for accessing remote content.

extent

ol.Extent | undefined

This property defines the extent for the source.

logo

string | undefined

This property defines the logo URL.

opaque

boolean | undefined

This property sets the opacity.

projection

ol.proj.ProjectionLike

This property sets the projection.

tileClass

function | undefined

This property sets a tile class. By default, it references the ol.source.TileImage function.

tileGrid

ol.tilegrid.TileGrid | undefined

This property sets a tile grid.

tileLoadFunction

ol.TileLoadFunctionType | undefined

This property is an optional function to load a tile to a given a URL.

tilePixelRatio

number | undefined

This property is the pixel ratio used by the tile service. For example, if the tile service advertises 256 px by 256 px tiles but actually sends 512 px by 512 px images (for retina / hdipi devices), then tilePixelRatio should be set to 2. The default value is 1.

tileUrlFunction

ol.TileUrlFunctionType | undefined

This is an optional function to get the tile URL given a tile coordinate and the projection.

The most important functions are mostly those that do not start with the word tile. The tileClass, tileGrid, tileLoadFunction and tilePixelRatio exist but we choose to not cover them as they seem out of the scope for beginners; they help setting access to backend with particular tile grids. Along this book, we will sometimes refer to source as layer but it means a layer with the source adapted for a particular backend.

The OpenStreetMap layer

OpenStreetMap (OSM) is a free, Wikipedia style map of the world driven by user contributed content. You are able to use your own OSM tiles or ones provided through the OpenStreetMap servers.

Setting up an OpenStreetMap service and tiles yourself is not too difficult, but it is outside the scope of this book (visit http://switch2osm.org for more information on this). Accessing OSM with OpenLayers, however, isn't.

More information on the OpenStreetMap project can be found at http://www.openstreetmap.org. To access OpenStreetMap, only using the ol.source.OSM constructor without any options is enough. In the previous examples, we were already using this source, so we will not give you another similar example.

Accessing your own OSM tiles

Though using constructor without options can be enough, you can also use other properties. The default constructor code uses the publicly available OSM tiles, but it is easy to point it at your own tiles. To do so, create the layer in this format:

var osmLayer = new ol.layer.Tile({
  source: new ol.source.OSM({
    url: 'http://{a-c}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png'
  })
});

To use this, you will have to replace http://{a-c}.tile.opencyclemap.org/cycle/ with the server hosting your OSM tiles. {z}, {x}, and {y} are variables that OpenLayers will replace with the appropriate values to reference specific map tiles.

The {a-c} parameters mean to say you can access tiles using these URLs:

http://a.tile.opencyclemap.org/cycle,

http://b.tile.opencyclemap.org/cycle,

http://c.tile.opencyclemap.org/cycle

This behavior comes from restrictions from your browser: you can't simultaneously ask images from a same domain. It can only request a certain number of resources from the same domain simultaneously, usually 2 or 4. Using subdomains is a way to bypass this limit and speed up the map displaying.

Understanding OSM tiling

If you have different sources for tiling, it is not only to provide different URLs to access different map images. The way tiling is done depends on established standards.

For example, Stamen layers, OpenStreetMap layers, and MapQuest layers are using the same tiling convention, whereas WMTS (Web Map Tile Service) can adopt other patterns to display tiles. These patterns are set with the ol.tilegrid.TileGrid object that describe for each arbitrary zoomlevel how the tiles are split.

The rules are the following. In reality, the Earth is not a sphere, but in this case, our planet is assimilated to a sphere because we are using Spherical Mercator projection (mainly known as EPSG 900913 or EPSG 3857). Do not worry about projections at the moment, we will review them in a later chapter as it can be a difficult topic to understand.

The entire sphere is projected on a single square at the 0 level. Then, for each zoom, you increase a level and for each tile at the previous zoom, we get 4 tiles at this one.

Understanding OSM tiling

With the preceding diagram, you will know how to access a tile covering East Europe such as Poland at the 3 level. The URL always looks like URL_TO_TILES/level_z/x_in_grid_for_level/y_for_grid_in_level.png. So, for Poland that has a zoom level 3, x equal to 4, an y to 2, the URL could be http://b.tile.openstreetmap.org/3/4/2.png or http://c.tile.openstreetmap.org/3/4/2.png to get the right tile.

OpenStreetMap source class properties

This source enables your layer to consume OpenStreetMap data and customize OpenStreetMap specific properties.

The constructor is ol.source.OSM and its properties can use following options:

Name

Type

Description

attributions

Array.<ol.Attribution> | undefined

This property can provide an array to put multiple attributions

crossOrigin

null | string | undefined

This property sets configuration for remote access using CORS (Cross Origin Resource Sharing)

maxZoom

number | undefined

This property is the maximum zoom. By default, OpenStreetMap servers provide tiles until a maximum level 18. Some servers are able to work until level 20 for micromapping.

url

string | undefined

This property sets the URL referring to tiles. If not defined, the default value is http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png.

tileLoadFunction

ol.TileLoadFunctionType | undefined

This is an optional function to load a tile given a URL.

The MapQuest layer

MapQuest was, until around 2008, the leading provider for web mapping API. With the entry of Google Maps, the leadership was lost. In 2010, the company chose to change its position concerning web mapping API, by choosing to exclusively rely on OpenStreetMap data to do the job.

Two layers are available freely. They also provide other services such as their own web mapping API. You can get further information at the Open developer part website at http://developer.mapquest.com/web/products/open/.

MapQuest source class properties

The MapQuest source class refers directly to the MapQuest tiles server URL.

The constructor is ol.source.MapQuest.

You are only required to use the source constructor included in an ol.layer.Tile object to add the layer. There are only two options' properties. The first one is the layer. It can take values such as osm (roads), sat (satellite), and hyb (hybride) depending on the tiles you want to display. The other property tileLoadFunction will not be covered here: it's not a common requirement for beginners and we already listed it in the ol.source.OSM. At least, you just need to know that it helps change call to tiles with rules defined in your code.

Have a go hero – using OSM layer and MapQuest layers to create a map

The assignment is quite easy:

  1. Create a map with both MapQuest OSM and satellite layers.
  2. Play with the layer used in particular methods such as setVisible to be able to see one layer and hide it to show the other underlying layer.
  3. Find some alternative URLs that provide a URL to tiles coming from OpenStreetMap data too but not coming from the official website.
  4. Find the real URL that MapQuest layers use, using the debugger and in the code of the library itself (available at https://github.com/openlayers/ol3).
  5. Center your map on New York City, United States and choose the zoom you want or the part of the city you prefer. You can use http://openstreetmap.org to get the coordinates' center.

You will get something like the following screenshot if you center on the Statue of Liberty and use the MapQuest OSM layer:

Have a go hero – using OSM layer and MapQuest layers to create a map

Stamen layers

The Stamen layers were named so after a company. To cite the OpenstreetMap wiki, Stamen (http://stamen.com) is a San Francisco design and development studio focused on data visualization and map-making. These layers extensively use OpenStreetMap data in many of their map visualizations and have provided three CC-BY OpenStreetMap tilesets: Toner, Terrain, and Watercolor. You can see how the styles look at the official demo website, http://maps.stamen.com.