Dynamically update custom text element for print template layout

2036
12
11-25-2016 01:59 PM
NhuMai
by
New Contributor II

I'm working on setting custom text elements via the template layout options for a print task to be used in a print widget. I am trying to get the custom text to display the lat long coordinates of the current map extent.

I followed this sample to get the widget up and going. However, it sets up the template layouts and starts up the print widget when the app is loaded. This means that my "current" map extent for the layout is the initial map extent. Any suggestions on how to get the template custom text element to update to the current map extent? For the moment, I'm thinking that I have to set up and start up the print widget every time the extent changes...

Here is what I have so far:

require([
"esri/layers/FeatureLayer",
"esri/dijit/Print",
"esri/tasks/PrintTemplate",
"esri/request",
"esri/config",
"dojo/_base/array",
"dojo/dom",
], function(
FeatureLayer,
Print, PrintTemplate,
esriRequest, esriConfig,
arrayUtils, dom
) {

//var printUrl = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task";
var printUrl = "http://ourserver/arcgis/rest/services/Our_Tools/Our_Print_Tool/GPServer/Exporter%20une%20carte%20Web";

esriConfig.defaults.io.proxyUrl = "http://ourserver/Java/proxy.jsp";

dojo.connect(window.myMap, "onExtentChange", showExtent);

// get print templates from the export web map task
var printInfo = esriRequest({
"url": printUrl,
"content": {
"f": "json"
}
});
printInfo.then(handlePrintInfo, handleError);

var center = "";
var latLong = "";

function handlePrintInfo(resp) {
var layoutTemplate, templateNames, mapOnlyIndex, templates;

showExtent(window.myMap.extent)

layoutTemplate = arrayUtils.filter(resp.parameters, function(param, idx) {
  return param.name === "Layout_Template";
});

if ( layoutTemplate.length === 0 ) {
console.log("print service parameters name for templates must be \"Layout_Template\"");
return;
}
templateNames = layoutTemplate[0].choiceList;

// remove the MAP_ONLY template then add it to the end of the list of templates
mapOnlyIndex = arrayUtils.indexOf(templateNames, "MAP_ONLY");
if ( mapOnlyIndex > -1 ) {
var mapOnly = templateNames.splice(mapOnlyIndex, mapOnlyIndex + 1)[0];
templateNames.push(mapOnly);
}

// create a print template for each choice
templates = arrayUtils.map(templateNames, function(ch) {
var plate = new PrintTemplate();
plate.layout = plate.label = ch;
plate.format = "PDF";
plate.layoutOptions = {
customTextElements : [
{
centerLatLong: latLong,
centerXY: center
}
]
};
return plate;
});

// create the print dijit
printer = new Print({
"map": window.myMap,
"templates": templates,
url: printUrl
}, dom.byId("print_button"));
printer.startup();
}

function showExtent(extent) {
var lat = window.myMap.extent.getCenter().getLatitude().toFixed(2);
var long = window.myMap.extent.getCenter().getLongitude().toFixed(2);

latLong = "Latitude : " + lat + " - Longitude : " + long;

var X = window.myMap.extent.getCenter().x.toFixed(2);
var Y = window.myMap.extent.getCenter().y.toFixed(2);

center = "X : " + X + " - Y : " + Y;

console.log(latlong);
console.log(center);
}

function handleError(err) {
console.log("Something broke: ", err);
}
});









Tags (2)
0 Kudos
12 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Quynh,

   Change:

window.myMap.on('extent-change', showExtent());

to:
window.myMap.on('extent-change', showExtent);
0 Kudos
Quynh_NhuMai
New Contributor III

Ah whoops, okay--so that fixed the mentioned error.

The printer now fails to reset the print layout list. No errors given, but the dropdown menu disappears and printing gives the map only output. I wonder if has to do with the way this line runs:

printer.templates = [plate];
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Quynh,

  In your code the plate var is only going to be the last print template that you worked with in your

templates = arrayUtils.map(templateNames, function(ch) {
loop
0 Kudos