I'd like to have a check/uncheck option like this print checkbox to show/hide labels in my WMS layers served by mapserver 5.6.7 in openlayers 2.11. I have a toolbar where to put this using probably geoext1 or ext3.4, so the questions:

  1. Is this possible?
  2. If so, how can it be done?

Regarding the comment about two separate labels (one for the "rest of the map" and one for the labels): this is what I thought of as well, and it should be fairly easy to implement:

var url = "http://myserver.com/wms";

var background = new OpenLayers.Layer.WMS(
    "Base Map",
    {layers: 'background'} 

var labels = new OpenLayers.Layer.WMS(
    {layers: 'labels'},
    {displayInLayerSwitcher: false}

map.addLayers([background, layer]);

function toggleLabels() {
    var was = labels.getVisibility();
  • thanks atlefren, I'll try that and mark it as answer if it worked. Upvote was done for your support. – Gery May 31 '13 at 4:58

When you are getting data from your Mapserver via a WMS request, the labels are not separate. They are present in the image itself. Mapserver will put them in the WMS Map Image, based on settings in the map file.

What needs to be done, is that you have to ask Mapserver not to put in the Labels in the output image. This can be done by using the correct parameters in the WMS request. You need to create a SLD, for rendering the map, without the label.

There are two parameters that could be used.

  1. You can use either the SLD parameter, where you pass the

    URL to the SLD. Applies the SLD on the layer and the legend is drawn after the SLD is applied (using the classes specfied by the SLD). Note here that you need to put a class1 inside the Rule element so that a class name is created from the SLD and therefore a correct legend image.

  2. Or you could use the SLD_BODY parameter where you pass the

    body (code) of the SLD, instead of specifying a URL (as in the ‘SLD’ parameter).

  • Thanks for the reply. I've read about the SLD in the WMS request link but seems to me that it's about labels in legend, and I need to show/hide labels in the layer from the client (openlayers). I've found this: ... it often makes sense to store labels as separate layers in the MAP file, and switch them on and off through the client application (OpenLayers, WMS client etc.) posted here: gis.stackexchange.com/questions/10413/mapserver-hide-labels, it's what I need, do you know an example for this? – Gery Apr 30 '13 at 9:43
  • Your understanding of SLD's is incorrect. SLD are used to style the Data. They will style the data both in the Legend, as well as the Map Image. As to your second point, I really haven't used much of Mapserver, so I don't know how to do that. – Devdatta Tengshe Apr 30 '13 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.