LabelExpressionInfo not honoring where clause

6551
23
07-14-2015 01:20 PM
DavidColey
Frequent Contributor

Hello-

So I'm trying to override the default feature layer labels, but the where clause isn't being honored.  For my map constructor options:

showLabels : true

My LabelingInfo:

var labelArray = [
  {
  labelPlacement: "esriServerPointLabelPlacementAboveRight",
  labelExpression: "[AddNumber]",
  symbol: {
    type: "esriTS",
      color: [255,0,0,255], //[38,115,0,255],
      font: {
      family: "Arial",
      size: 9,
      style: "italic",
      weight: "bold",
      decoration: "none",
    }
    },
    minScale: 2401,
    maxScale: 0,
    where: "AddressStatus LIKE 'A%'"
  }
  ];
  console.log(labelArray);

and finally for the feature layer:

lyrAddresses.setLabelingInfo(labelArray);

All of the other constructor options for the labelng info are being honored, but wheneverI try to invoke the where clause, none of the points will label-

Thanks

David

Tags (3)
0 Kudos
23 Replies
RobertScheitlin__GISP
MVP Emeritus

Thejus,

   That is exactly what I was thinking why would they provide a setLabelingInfo method because I have tried many time to get it to work but have been unsuccessful.

0 Kudos
DavidColey
Frequent Contributor

Right, see I think it's a bug in that the API reference for the labeling info property on a feature layer reads -

'Label definition for this layer, specified as an array of label classes. Use this property to specify any changes to the structure of the label'.

For my particular need what I am doing is specifying one label class based on an AddressNumber and a second class based on an AddressStatus and then positioning the first at upper right and the second at lower right . . .

Thanks for looking into this all-

David

0 Kudos
DavidColey
Frequent Contributor

Actually this might not be so much a bug as an oversight(?) in that a where expression can't operate against a parent graphic class?

JssrRR
by
Occasional Contributor II

Hi David,

I came across this thread, where DSwingley of ESRI says

"Feature Layers don't support a layer's labels. When you create a feature layer from a layer in a map service, you get the geometry and attributes. If you want to label features, look into using a Text Symbol."

https://community.esri.com/message/48730#48730

Robertvan_Gilst
New Contributor III

Hi,

I have the same problem. When trying to update the json string to fx.:

var json = {

          "labelExpressionInfo": {"value": "{STATE_NAME}"},

          "where": "STATE_NAME = 'Texas'"

        };

in ArcGIS API for JavaScript Sandbox

No labels appear.

When I change the where to "STATE_NAME <> 'Texas'", everything has a label.

When I remove the space before < like "STATE_NAME<> 'Texas'", No labels appear.

My conclusion is that there is some kind of bug, or am I overseeing something?

Robert

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Robert,

   I agree it seems to be a bug. Here is a one workaround:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Labeling features client-side</title>
    <link rel="stylesheet"   href="https://community.esri.com//js.arcgis.com/3.15/esri/css/esri.css">
    <style>
      html, body, #map {
        height: 100%; width: 100%; margin: 0; padding: 0; 
      }
    </style>

   <script src="//js.arcgis.com/3.15/"></script>
    <script>
      var map;
    
      require([
        "esri/map", 
        "esri/geometry/Extent",
        "esri/layers/FeatureLayer",
        "esri/symbols/SimpleLineSymbol",
        "esri/symbols/SimpleFillSymbol",
        "esri/symbols/TextSymbol",
        "esri/renderers/SimpleRenderer",
        "esri/layers/LabelLayer",
        "esri/Color",
         
        "dojo/domReady!"
      ], function(Map, Extent, FeatureLayer,
                  SimpleLineSymbol, SimpleFillSymbol, 
                  TextSymbol, SimpleRenderer, LabelLayer, Color) 
      {
        // load the map centered on the United States
        var bbox = new Extent({"xmin": -1940058, "ymin": -814715, "xmax": 1683105, "ymax": 1446096, "spatialReference": {"wkid": 102003}});
      
        //create the map and set the extent, making sure to "showLabels"
        map = new Map("map", {
          extent: bbox,
          showLabels : true //very important that this must be set to true!   
        });

        // create a renderer for the states layer to override default symbology
        var statesColor = new Color("#666");
        var statesLine = new SimpleLineSymbol("solid", statesColor, 1.5);
        var statesSymbol = new SimpleFillSymbol("solid", statesLine, null);
        var statesRenderer = new SimpleRenderer(statesSymbol);
         
        // create the feature layer to render and label
        var statesUrl = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3";
        var states = new FeatureLayer(statesUrl, {
          id: "states",
          outFields: ["*"]
        });
        states.setRenderer(statesRenderer);


        var usaLblLayer = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3", {
          id: "us_states_lbl",
          opacity: 0.7,
          visible: true,
          outFields: ["*"]
        });
        usaLblLayer.setDefinitionExpression("STATE_NAME = 'Texas'");
        usaLblLayer.setRenderer(statesRenderer);
        map.addLayer(usaLblLayer);
        
        var statesLabel = new TextSymbol().setColor(new Color("#ff0000"));
        statesLabel.font.setSize("14pt");
        statesLabel.font.setFamily("arial");
        var renderer = new SimpleRenderer(statesLabel);
        var labelLayer = new LabelLayer({ id: "labels" });
        labelLayer.addFeatureLayer(usaLblLayer, renderer, "{STATE_NAME}");
        map.addLayer(labelLayer);
        map.addLayer(states);
    
      });
    </script>
  </head>
  <body>
    <div id="map"></div>
  </body>
