Select to view content in your preferred language

Control which basemap shows on pdf using printTask

1661
11
06-05-2014 10:39 AM
AndrewMosley
Emerging Contributor
Hi there,

I have a printTask that works wonderfully except for one
unfortunate bit that I cannot seem to figure out or find any resources on.

I have two ArcGISTiledMapServiceLayers that function as basemaps; one of which is imagery on our arcgisonline server,
the other is ESRIs world Street Map.
The world street map displays until the user is zoomed in beyond a certain extent, then the imagery displays beyond that.

However, when executing printTask, the imagery always appears on top of the world street map, regardless of what is
currently being displayed.
Basically, I just want the printTask to print whatever basemap is currently displaying.

Any suggestions/thoughts/ideas would be most helpful. I've attached a screenshot of what's being output.

Thanks!
0 Kudos
11 Replies
JeffPace
MVP Alum
It sounds like you are using LODs to toggle basemap, but not setting the visibility.  The printtask seems to be ignoring the LOD constraint, and since both are technically .visible=true, they are both displaying

Can you show your code that controls your basemap visibility?
0 Kudos
AndrewMosley
Emerging Contributor
You're correct- I am using LODs to control the visibility of the imagery. Here's the snippet:

var lods = [{
        "level" : 0,
        "resolution" : 156543.033928,
        "scale" : 591657527.591555
    }, {
        "level" : 1,
        "resolution" : 78271.5169639999,
        "scale" : 295828763.795777
    }, {
       etc etc
    }, {
       "level" : 19,
        "resolution" : 0.298582141647617,
        "scale" : 1128.497176
    }, {
        "level" : 20,
        "resolution" : 0.1492910708237085,
        "scale" : 564.248588125
    }];

       tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/Worl        d_Street_Map/MapServer");
       hcBasemap = new esri.layers.ArcGISTiledMapServiceLayer("http://www.blahblahblah.org/ArcGIS10/rest/services/Cache/Cache        Aerials2010/MapServer", {
             visible : false
    });
      hcBasemapDynamic = new esri.layers.ArcGISDynamicMapServiceLayer("http://www.blahblahblah.org/ArcGIS10/rest/services/Ca      che/CacheAerials2010/MapServer", {
             id : "Imagery_Dynamic",
             minScale : 564.248588125
    });


and the printMap function for good measure:

function printMap(label, author, copyright, format, layout, scaleunit) {

    var layersToPrint = esri.tasks.LegendLayer();
    layersToPrint.layerId = "BlahBlahBlah";
    layersToPrint.subLayerIds = [14, 15, 16, 17, 18, 19, 21, 24, 27, 28, 29, 30];
    params = new esri.tasks.PrintParameters();
    var template = new esri.tasks.PrintTemplate();
    template.exportOptions = {
        width : 750,
        height : 1000,
        dpi : 96
    };
    template.layoutOptions = {
        copyrightText : copyright,
        scalebarUnit : scaleunit,
        titleText : label,
        authorText : author,
        legendLayers : [layersToPrint]
    };
    template.format = format;
    template.layout = layout;
    template.preserveScale = true;

    params.template = template;

    params.map = map;
    showLoader();
    printTask.execute(params, function(result) {
        hideLoader();
        window.open(result.url, '_blank');
        window.focus();
    });
}
0 Kudos
JeffPace
MVP Alum
ok, i dont see where you turn the streets off and switch to the imagery
0 Kudos
AndrewMosley
Emerging Contributor
Also, if i set the visibility to false on the imagery layer, it behaves as expected and does not appear on the printTask.
However, this of course also causes it to not display on the map.

Is there a better way to toggle which basemap is being displayed to the user that also allows setting visibility so
that the printTask executes properly?

Maybe something to the effect of:
if (scale => 564) {
visibility : false
} else {
visibility : true
};?
0 Kudos
AndrewMosley
Emerging Contributor
Streets are never technically turned off, rather stacked on top of and the minScale of imagery is handled.
0 Kudos
JeffPace
MVP Alum
i would suggest using a
map.on('extent-change', function(delta, extent, levelChange, LOD){
if(levelChange){
if(LOD>16){
//set streets visible, set imagery off
}else{
//opposite
}
}
})


or something similiar
0 Kudos
AndrewMosley
Emerging Contributor
I've got this:
map.on('extent-change', function(delta, extent, levelChange, lods){
        if(levelChange){
            if(lods.level>17) {
                hcBasemapDynamic visibile: true,
                tiledMapServiceLayer visible : false
            } else {
                hcBasemapDynamic visible : false,
                tiledMapServiceLayer visible : true
            }
        }
    });


I'm struggling with how to write the set visible/not portion.
0 Kudos
JeffPace
MVP Alum
try

map.getLayer("layerId").show();
map.getLayer("layerId").hide();

you will have to add an id to tiledMapServiceLayer  otherwise it will end up as layer_0, etc..

so

map.on('extent-change', function(delta, extent, levelChange, lods){
        if(levelChange){
            if(lods.level>17) {
                map.getLayer("Imagery_Dynamic").show();
                map.getLayer("tiledMapServiceLayerId").hide();
            } else {
                map.getLayer("Imagery_Dynamic").hide();
                map.getLayer("tiledMapServiceLayerId").show();
            }
        }
    });


0 Kudos
AndrewMosley
Emerging Contributor
So I'm not sure what you mean by give the layer an ID to fetch by but I tried passing just the variable name with:

map.on('extent-change', function(delta, extent, levelChange, lods){
        if(levelChange){
            if(lods.level>17) {
                map.getLayer("hcBasemapDynamic").show();
                map.getLayer("tiledMapServiceLayer").hide();
            } else {
                map.getLayer("tiledMapServiceLayer").show();
                map.getLayer("hcBasemapDynamic").hide();
            }
        }
    });


This actually hid both layers (which I assume has to do with the ID thing) but more troubling is when trying the printing function, it still printed as before! (Screenshot attached- Left is the gis app, Right is the print pdf)
Another question about this- I currently have this bit inside my map init function. In its own function it had 0 impact so I assume this is the correct location?
0 Kudos