Select to view content in your preferred language

Legend with esri.tasks.PrintTask

1774
2
09-11-2012 11:49 AM
MichaelPorter1
Occasional Contributor
I'm having problems getting the print Task to add a legend consistently.

The code I use is shown below

    function printMap (legendLayers) {
            var printTemplate = new esri.tasks.PrintTemplate();
            printTemplate.format = "JPG";
            printTemplate.layout = "Letter ANSI A Landscape";
            var legendLayer = new esri.tasks.LegendLayer();
// layer with "dynamicLayer" id points to http://sampleserver6.arcgisonline.com/arcgis/rest/services/Military/MapServer
// and has layers 2,3,4,5 visible in the map
            legendLayer.layerId = "dynamicLayer";
            legendLayer.subLayerIds = [0,1,2,3,4,5];
            var layoutOptions = {TitleText: "Test Map",    scalebarUnit: 'Miles', legendLayers: [legendLayer]};
            printTemplate.layoutOptions = layoutOptions;
            printTemplate.preserveScale = true;
           
            var printParams = new esri.tasks.PrintParameters();
            printParams.map = map;
            printParams.outSpatialReference = map.spatialReference;           
            printParams.template = printTemplate;
           
            var printURL = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export Web Map Task";
            var printTask = new esri.tasks.PrintTask(printURL);
            printTask.execute(printParams, function(result) {
                window.open(result.url + "?timestamp="+(new Date().getTime()));
            });
     }

Full example is in the attached file.  It is important that the solution supports group layers.

Any help is greatly appreciated.

Thanks,

Mike
0 Kudos
2 Replies
JohnGravois
Deactivated User
when comparing the JSON the API sends as a "Web_Map_as_JSON" parameter for normal layers (which generate a legend swatch) and layers within group layers (which do not), they look identical.  this leads me to believe that the problem displaying patches is occurring server side.


working
"legendOptions" : {
   "operationalLayers" : [{
     "id" : "Boundaries",
     "subLayerIds" : [1]
    }


not working
"legendOptions" : {
   "operationalLayers" : [{
     "id" : "Boundaries",
     "subLayerIds" : [8]
    }
   ]
  }


if you haven't already, can you confirm the same?  if so, the ArcGIS Server general forum might be a good next step...
0 Kudos
JohnGravois
Deactivated User
i took another look at this after you replied to jian's thread and was able to adapt your sample to get legend swatches for "Hostile Units" to display using the following code

//add only a layer which is embedded in a single group layer
dynamicMapServiceLayer.setVisibleLayers([6]);
//...
//make sure both the group layer and sublayer are referenced in the subLayerIds
legendLayer.subLayerIds = [0,6]


for the the Lansing river and Areas layer (which is embedded in a group layer inside another group layer) i was able to get swatches using the following
dynamicMapServiceLayer.setVisibleLayers([8]);
//...
legendLayer.subLayerIds = [0,7,8]


i'm not positive, but i think your problems with layers 2,3,4,5 is that the dozens and dozens of symbols won't fit in the space allocated to the legend
0 Kudos