Time For Action – using addUniqueValueRules
Now that we know about addUniqueValueRules
, let's see it in action.
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);
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' }
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...