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 – using rules and filters


Let's use rules and filters to assign different styles to feature objects based on their attributes.

  1. Let's jump in to using rules and filters. Add a WMS layer to the map. We'll also need a vector layer, so add in a vector layer:

            vector_layer = new OpenLayers.Layer.Vector('Basic Vector Layer'); 
            map.addLayer(vector_layer);
  2. Next, we'll need some features. We'll generate some random feature points again, and we'll create a property for each object called population which contains a random number between 0 and 2000:

    for(var i=0; i<20; i++){
      vector_layer.addFeatures([new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(
         (Math.floor(Math.random() * 360) - 180),
          (Math.floor(Math.random() * 180) - 90)
        ),
        {
          'population': Math.floor(Math.random() * 2000)
        }
      )]);
    }
  3. Now we need to create a style object—rules are applied directly to style objects, and filters are applied directly to rules. So, let's create...