Let's use rules and filters to assign different styles to feature objects based on their attributes.
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);
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) } )]); }
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...