Table of Contents for
OpenLayers 3.x Cookbook - Second Edition

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition OpenLayers 3.x Cookbook - Second Edition by Antonio Santiago Perez Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. OpenLayers 3.x Cookbook Second Edition
  4. OpenLayers 3.x Cookbook Second Edition
  5. Credits
  6. About the Authors
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Sections
  13. Conventions
  14. Reader feedback
  15. Customer support
  16. 1. Web Mapping Basics
  17. Creating a simple fullscreen map
  18. Playing with the map's options
  19. Managing the map's stack layers
  20. Managing the map's controls
  21. Moving around the map view
  22. Restricting the map's extent
  23. 2. Adding Raster Layers
  24. Using Bing imagery
  25. Using OpenStreetMap imagery
  26. Adding WMS layers
  27. Changing the zoom effect
  28. Changing layer opacity
  29. Buffering the layer data to improve map navigation
  30. Creating an image layer
  31. Setting the tile size in WMS layers
  32. 3. Working with Vector Layers
  33. Adding a GML layer
  34. Adding a KML layer
  35. Creating features programmatically
  36. Exporting features as GeoJSON
  37. Reading and creating features from a WKT
  38. Using point features as markers
  39. Removing or cloning features using overlays
  40. Zooming to the extent of a layer
  41. Adding text labels to geometry points
  42. Adding features from a WFS server
  43. Using the cluster strategy
  44. Reading features directly using AJAX
  45. Creating a heat map
  46. 4. Working with Events
  47. Creating a side-by-side map comparator
  48. Implementing a work-in-progress indicator for map layers
  49. Listening for the vector layer features' events
  50. Listening for mouse or touch events
  51. Using the keyboard to pan or zoom
  52. 5. Adding Controls
  53. Adding and removing controls
  54. Working with geolocation
  55. Placing controls outside the map
  56. Drawing features across multiple vector layers
  57. Modifying features
  58. Measuring distances and areas
  59. Getting feature information from a data source
  60. Getting information from a WMS server
  61. 6. Styling Features
  62. Styling layers
  63. Styling features based on geometry type
  64. Styling based on feature attributes
  65. Styling interaction render intents
  66. Styling clustered features
  67. 7. Beyond the Basics
  68. Working with projections
  69. Creating a custom control
  70. Selecting features by dragging out a selection area
  71. Transitioning between weather forecast imagery
  72. Using the custom OpenLayers library build
  73. Drawing in freehand mode
  74. Modifying layer appearance
  75. Adding features to the vector layer by dragging and dropping them
  76. Making use of map permalinks
  77. Index

Using the keyboard to pan or zoom

Users of your mapping application may feel more comfortable navigating around the map with the use of a keyboard. This may also fulfill some accessibility requirements of the app. For whatever reason, we'll demonstrate how to add and customize keyboard control for a map.

The source code of this recipe can be found in ch04/ch04-keyboard-pan-zoom/.

How to do it…

To enable the arrow keys to pan and the plus and minus keys to zoom, follow these steps:

  1. Create an HTML file and include the OpenLayers dependencies and a div element for the map.
  2. Create a custom JavaScript file and set up both keyboard interactions, as follows:
    var keyboardPan = new ol.interaction.KeyboardPan({
      duration: 90,
      pixelDelta: 256
    });
    
    var keyboardZoom = new ol.interaction.KeyboardZoom({
      duration: 90
    });
  3. Instantiate and configure the map, making sure to add the keyboard interactions:
    new ol.Map({
      view: new ol.View({
        zoom: 10,
        center: [-12987415, 3851814]
      }),
      target: 'js-map',
      layers: [
        new ol.layer.Tile({source: new ol.source.Stamen({
          layer: 'terrain'
        })})
      ],
      interactions: ol.interaction.defaults().extend([
        keyboardPan, keyboardZoom
      ]),
      keyboardEventTarget: document
    });

How it works…

We've enabled full panning and zooming control directly from the keyboard in a small amount of code. Let's dive right in with the details.

var keyboardPan = new ol.interaction.KeyboardPan({
  duration: 90,
  pixelDelta: 256
});

Both keyboard events are part of the OpenLayers ol.interaction object, which houses a whole host of interactions—DragPan and PinchRotate to name a few. We've customized some properties of the panning interaction, namely duration (in milliseconds) and pixelDelta (the amount of pixels the map moves in a single pan).

The similar keyboard zoom interaction has been passed a custom duration of 90 ms, as well. The keyboard zoom interaction has a delta property that accepts a number representing the quantity of zoom levels to execute per zoom alteration.

There are two specific map configuration properties that we'll look over next:

  interactions: ol.interaction.defaults().extend([
    keyboardPan, keyboardZoom
  ]),
  keyboardEventTarget: document

An instance of an OpenLayers map already comes with a variety of interactions enabled, of which KeyboardPan and KeyboardZoom are actually included. However, we've decided to customize the settings of these interactions, which are easily achieved by extending the array of the default interactions with the two modified keyboard ones.

By default, the keyboard events responsible for panning and zooming are only processed by OpenLayers if the map element (the div element containing the map) has focus. This means that the DOM element must have a tabindex property with a numeric value. This is easy enough to do, but, as our map is fullscreen, we have specified that the event target for keyboard events is the whole web page (document) via the map property, keyboardEventTarget. So, as long as the browser window has focus, keyboard interaction will work without the need to manually focus on the map DOM element by tabbing around.

Both keyboard interactions have a getActive method that can be called to determine whether or not the interaction is currently enabled.

See also

  • The Creating features programmatically recipe in Chapter 3, Working with Vector Layers
  • The Working with geolocation recipe in Chapter 5, Adding Control
  • The Creating a side-by-side map comparator recipe
  • The Listening for vector layer features' events recipe