Toggle Labels?

2473
7
Jump to solution
08-07-2013 05:41 AM
MarkHoover
Occasional Contributor II
We have a few map layers in our application for which we'd like to toggle the labels off and on.  We've published the layers out with labels, but trying to set showLabels through the LayerDrawingOptions of the service is proving ineffective.

As far as I can tell, if you only set up showLabels = false, the features do not draw on the map at all, because the LayerDrawingOptions object overwrites all of the drawing properties of the layer.  As a result, the layer no longer has a renderer and doesn't draw.  But the problem is, I've gone out and grabbed the renderer information as JSON from my service, passed that over to the LayerDrawingOptions, and still I get nothing.

Has anyone had any luck setting up label toggling?
0 Kudos
1 Solution

Accepted Solutions
JohnGravois
Frequent Contributor
if the option is enabled, you will see "Supports Dynamic Layers: true" in the REST endpoint for the map service. 

in order to set this property when publishing a service, you need to toggle "Allow per request modification of layer order and symbology" box within the Mapping capability.

View solution in original post

7 Replies
BrandonVan_Horn
Occasional Contributor
We run a separate service for labels. That way you can turn them on/off at will.
0 Kudos
MarkHoover
Occasional Contributor II
Thanks for the suggestion, Brandon.  Due to the number of layers in our app, the potential maintenance of that idea makes us shirk away from it.  A workaround I suppose, but not one we're eager to try.  Considering there appears to be a way to do this through the API I'd prefer either getting that to work or declaring this some sort of bug if we just can't for whatever reason.
0 Kudos
MarkHoover
Occasional Contributor II
Can anyone from Esri verify how this should work?  Here is the relevent code:

Ext.Ajax.request({
    url: node.attributes.url + "/" + node.attributes.lyrIds[0] + '?f=json',
    method: 'POST',
    success:
        function (response) {
            var layerProperties = Ext.util.JSON.decode(response.responseText);
            var rendererJSON = Ext.util.JSON.encode(layerProperties.drawingInfo.renderer);
            var renderer;
            if (layerProperties.drawingInfo.renderer.type == 'uniqueValue') {
                renderer = new esri.renderer.UniqueValueRenderer(rendererJSON);
            }
            else if (layerProperties.drawingInfo.renderer.type == 'classBreaks') {
                renderer = new esri.renderer.ClassBreaksRenderer(rendererJSON);
            }
            else if (layerProperties.drawingInfo.renderer.type == 'simple') {
                renderer = new esri.renderer.SimpleRenderer(rendererJSON);
            }

            var layerDrawingOptions = [];
            var layerDrawingOption = new esri.layers.LayerDrawingOptions();
            layerDrawingOption.showLabels = false;
            layerDrawingOption.transparency = 1;
            layerDrawingOption.renderer = renderer;
            layerDrawingOptions[node.attributes.lyrIds] = layerDrawingOption;
            layer.setLayerDrawingOptions(layerDrawingOptions);
        }
});
0 Kudos
JohnGravois
Frequent Contributor
I couldn't find a 10.1 sample service with visible labels and the option to render layers dynamically, so i published my own and confirmed that you dont have to set a renderer to get labels to stop displaying...

app.map = new esri.Map("map", { 
  basemap: "topo",
  center: [-88.13533, 41.78473],
  zoom: 6,
  slider: true
});
        
dynUrl = "http://[servername]/arcgis/rest/services/statesLabels/MapServer";
app.dynLayer = new esri.layers.ArcGISDynamicMapServiceLayer(dynUrl, { 
  "id": "usa",
  "opacity": 0.9
});
  
//app.dynLayer.setVisibleLayers([0]);  
  
var optionsArray = [];
var drawingOptions = new esri.layers.LayerDrawingOptions();
drawingOptions.showLabels = false;
//drawingOptions.renderer = renderer;
optionsArray[0] = drawingOptions;
app.dynLayer.setLayerDrawingOptions(optionsArray);
app.map.addLayer(app.dynLayer);
0 Kudos
MarkHoover
Occasional Contributor II
Your snippet helped me pick out one error with my code but unfortunately it's still not working.

You mentioned the sample service needed "the option to render layers dynamically".  Is that a setting I need to turn on when publishing out this map service?  If so, do you know where I set that?
0 Kudos
JohnGravois
Frequent Contributor
if the option is enabled, you will see "Supports Dynamic Layers: true" in the REST endpoint for the map service. 

in order to set this property when publishing a service, you need to toggle "Allow per request modification of layer order and symbology" box within the Mapping capability.
MarkHoover
Occasional Contributor II
Perfect, that's what I was missing.  Thanks so much!
0 Kudos