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

669
1
12-12-2013 11:59 AM
AZendel
Occasional Contributor III
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!
0 Kudos
1 Reply
deleted-user-Jie3eyjOl9XM
Occasional Contributor

I've run across the same problem. Rather than handle this in the layers-add-result event, you can build the layerInfos while you are adding the layers to the map.

First, create an empty array. Then, as you add each layer to the map, if the layer is not a WebTiledLayer, add a layerInfo element to that array. Finally, construct the Legend dijit, passing in your clean array of LayerInfos.

Ultimately, the lack of legend is a real shortcoming with WebTiledLayers. I'd like to see an alternate Legend dijit that lets me include a legend for my WebTiledLayers, too.

0 Kudos