When working with vector layers, it is common to find a situation where you need to know what is happening, that is, when a new feature is going to be added to the layers or when a feature has been modified, deleted, and so on. Fortunately, vector layer has the capability to trigger a great fan of events.
The goal of this recipe is to show how easy it is to listen for events in a vector layer and know what is happening on it.
We are going to load a GML file with some cities around the world, and we will style its fill color depending on some feature attribute.
Create an HTML file and add the OpenLayers library dependency files. Then, add a
div
element to define where to hold the map instance:<div id="ch04_vector_layer_listener" style="width: 100%; height: 100%;"></div>
Initialize the map instance, add a base layer, and centralize the viewport:
<!-- The magic comes here --> <script type="text/javascript"> /...