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

Time for action – creating a Bing Maps layer

Bing Maps tiles can be viewed using the official Bing Maps API, but they can also be used through an API that gives direct access to their tiles. This is what we will see now:

  1. Go to the Microsoft Mapping API register website at http://www.bingmapsportal.com to get an official key.
  2. Let's set up the source object now. We need to not only specify the style property but also the key. Contrary to the previous examples, a tile access control is done and works through an account associated with a key to use in your code:
      var sourceBingMaps = new ol.source.BingMaps({ 
        key: 'AibU6zHqoTPYDuNRtHPMJq557poKb9AVTIJ0NWWnNZf8LfoRRwoigHTQ0Frrsr5m',
        imagerySet: 'Road',
      });
  3. Now, create the first ol.layer.Tile layer:
      var bingMapsRoad = new ol.layer.Tile({ 
        source: sourceBingMaps
      });
  4. Now, add another layer using the other type of image 'Aerial':
      var bingMapsAerial = new ol.layer.Tile({ 
        source: new ol.source.BingMaps({ 
          key: 'AibU6zHqoTPYDuNRtHPMJq557poKb9AVTIJ0NWWnNZf8LfoRRwoigHTQ0Frrsr5m', 
        style: 'Aerial', 
        }) 
      });
  5. Let's create the map and add the two layers and the view directly:
      var map = new ol.Map({ 
        layers: [bingMapsRoad, bingMapsAerial], 
        target: 'map', 
        view: new ol.View({ 
        center: ol.proj.transform([6.562783, 46.517814], 'EPSG:4326', 'EPSG:3857'), 
          zoom: 13 
        })
      });
  6. You should see something like the following image:
    Time for action – creating a Bing Maps layer

    Note

    To make the example work, it's required to put your file on a web server. It can be Apache, but with Python, you can simply do it in the command line in your demo file root directory:

    python -mSimpleHTTPServer

    Next, open your browser to http://localhost:8000/sandbox/your_file_name.html.

    Another way can be to use the node index.js command (if you downloaded the samples code) and open http://localhost:3000/sandbox/your_file_name.html.

    Also, you don't have the obligation to create an API key when you are on your local machine. The one used in this book works only when using localhost or the official book website at http://openlayersbook.github.io.

What just happened?

We just made a map using the Microsoft Bing Map tiles' API. It works similar to the OpenStreetMap tiles layers but required to be authenticated. We can communicate with Microsoft tiles API server and use different properties. Let's go over the properties, as there are some that the other third-party sources do not provide.

Bing Maps source class properties

Bing Maps source helps you consume Bing tiles representing road and imagery in your layer.

The constructor is ol.source.BingMaps and the following table is for the options:

Name

Type

Description

culture

string | undefined

It is the language and the localization you want to display for your labels. The supported Culture Codes for each style are listed in the official Microsoft documentation at http://msdn.microsoft.com/en-us/library/hh441729.aspx.

kety

string

This is the Bing Maps API key. Get yours at http://bingmapsportal.com.

imagterySet

string

This is a type of imagery. It can be Road, Aerial, and AerialWithLabels. Some others are also available for local parts of the world, such as collinsBart or ordnanceSurvey.

tileLoadFunction

ol.TileLoadFunctionType | undefined

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

The TileJSON layer

The TileJSON format was invented by MapBox, another company providing OpenStreetMap related services. It relies on JSON notation.

According to the specification, TileJSON is an open standard for representing map metadata. Its main goal is to reference the name, the attribution, the server URL, the minimum and max zoom, the center, the bounds, and the scheme of the tile (for OSM, numbering go top to bottom but other tiles' systems start numbering upwards). You can see all available parameters looking at the official specification at https://github.com/mapbox/tilejson-spec.

As it is not the mainstream way to get tiles, although it's a smart way, we will not review it through an example but directly advise you to go the official demo available at http://openlayers.org/en/v3.0.0/examples/tilejson.html and look into the code.

TileJSON source class properties

TileJSON source permits you to display in a tiled layer custom tile. You can declare their source using the following constructor:

ol.source.TileJSON

You can also review the available properties, particularly the required URL parameter on the official API docs at http://openlayers.org/en/v3.0.0/apidoc/ol.source.TileJSON.html.

WMTS layers

The WMTS layer is based on the WMTS standards. The specification defines it as The Web Map Tile Service described in this standard builds on earlier efforts to develop scalable, high-performance services for web-based distribution of cartographic maps.

A WMTS-enabled server application can serve map tiles of spatially referenced data using tile images with predefined content, extent, and resolution.

This type of layer is quite different from most of the previous tiled layers we've seen. WMTS layers are more customizable. You can make a request to get tiles in custom projection, you can also choose your grid without caring about the implicit rules, that each tile when you zoom it will give you four tiles, as illustrated with the following figure:

WMTS layers

It's really not the most common tiles layer, but you can see it as the most efficient tile system for custom requirements. You can better choose your level of tiling or you can also use custom tile size. For example, for mobile, a smaller tile with 64 pixels for its side can be used instead of the standard 256 pixels size. We recommend, if you are curious, to see the official examples at http://openlayers.org/en/v3.0.0/examples/ using the WMTS keyword. We will not be covering the details here. You can learn more about the standard itself by going to the official dedicated web pages at http://www.opengeospatial.org/standards/wmts.

You also need to understand that one of its main goals was to fill the issue for fast rendering contrary to OGC WMS standard as it can be cached.

WMTS source class properties

WMTS sources are complex to declare. They require more parameters than other sources because of their flexibility. For example, the ability to customize for each tile levels grid means also as a drawback, complexity.

The constructor is ol.source.WMTS. To see its options, we recommend that you visit the official API docs at http://openlayers.org/en/v3.0.0/apidoc/ol.source.WMTS.html.

The DebugTileSource source

The DebugTileSource source is only a way to debug tiles rendering in OpenLayers 3. It doesn't use the tile numbering from the source but the internal that OpenLayers use. We just mentioned it to be exhaustive. You use it into a ol.layer.Tile class. You can look at the demo to learn more about it at http://openlayers.org/en/v3.0.0/examples/canvas-tiles.html. It can really help you to debug special grid, for example, in WMTS.

TileDebugTile source class properties

The TileDebugTile source enables you to display numbered grids to show how tiles are regrouped in the OpenLayers Canvas rendering. Canvas is a renderer to display a map in your browser.

The constructor is ol.source.TileDebug and for the options, you should visit the official API docs at http://openlayers.org/en/v3.0.0/apidoc/ol.source.TileDebug.html.

OpenLayers tiled WMS

A WMS (Web Map Service) is a standard protocol for serving georeferenced map images over the Internet that are generated by a map server. You can watch the full reference at the official OGC (Open Geospatial Consortium) website, the organization managing this standard (http://www.opengeospatial.org/standards/wms).

The two versions of WMS available are 1.1.1 and 1.3.0.

WMS is dynamic: you can generate on the fly images.

So, why do I see a reference to a tiled WMS in OpenLayers 3?

It's just a way to lower charge on server-side—when you are generating small images, the process is faster, the memory cost does not really change as the number of requests increase, but you gain the ability to cache the tiles.

But wait, so why can't we use ImageWMS with WMS if we can have the advantage of both tiled (caching) and untiled system (freshness)?

We will not directly answer this right now, but will give you the hint in the section dedicated to ImageWMS.

You can refer to the official example to see a simple example as a reference at http://openlayers.org/en/v3.0.0//examples/wms-tiled.html.

Note

To find out what projections a WMS service supports, you can make a getCapabilities request to the server. To make this request, specify the request, service, and version properties in the URL. For example, http://suite.opengeo.org/geoserver/wms?service=WMS&version=1.1.1&request=GetCapabilities.

You also have to be cautious. Here, we use the Version 1.1.1 version call but the specification supports two versions: 1.1.1 and 1.3.0. It's recommended to use 1.3.0 nowadays, but you can meet external WMS web services using only 1.1.1. Just use the VERSION parameter in the params options.

Tiled WMS source class properties

Tiled WMS source helps you set tiled WMS calls for your layer. The constructor is ol.source.TiledWMS and the options are listed in the following table:

Name

Type

Description

attributions

Array.<ol.Attribution> | undefined

This property sets an attributions array for the source.

params

Object.<string, *>

This property contains an object of WMS request parameters. At least, a param layer is required. Styles are set by default. The version is 1.3.0 by default. The width, height, BBOX, and CRS (SRS for WMS, version less than 1.3.0) will be set dynamically.

crossOrigin

null | string | undefined

This property sets the crossOrigin setting for image requests.

extent

ol.Extent | undefined

This property sets the extent of your layer source.

tileGrid

ol.tilegrid.TileGrid | undefined

This property enables you to declare a tile grid, a custom grid for your source layer.

maxZoom

number | undefined

This property sets the maximum zoom level.

projection

ol.ProjectionLike

This property sets the projection.

url

string | undefined

This property sets WMS service URL.

urls

Array.<string> | undefined

This property sets an array of WMS service URLs. Use this instead of URL when the WMS supports multiple URLs for GetMap requests.

OpenLayers Zoomify

Contrary to most components that are useful only in a map context, this component helps you to display any arbitrary images. It's really useful when you need to display a large image and want performances associated with tiles displayed and the usual interactions of mapping such as panning and zooming.

Some use cases can be game maps, discovery of scanned historical documents such as birth certificates for genealogy, or the old maps with no referencing, meaning that they do not overlay well with existing imagery and geographical data.

To make the ol.source.Zoomify component work, you will need to preprocess a large image to create tiles.