AnsweredAssumed Answered

Prevent invalid legend layers (e.g., WebTiledLayer) from being sent to legend widget

Question asked by azendel on Dec 12, 2013
Latest reply on Oct 21, 2014 by ajmorris
I'm working off of this sample using the 3.7 API.  I added my own FeatureLayer's to the map and the sample ran flawlessly.  But whenever I introduce a WebTiledLayer, the feature layers are not added to the map or the legend widget.  This makes total sense because WebTiledLayer's don't have a legend.  So how do I prevent the invalid layers from being sent to the legend widget?

I understand that the layerInfo variable lists the layers that should be sent to the legend and that only valid layers should be included.  I tried several ways to filter out the WebTiledLayer's in the following block, but nothing worked.

      //add the legend
      map.on("layers-add-result", function (evt) {
        var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
          return {layer:layer.layer, title:layer.layer.name};
        });
        if (layerInfo.length > 0) {
          var legendDijit = new Legend({
            map: map,
            layerInfos: layerInfo
          }, "legendDiv");
          legendDijit.startup();
        }
      });



I tried assigning id's to the FeatureLayers's and then filtering those that don't have a matching id:

 if ((layer.layer.id == "tracts") || (layer.layer.id == "counties")) {
                  return { layer: layer.layer, title: layer.layer.name };
              }


No luck.  Ideally, I'd like to check whether or not a layer is a featureLayer and if it's not, exclude it from layerInfo.  My final app will have quite a number of featureLayers and I don't want to have to check the ID for all of them.  Another potential hitch is that I'll be using a select (html dropdown) element to turn certain featureLayer's on and off via featureLayer.show() and featureLayer.hide().  So I'll somehow need to check whether or not a layer is visible and then send it to the legend if it is.  I think there's probably no way to do this without checking the layer name/id somehow.

Thanks for any suggestions!

Outcomes