Select to view content in your preferred language

PrintTask and the legendLayers option

1387
3
01-22-2013 08:13 AM
JonathanKressin
Regular Contributor
Hello,

I have an application that is consuming multiple dynamic map services.  The print functionality is working correctly at creating maps out of these services.  I am just having a bit of trouble with the legendLayers option.  If I don't set it, the legend in my template shows legend items for all visible layers.  I want to cut this down to a specific subset of layers.  I can generate an array of LegendLayer items, but I don't get anything in the legend when I go this route.  I am guessing what I am doing wrong is setting the layerId to the name as listed in the mxd.  What value should I use for this ID when using multiple dynamic services.

To not get too complicated, but here is a snippet of the code showing what I am trying to do.  The rest of the template information is configured earlier on:


print_map: function (template, title) {
        var params = new esri.tasks.PrintParameters();
        var legend_layers = this.get_legend_layers();
        params.map = this.map;
        for (var x = 0; x < this.templates.length; x++) {
            if (this.templates.label == template)
                params.template = this.templates;
        }
        params.template.layoutOptions.titleText = title;
        params.template.layoutOptions.legendLayers = legend_layers;
        var printtask = new esri.tasks.PrintTask(this.printService, params);
        printtask.execute(params, function (result) {
            console.log(result);
            var mywindow = window.open(result.url);
        }, function (err) { alert("Error printing map"); });
    },


and the get_legend_layers function works like this:
   
get_legend_layers: function () {
        var legend_layers = Array();
        for (var x in this.defaults) {
            // Interate thru the application configuration structure (index is service id, contains info on service and individual layers)
            // Did we go in or out of scale
            var myservice = this.layers;
            if (myservice != null && myservice.visible == true) {
                var vis_layers = this.layers.visibleLayers;
                //iterate thru this services visible layers
                for (var y = 0; y < vis_layers.length; y++) {
                    var name = this.layers.layerInfos[vis_layers].name.toString()
                    //console.log(name);
                    // Check with configuration settings that this layer is something we want in the legend
                    if (this.defaults[name].is_listed == true && this.defaults[name].attribute_list != "") {
                        var ll = new esri.tasks.LegendLayer();
                        console.log(name);
                        ll.layerId = name;
                        legend_layers.push(ll);
                    }
                }
            }
        }
        console.log(legend_layers);
        return (legend_layers);
    }


Thanks for any advice you can provide on this.

Jonathan
0 Kudos
3 Replies
JonathanKressin
Regular Contributor
Started working on something else, and then figured out what I was doing wrong.  The layerId is actually the service ID, the sublayers are the actual listing of layers on the service.  I was making this way harder then it needed to be.

Jonathan
0 Kudos
BeauDealy
Deactivated User
We had just run into the same problem. The Javascript API documentation is really ambiguous here, and kinda makes you think that you should be looking at individual layers in the service. That said, it makes more sense after the fact; especially in situations where folks might be calling from more than one dynamic service (but who does that, right?).  Thanks so much for steering us straight! Beer's on us if you're ever in Hays, KS. 🙂
0 Kudos
JonathanKressin
Regular Contributor
I'm glad to hear this helped you.  I am finding so far with my application that the print functionality has given me some of the most difficulty.

For anyone else reading this thread, I have one more question related to the legend:

Does anyone have any idea if it is possible to somehow control the columns of the items being added to the legend?  We have a print template we'd like to use that has the legend display horizontally.  This template looks great with sample data added to the mxd -- not so much when used as a print template.

Jonathan
0 Kudos