</html>
Robertvan_Gilst
New Contributor III

Robert,

Thanks for the work-around. Unfortunately I cannot use it in my setup. Because I create the featurelayer I use on the fly and I wanted to have all setup regarding rendering i standard Esri notation in a configuration json file.

I found my another work-around. Because I create the layer on the fly, I can easily create a new string field, which I fill out when the data is read. If I do not want a label, I add an empty string, otherwise I add a value.

Not the prettiest solution, but it does the job.

Robert

UjjwalNigam
New Contributor III

Is there a bug number for this where clause issue?

It still does not work in v3.24

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Ujjwal,

   Here is a sample that shows how to do labeling with a where clause:

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  <title></title>

  <link rel="stylesheet" href="http://js.arcgis.com/3.24/dijit/themes/claro/claro.css">
  <link rel="stylesheet" href="https://js.arcgis.com/3.24/esri/css/esri.css" />
  <style>
    html,
    body {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }

    #map {
      margin: 0;
      padding: 0;
    }

  </style>

  <script src="https://js.arcgis.com/3.24/"></script>
  <script>
    var app = {};
    require([
        "esri/map", "esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/ArcGISDynamicMapServiceLayer",
        "esri/layers/LayerDrawingOptions", "esri/layers/LabelClass", "esri/symbols/TextSymbol", "esri/Color",
        "dojo/parser", "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!"
      ], function (
      Map, ArcGISTiledMapServiceLayer, ArcGISDynamicMapServiceLayer,
      LayerDrawingOptions, LabelClass, TextSymbol, Color, parser
    ) {
      parser.parse();

      app.dataUrl = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2";
      app.defaultFrom = "#ffffcc";
      app.defaultTo = "#006837";

      app.map = new Map("map", {
        center: [-85.787, 39.782],
        zoom: 5,
        slider: false
      });

      var basemap = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer");
      app.map.addLayer(basemap);
      var ref = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Reference_Overlay/MapServer");

      urlDyn = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer";
      usaLayer = new ArcGISDynamicMapServiceLayer(urlDyn, {
        id: "us_counties",
        opacity: 0.7,
        visible: false
      });
      usaLayer.setVisibleLayers([1,3]);
      app.map.addLayer(usaLayer);

      var optionsArray = [];
      var drawingOptions = new LayerDrawingOptions();
      var labelClass = new LabelClass({
        labelExpression: '[' +  "STATE_NAME" + ']',
        labelPlacement: 'esriServerPolygonPlacementAlwaysHorizontal',
        symbol: {
          "type": "esriTS",
          "color": [255,0,0],
          "haloColor": [0,245,255],
          "haloSize": 2,
          "font": {
            "family": "Arial",
            "size": "14px",
            "weight": "bold"
          }
        },
        maxScale: 840000,
        where: "STATE_NAME LIKE 'T%'"
      });

      drawingOptions.labelingInfo = [labelClass];
      drawingOptions.showLabels = true;

      optionsArray[3] = drawingOptions;
      app.map.getLayer("us_counties").setLayerDrawingOptions(optionsArray);
      app.map.getLayer("us_counties").show();
    });
  </script>
</head>

<body class="claro">
  <div data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline',gutters:false" style="width: 100%; height: 100%; margin: 0;">
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'">
    </div>
  </div>
</body>

</html>
UjjwalNigam
New Contributor III

Hi Robert,

The example you've shown is for a dynamic map service, while I am working

on a Feature layer.

In my code, though it does not throw any error, but the labels dont show up.

The same behavior can be seen in the ESRI sandbox, which you can access at:

https://developers.arcgis.com/javascript/3/sandbox/sandbox.html?sample=layers_label

This makes me believe that this is a possible bug in the Arcade labelling

engine, because ESRI claims to honor any "valid" SQL in the where clause.

Regards

Ujjwal

0 Kudos