[OpenLayers-Users] Changing the style at runtime of the WMS

Tim Schaub tschaub at opengeo.org
Fri Nov 14 14:03:23 EST 2008


Hey-

Lucas vargas wrote:
> Hi,
> 
> I'm trying with this code:
> 
> basic_wms = new OpenLayers.Layer.WMS("topp:world", 
> "http://localhost:8888/geoserver/wms", {
>             srs: 'EPSG:4326', transparent: 'true', layers: 'topp:world', 
> styles: '',               
>                 format: format, tiled: 'true' }, {isBaseLayer: true}, 
> {buffer: 0});              
>                
>         map.addLayers([basic_wms]);
>         map.zoomToMaxExtent();
> 
>         var stationStyles = new OpenLayers.StyleMap({ // This is only 
> for stations
>                 "default": new OpenLayers.Style({
>                     fillColor: "#006699",
>                     strokeColor: "#ff9933",
>                     strokeWidth: 2,
>                     fillOpacity: 0.6
>                 }),
>                 "select": new OpenLayers.Style({
>                     fillColor: "#006699",
>                     strokeColor: "#ff9933",
>                     strokeWidth: 6,
>                     fillOpacity: 1
>                 })
>             });
> 
> basic_wms.mergeNewParams({ styles : stationStyles['select']});
> 

An OpenLayers.StyleMap is used by vector layers.  If you know SLD, you 
can think of it as a series of user styles.  When you request rendered 
tiles with WMS, you are specifying a named style in your styles 
parameter (or a series of named styles).

Named styles are configured on the server, user styles can be configured 
on the client.  So, if you knew of a named style called "somestyle" that 
had been configured on the server, you could update your WMS layer to 
use that:

basic_wms.mergeNewParams({styles: "somestyle"});

This would add the styles=somestyle pair to the query string in your 
GetMap request.

If you want to ask your WMS to render data using some custom style, then 
you have to 1) include a serialized sld:StyledLayerDescriptor element as 
a SLD_BODY parameter in your query string, or 2) include a URL to an SLD 
doc in the SLD parameter of your query string.

The first option is a bit of work, but can be done with the SLD format.

Alternatively, you can use a vector layer to render your data 
client-side.  In this case, you just give the layer the style map you 
created above.

Tim

> but does not work: D
> 
> Can anybody help me?
> 
> Thanks.
> 
> Lucas Vargas Eskopinski.
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.



More information about the Users mailing list