Book Image

OpenLayers 2.10 Beginner's Guide

Book Image

OpenLayers 2.10 Beginner's Guide

Overview of this book

Table of Contents (18 chapters)
OpenLayers 2.10
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for Action – destroying features


Let's demonstrate the difference between removeFeatures and destroyFeatures.

  1. Open up the first example from the chapter. We'll use Firebug again.

  2. Before we remove features, we first need to add them to the map. We'll use the same code from the previous example. Let's create a feature object. In Firebug, type and execute:

    var feature_point = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-72, 42));
  3. Now, let's add it to the map:

    map.layers[1].addFeatures([feature_point]);
  4. We have the feature on the map now, so let's see how removeFeatures works. We can call this function and either pass in a feature from the map.layers[1].features array, or pass in a feature object we've already created. Let's use the second method:

    map.layers[1].removeFeatures([feature_point]);
  5. Now you should not see any features on your map. If we check the map.layers[1].features array, it should be empty:

    >>>map.layers[1].features;
    []
  6. So, as we expected, the feature we originally...