turn layer labels on and off dynamically

Stephen Woodbridge woodbri at SWOODBRIDGE.COM
Tue May 29 23:25:35 EDT 2007


Gregor Mosheh wrote:
>> I am using the Java MapScript for my application and I would like to
>> present the option for the user to decide whether or not to show labels
>> for a given layer. Is there any way to turn the label for a layer on or
>> off dynamically? I didn't see anything obvious in the MapScript API.
> 
> Speaking from the world of PHP/Mapscript, with hopes that it's 
> applicable to your situation... Our solution is to set the label color 
> to something invalid, to suppress the rendering of labels, e.g.:
> 
>    $class = $layer->getClassNyName('roads');
>    if (!$labels) {
>       $class->label->color->setRGB(-1,-1,-1);
>    }
> 
> 
> 

Ooops, that does not quite work the way you are expecting it to. color 
-1 -1 -1 means transparent. so you are rendering the labels but they are 
just transparent. Since you are rendering the labels, it takes cpu time 
and it might bump other labels out of the cache. I don't think you 
really want to do this.

A better solution might be to set the LABELMINSCALE or LABELMAXSCALE to 
a value that would force the labels off, then reset the value to its 
normal value to turn them back on based on the zoom scale.

You might also be able to use LABELREQUIRES "0" | "1" to turn on/off labels.

Both of these are LAYER attributes.

I have not played with either of these, but you should be able to get 
one of them to work.

-Steve W.



More information about the mapserver-users mailing list