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

Chapter 4. Working with Events

In this chapter, we will cover the following topics:

  • Creating a side-by-side map comparator
  • Implementing a work-in-progress indicator for map layers
  • Listening for the vector layer features' events
  • Listening for mouse or touch events
  • Using the keyboard to pan or zoom

Introduction

This chapter is focused on events, which is an important concept in any JavaScript application. Although this chapter is brief, the concepts explained here are very important to understand when working with OpenLayers and mapping applications in general.

Events are fundamental in JavaScript. They are the impulses that allow us to produce a reaction. As programmers of a mapping application, we are interested in reacting when the map zoom changes, when a layer is loaded, or when a feature is added to a layer. Every class, which is susceptible to emit events, is responsible for managing its listeners (those interested in being notified when an event is fired) and also to emit events under certain circumstances.

For example, we can register a function handler that listens for the change:resolution event on the OpenLayers map view instance. Every time the view instance changes its zoom level, it has the responsibility to trigger the change:resolution event, so all its listeners will be notified by the new event.

To help in all this process, OpenLayers has an event class called ol.ObjectEvent that extends the event class (goog.events.Event) from the Google Closure library, which takes care of registering listeners and simplifying the action of firing an event to all of them. In summary, this class allows listeners to perform the following:

  • Define an event
  • Register listeners
  • Trigger events to notify all listeners

Some classes, such as ol.Map, extend the base ol.ObjectEvent class to customized subclasses that are better suited for the specific set of available events. The ol.Map class has an event class called ol.MapEvent that provides some base properties and events. For example, it contains an event called moveend, which needless to say, fires when the map is moved.

The ol.MapEvent class gets further extended by a class called ol.MapBrowserEvent, providing extra methods and events: click and pointerdrag, to name a few. The event object, which is reflective of the action against the map, has additional information, such as the coordinates of the map event and the pixel position of the browser event.

Besides the ol.Map class, many other classes, such as ol.layer.Vector, also emit an abundance of event types (of ol.ObjectEvent). When layer properties, such as extent, opacity, and source change, there's an opportunity to register a subscription (handler or listener) to these events that get published.

As a programmer, you'll need to look at the OpenLayers API documentation, which is accessible from the OpenLayers website (http://openlayers.org), or you can also refer to the source code to find out about the available events that you can register on each class. The source code is also hosted on GitHub (https://github.com/openlayers/ol3).