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 addUniqueValueRules


Now that we know about addUniqueValueRules, let's see it in action.

  1. We're going to use a basic vector layer with some randomly generated features and settlement_type properties. Add a WMS layer. Then, we need a vector layer:

    vector_layer = new OpenLayers.Layer.Vector('Settlement Vector Layer');
    map.addLayer(vector_layer);
  2. Now, let's create an anonymous object consisting of integer values as the keys and our desired settlement_type values as the values. We do this because we'll need a way to randomly pick what type of settlement a feature should be.

    var  settlement_values = { 
        0: 'hut', 
        1: 'village', 
        2: 'city', 
        3: 'metropolis', 
        4: 'facebook' 
    }
  3. Ok, let's now create 20 random points. Using code similar to the previous example, we'll create twenty random points and assign a random settlement type to each feature using the settlement_values object we just created.

    for(var i=0; i<20; i++){ 
        vector_layer.addFeatures([new OpenLayers...