Table of Contents for
Mastering OpenLayers 3

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering OpenLayers 3 by Gábor Farkas Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. Mastering OpenLayers 3
  4. Mastering OpenLayers 3
  5. Credits
  6. About the Author
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Conventions
  13. Reader feedback
  14. Customer support
  15. 1. Creating Simple Maps with OpenLayers 3
  16. Structure of OpenLayers 3
  17. Building the layout
  18. Using the API documentation
  19. Debugging the code
  20. Summary
  21. 2. Applying Custom Styles
  22. Customizing the default appearance
  23. Styling vector layers
  24. Customizing the appearance with JavaScript
  25. Creating a WebGIS client layout
  26. Summary
  27. 3. Working with Layers
  28. Building a layer tree
  29. Adding layers dynamically
  30. Adding vector layers with the File API
  31. Adding vector layers with a library
  32. Removing layers dynamically
  33. Changing layer attributes
  34. Changing the layer order with the Drag and Drop API
  35. Clearing the message bar
  36. Summary
  37. 4. Using Vector Data
  38. Accessing attributes
  39. Setting attributes
  40. Validating attributes
  41. Creating thematic layers
  42. Saving vector data
  43. Saving with WFS-T
  44. Modifying the geometry
  45. Summary
  46. 5. Creating Responsive Applications with Interactions and Controls
  47. Building the toolbar
  48. Mapping interactions to controls
  49. Building a set of feature selection controls
  50. Adding new vector layers
  51. Building a set of drawing tools
  52. Modifying and snapping to features
  53. Creating new interactions
  54. Building a measuring control
  55. Summary
  56. 6. Controlling the Map – View and Projection
  57. Customizing a view
  58. Constraining a view
  59. Creating a navigation history
  60. Working with extents
  61. Rotating a view
  62. Changing the map's projection
  63. Creating custom animations
  64. Summary
  65. 7. Mastering Renderers
  66. Using different renderers
  67. Creating a WebGL map
  68. Drawing lines and polygons with WebGL
  69. Blending layers
  70. Clipping layers
  71. Exporting a map
  72. Creating a raster calculator
  73. Creating a convolution matrix
  74. Clipping a layer with WebGL
  75. Summary
  76. 8. OpenLayers 3 for Mobile
  77. Responsive styling with CSS
  78. Generating geocaches
  79. Adding device-dependent controls
  80. Vectorizing the mobile version
  81. Making the mobile application interactive
  82. Summary
  83. 9. Tools of the Trade – Integrating Third-Party Applications
  84. Exporting a QGIS project
  85. Importing shapefiles
  86. Spatial analysis with Turf
  87. Spatial analysis with JSTS
  88. 3D rendering with Cesium
  89. Summary
  90. 10. Compiling Custom Builds with Closure
  91. Configuring Node JS
  92. Compiling OpenLayers 3
  93. Bundling an application with OpenLayers 3
  94. Extending OpenLayers 3
  95. Creating rich documentation with JSDoc
  96. Summary
  97. Index

Customizing a view

Customizing the view is not a hard task; it manly consists of considerations regarding the nature of the project and expected outcome. For our application, wrapping the map around the x axis is completely needless. Also, we can restrict the extent to the projection's extent. In the first example, called ch06_customize, we make such modifications in our application. First, we disable the layer wrapping in our base layers:

    var map = new ol.Map({
        […]
            new ol.layer.Tile({
                source: new ol.source.TileWMS({
                    […]
                    wrapX: false
                […]
            new ol.layer.Vector({
                source: new ol.source.Vector({
                    […]
                    wrapX: false
                }),

Tip

Unlike in OpenLayers 2, you cannot make a high level decision to disable layer wrapping for the entire map. You have to disable it layer-wise in the layer's source object, as it is enabled in most of the sources by default.

With this consideration, we gained an instant boost in performance. However, we have to say goodbye to it instantly as this will make our map more responsive. By default, when we are interacting with the map or an animation takes place, tile and vector layers wait for us to finish before they get updated. We can disable this phenomenon with two options. In case of vector layers, we have to disable it in the layer level, but for tile layers, we can disable it in the map object. We can do it in this way:

            new ol.layer.Vector({
                […]
                updateWhileAnimating: true,
                updateWhileInteracting: true
            })
        […]
        loadTilesWhileAnimating: true,
        loadTilesWhileInteracting: true
    });

In the next step, we'll modify the view object to restrict the maximum extent. Note that we can pan outside the map's extent since this option only constrains the center of the map:

        view: new ol.View({
            […]
            extent: ol.proj.get('EPSG:3857').getExtent()
        }),

Note

As the Web Mercator projection is provided by the library, we can use a shortcut to access it. Later, we will see how to add new projections to the library's projection list.

Finally, we enable some keyboard interactions. There are two interactions related to the keyboard events in OpenLayers 3, and both of them get added to the map by default. However, we cannot use them as only focused elements can receive keyboard events, and only input-related elements can be focused by default. To make our map's div element focusable, we must add a tabindex property to it in the HTML file:

[…]
<div id="map" class="map" tabindex="-1"></div>
[…]

Now, if we click on the map, we can pan with the arrow keys and zoom in with the + (plus) and - (minus) keys.

Tip

You can provide -1, 0, or any positive integer to the tabindex property. The -1 value opts out the element from focusing with the Tab key, but it makes it focusable with a mouse click or with the focus method. The 0 value does not alter the natural focusing order with the Tab key, while a positive integer determines its order.

If a focusable element gains focus, some browsers render an outline on the border of the element. As we do not want to have an outline on our map, we will disable it in our CSS file by extending our map class with a single rule:

.map {
    […]
    outline: 0;
}