Book Image

OpenLayers 3.x Cookbook - Second Edition

By : Peter J. Langley, Antonio Santiago Perez
Book Image

OpenLayers 3.x Cookbook - Second Edition

By: Peter J. Langley, Antonio Santiago Perez

Overview of this book

OpenLayers 3 is one of the most important and complete open source JavaScript mapping libraries today. Throughout this book, you will go through recipes that expose various features of OpenLayers 3, allowing you to gain an insight into building complex GIS web applications. You will get to grips with the basics of creating a map with common functionality and quickly advance to more complicated solutions that address modern challenges. You will explore into maps, raster and vector layers, and styling in depth. This book also includes problem solving and how-to recipes for the most common and important tasks.
Table of Contents (14 chapters)
OpenLayers 3.x Cookbook Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Listening for the vector layer features' events


When working with vector layers, it is common to find a situation where you need to respond to something that's occurred on the layer, such as when a new feature is added, modified, deleted, and so on. Fortunately, these types of events are available from the vector source, and we can easily subscribe to them.

The goal of this recipe is to show you how simple it is to listen for events on a vector source and perform some actions with this information.

We are going to load an external GeoJSON file of geometry points, and we will style the fill color and radius depending on the feature attributes. We will also track how many features are on the vector source layer and allow the user to delete a feature by clicking on it, subsequently updating the feature count. The source code can be found in ch04/ch04-vector-feature-events, and here's what we'll end up with:

How to do it…

In order to understand how vector layer feature events work, code this recipe...