Skip to content

Instantly share code, notes, and snippets.

@JimBlaney
Last active October 13, 2015 17:41
Show Gist options
  • Save JimBlaney/8195418679915c52e519 to your computer and use it in GitHub Desktop.
Save JimBlaney/8195418679915c52e519 to your computer and use it in GitHub Desktop.
ArcGIS API for JavaScript snippet to print current map in new window
var parentMap = this.map;
require([
'dojo/_base/window',
'dojo/on',
'dojo/dom-construct',
'esri/map',
'esri/layers/ArcGISDynamicMapServiceLayer',
'esri/layers/ArcGISTiledMapServiceLayer',
'esri/layers/GraphicsLayer'
], function(
win,
on,
domConstruct,
Map,
ArcGISDynamicMapServiceLayer,
ArcGISTiledMapServiceLayer,
GraphicsLayer
) {
// create a new window
var childWin = window.open('about:blank', '_blank');
// wait for the window to finish loading
on.once(childWin, 'load', function() {
win.withDoc(childWin.document, function() {
// update the title to something other than 'about:blank'
win.doc.title = 'Print Map';
// inject the Esri stylesheet so the map will look like it's supposed to
domConstruct.create('link', {
rel: 'stylesheet',
href: require.toUrl('esri/css/esri.css')
}, win.doc.getElementsByTagName('HEAD')[0]);
// create a DOMNode to contain the map
domConstruct.create('div', {
id: 'printMap',
style: {
position: 'fixed',
top: 0, left: 0,
display: 'block',
width: '960px', height: '720px', // 96 dpi w/ 0.5 margins
backgroundColor: '#ccc'
}
}, win.body());
// create the map
var map = new Map('printMap', {
logo: false,
showAttribution: false,
slider: false,
wrapAround180: true,
extent: parentMap.extent,
zoom: parentMap.getZoom()
});
// fix the map properties
map.on('load', function() {
map.disableMapNavigation();
map.setTimeExtent(parentMap.timeExtent);
});
// make a copy of each layer in the map
var parentLayer = null,
childLayers = [];
for (var i = 0, il = parentMap.layerIds.length; i < il; i++) {
parentLayer = parentMap.getLayer(parentMap.layerIds[i]);
if (parentLayer.isInstanceOf(ArcGISDynamicMapServiceLayer)) {
childLayers.push(new ArcGISDynamicMapServiceLayer(parentLayer.url, {
resourceInfo: parentLayer.getResourceInfo();
}))
} else if (parentLayer.isInstanceOf(ArcGISTiledMapServiceLayer)) {
childLayers.push(new ArcGISTiledMapServiceLayer(parentLayer.url, {
resourceInfo: parentLayer.getResourceInfo();
}))
}
}
for (var i = 0, il = parentMap.graphicsLayerIds.length; i < il; i++) {
parentLayer = parentMap.getLayer(parentMap.graphicsLayerIds[i]);
// TODO implement graphic and renderer/symbol copy
}
// TODO listen for layer load events and display some UI to indicate that layers are still loading
map.addLayers(childLayers);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment