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 – changing the default attribution styles

Attributions, as a reminder, are the way to mention credits for layers sources that reference source of the tiles and/or data. The ol.control.Attribution control is dedicated for this.

  1. First, copy the example dedicated to the defaults controls in a new file.
  2. Next, change the ol.control.defaults options in the controls property of the map, and also set logo options to false at the ol.Map level:
    logo: false
    controls: ol.control.defaults({
      attributionOptions: {
      },
    }),
  3. Open your browser (we suppose you are using Google Chrome).
  4. Now, add in attributionOptions to the following content and reload the page:
    attributionOptions: {
      className: 'myCustomClass'
    },
  5. Use the Chrome Developers tools to find the element with myCustomClass to try to understand the effect of the className option in attributionOptions.
  6. Now, again add a new property in attributionOptions:
    attributionOptions: {
      className: 'myCustomClass',
      target: document.getElementById('myattribution'),
    },
  7. Add also in the HTML after the <div id="map" class="map"> </div>the following content:
    <br/>
    <div id="myattribution"></div>
  8. Reload the HTML page and you will see an image like the one that follows:
    Time for action – changing the default attribution styles

What just happened?

We first introduced you to the className property. This enables you to change the default class name for the control. Then, you can customize your control with CSS according to this new class name. You may have noticed that, in the first case, you were unable to see the content in the browser, but only in the debugger because the element was always attached to the map element with its child, the <div class="ol-viewport" ...> tag.

With the second case, we showed you the purpose of the target property: you can tell the control where you want to attach the control. So, you need to use a DOM selector such as document.getElementById('myattribution').

With this action, you might have seen that the control is now well separated from the <div id="map" class="map"></div> HTML.

It is now easy to customize as per your wish and you can display credits outside of the map: it can be useful when you use a lot of layers and don't want to display too much information.

The ol.control.Zoom control

The ol.control.Zoom control displays a plus and minus element to zoom in and zoom out. It is one of the default controls.

Zoom options

The zoom options are similar to both previous controls but we also have some other properties such as a delta property. The other specific properties are only to change text for the control or when the mouse hovers them.

We will not cover those options as they are quite straightforward to understand but advise you to play with it within the context of OpenStreetMap. If you remember, OpenStreetMap behavior for tiles in Chapter 1, Getting Started with OpenLayers (each zoom multiply zoom by 2), you will also remember how to play with the delta property. For each click on the control, your delta is 1. Change this property to a value, either 2 or 4, and try to click on the plus (+) and minus (-) buttons and see the change.

You can find the properties list for the control as follows:

Name

Type

Description

duration

number | undefined

This property sets the animation duration in milliseconds. The default value is 250.

className

string | undefined

This property sets the CSS class name for the control. The default value is ol-zoom.

zoomInLabel

string | undefined

This is the text label to use for the zoom in button. The default value is +.

zoomOutLabel

string | undefined

This is the text label to use for the zoom out button. The default value is -.

zoomInTipLabel

string | undefined

This is the text label to use for the button tip. The default value is Zoom in.

zoomOutTipLabel

string | undefined

This is the text label to use for the button tip. The default value is Zoom out.

delta

number | undefined

The zoom delta is applied on each click.

target

Element | undefined

This option sets the target, to the DOM element where you want to display your control.

The ol.control.Rotate control

The ol.control.Rotate control is not really obvious to understand. As OpenLayers 3 targets mobile browsers or applications, it can be useful to reset the rotation when you choose to make an interactive map that relies on the compass from your device. Sometimes, you want to reset the north direction for readability for end users. In fact, this control is a default one.

You can make it appear by reopening the previous example, then click and drag while pressing down both Shift and Alt keys. You will see a result like the following:

The ol.control.Rotate control

Rotate options

Let me remind you that you will find the options available to customize the Rotate control:

Name

Type

Description

className

string | undefined

This property sets the CSS class name. The default value is ol-rotate.

label

string | undefined

This property sets the text label to use for the rotate button. The default is Upper arrow key, as seen in the upper-right corner in the previous screenshot.

tipLabel

string | undefined

This property sets the text label to use for the rotate tip. The default value is Reset rotation.

duration

number | undefined

This property sets the animation duration in milliseconds. The default value is 250.

autoHide

boolean | undefined

With this, you can hide the control when rotation is 0. The default value is true.

target

Element | undefined

This property sets the target for the control.

You can play around with these options; for example, you can always display North with an arrow like for paper maps with autoHide. It's also possible, if your application is not targeting English-speaking users, to change the tipLabel.

The ol.control.FullScreen control

With this control, you can easily switch to the fullscreen mode that relies on HTML5. Also, for this reason, it will work better with a modern browser that supports this feature, such as Google Chrome, Firefox, and so on. Typing Esc will take you out from this mode.

This is the first control we've reviewed that is a not a default one. If you need it, you can use the ol.Collection returned by the ol.control.defaults method and with it the ol.Collection extend method, add the control using an array to the collection, when instantiating ol.Map with the bare minimum (no options):

controls: ol.control.defaults().extend([new ol.control.FullScreen()]),

As a reminder, supposing map is the ol.Map object name, you can also add the control with the following line:

map.addControl(new ol.control.FullScreen());

FullScreen options

These FullScreen are options for the FullScreen control. Except for the keys property that depends on latest support in browsers and tipLabel to set the tip text on the button, the properties are the usual ones, the ones inherited from ol.control.Control. All are considered as experimental.

Name

Type

Description

className

string | undefined

This property sets the CSS class name for the control. The default value is ol-full-screen.

tipLabel

string | undefined

This property sets the next label to use for the button tip. The default value is Toggle full-screen.

keys

boolean | undefined

This property grants full keyboard access.

target

Element | undefined

This option sets the target, to the DOM element where you want to display your control.

The ol.control.MousePosition control

The ol.control.MousePosition control helps you to determine the coordinates where your mouse is pointing on the map.

One way to do this is by instantiating the element with the syntax that follows:

controls: ol.control.defaults().extend( [
  new ol.control.MousePosition({
    key: value,
    ...
  })
]),

The object into the control constructor is optional.

MousePosition options

The MousePosition options are the available properties you pass to the control ol.control.MousePosition. They help customize the behavior of the control such as coordinates formatting or units.

Name

Type

Description

className

string | undefined

This property sets the CSS class name for the control. The default value is ol-mouse-position.

coordinateFormat

ol.CoordinateFormatType | undefined

This property sets the coordinate format.

projection

ol.ProjectionLike

This property sets the projection and the library uses it for displaying units.

target

Element | undefined

This option sets the target, to the DOM element where you want to display your control.

undefinedHTML

string | undefined

This property sets the markup for undefined coordinates. The default value is an empty string.

We will review these options with an example.