I want to show a layer map with many style depending on some attribute in the layer, but don't know how,. If I use SLD in geoserver I just show only one style, I have tried writing something in javascript (based on Openlayer library) like below, but it didn't work, the code didn't have any effect to the layer map. Any suggestion will be greatly appreciated.
//create a style object var style = new OpenLayers.Style(); //rule used for all polygons var rule_p1 = new OpenLayers.Rule({ filter: new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.EQUAL_TO, property: "vi_tri", value: "1", }), symbolizer: { fillColor: "#00FF00", fillOpacity: 0.6, strokeColor: "#FF0000", strokeWidth: 2, strokeDashstyle: "solid", } }); var rule_p2 = new OpenLayers.Rule({ filter: new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.EQUAL_TO, property: "vi_tri", value: "2", }), symbolizer: { fillColor: "#40FF00", fillOpacity: 0.6, strokeColor: "#FF0000", strokeWidth: 2, strokeDashstyle: "solid", } }); var rule_p3 = new OpenLayers.Rule({ filter: new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.EQUAL_TO, property: "vi_tri", value: "3", }), symbolizer: { fillColor: "#80FF00", fillOpacity: 0.6, strokeColor: "#FF0000", strokeWidth: 2, strokeDashstyle: "solid", } }); var rule_p4 = new OpenLayers.Rule({ filter: new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.EQUAL_TO, property: "vi_tri", value: "4", }), symbolizer: { fillColor: "#FFFF00", fillOpacity: 0.6, strokeColor: "#FF0000", strokeWidth: 2, strokeDashstyle: "solid", } }); style.addRules([rule_p1, rule_p2, rule_p3, rule_p4]); mybinh = new OpenLayers.Layer.WMS( "Lớp Mỹ Bình", urlmapfile, { LAYERS: 'demo:mybinh', transparent:"true", format: format, styleMap: style }, {singleTile: true, ratio: 1, isBaseLayer: false} );