基础功能---地图打印(踩坑、解决 详解)

踩坑

如果点击打印地图,没有反应也不报错。考虑一下几点
  1. ArcGIS Server中的地理处理服务没有开
  2. 浏览器阻止了弹出窗口
解决
  1. 运行服务
  2. 浏览器的弹出窗口,点击允许。如果没有弹出在浏览器设置 -> 网站设置 -> 弹出式窗口和重定向中打开允许,或者在允许中添加测试页面的地址。
  3. 当地图内存很大的时候,回调函数有可能会超时,这种情况下,会产生打印的pdf,执行回调函数中的内容需要等很久,具体时间视地图大小而定,因为已经响应超时。这种情况请继续看
  4. 打开控制台,查看资源,刷新页面,重新请求资源,点击打印,在最下面会增加两个资源
  5. 双击第二个资源,这是打印执行后服务器所返回的
  6. 出现这个,说明是成功了,但是弹出窗口还需要等待,可以直接点击value -> url打开pdf,这个就是返回的pdf的地址
  7. 这里报错状态码为500,说明是地理处理服务没有打开。打开后重新运行解决。
贴代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>地图打印</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.28/esri/css/esri.css" />
<script src="https://js.arcgis.com/3.28/"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<style type="text/css">
.MapClass{
width:100%;
height:500px;
border:1px solid #000;
}
</style>
<script type="text/javascript">

require(["esri/map",
"dojo/dom","dojo/on","dojo/query",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/symbols/SimpleMarkerSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/symbols/SimpleFillSymbol",
"esri/toolbars/draw",
"esri/graphic",
"esri/tasks/PrintTask",
"esri/tasks/PrintTemplate",
"esri/tasks/PrintParameters",
"dojo/colors",
"dojo/domReady!"],
function (Map,dom,on,query,
ArcGISDynamicMapServiceLayer,
SimpleMarkerSymbol,
SimpleLineSymbol,
SimpleFillSymbol,
Draw,
Graphic,
PrintTask,PrintTemplate,PrintParameters,
Color) {
var map = new esri.Map("mapDiv");
var layer = new esri.layers.ArcGISDynamicMapServiceLayer
("http://localhost:6080/arcgis/rest/services/ecology/beijing_2012_3/MapServer");
map.addLayer(layer);
//创建绘图对象
var toolBar = new Draw(map);
//线符号
lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new Color([255, 0, 0]), 3);
//面符号
polygonSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, lineSymbol, new Color([255, 0, 0, 0.25]));

//给绘制polygon的按钮绑定事件
query(".pbtn").on("click",function(event){
//激活绘制多边形
toolBar.activate(Draw.POLYGON, {
showTooltips:true
})

})
//绘图结束绑定事件
on(toolBar,"draw-end",function(result){
//获得面形状
var geometry=result.geometry;
//创建Graphic
var graphicpoint= new Graphic(geometry, polygonSymbol);
map.graphics.add(graphicpoint);
//关闭绘图工具
toolBar.deactivate();
})
//给地图打印按钮绑定事件
on(dom.byId("Btn"),"click",function(){
//创建地图打印对象
var printMap = new PrintTask("http://localhost:6080/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task");
//创建地图打印模版
var template = new PrintTemplate();
//创建地图的打印参数,参数里面包括:模版和地图
var params = new PrintParameters();
//输出图片的空间参考
printMap.outSpatialReference = map.SpatialReference
//打印图片的各种参数
template.exportOptions = {
width: 850,
height: 650,
dpi: 96
};
//打印输出的格式
template.format = "PDF";
//输出地图的布局
template.layout = "MAP_ONLY";
PrintTemplate
//设置参数地图
params.map = map;
//设置参数模版
params.template = template;
//运行结果
printMap.execute(params, function(result){
if (result != null) {
//网页打开生成的地图
window.open(result.url);
}
})
})

});
</script>
</head>
<body>
<div id="mapDiv" class="MapClass"></div>
<button class="pbtn" >画多边形</button>
<input id="Btn" type="button" value="地图打印" />
</body>
</html>