ArcGIS API for JavaScript之地图打印

简单粗暴,直接上代码!!!(3.23的API)

补充:https://github.com/shaoms/ArcGIS-API-for-JavaScript/tree/master/Print 这里面有我写的4.xapi打印功能

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>地图打印</title>

   <link rel="stylesheet" href="https://js.arcgis.com/3.23/dijit/themes/claro/claro.css">
   <link rel="stylesheet" href="https://js.arcgis.com/3.23/esri/css/esri.css">
   <script src="https://js.arcgis.com/3.23/"></script>
    <link rel="stylesheet" type="text/css" href="../css/mystyle.css" />

    <script>
        require([
            "esri/layers/ArcGISDynamicMapServiceLayer",
            "esri/map","esri/config",
            "esri/tasks/PrintTask",  
            "esri/tasks/PrintTemplate",  
            "esri/tasks/PrintParameters",  
            "dojo/domReady!"
        ], function(ArcGISDynamicMapServiceLayer, Map, esriConfig, PrintTask, PrintTemplate, PrintParameters){
            var lyr = new ArcGISDynamicMapServiceLayer(     "https://sms.esrichina.com/server/rest/services/test/22/MapServer");  
            var mymap = new Map("viewDiv", {
                basemap: "osm",
                zoom: 9,  
                center: [116.402544,39.915378] 
            });
            mymap.addLayer(lyr);
            var button = document.getElementById("button");  
            button.onclick = function(){  
                    var printTask = new PrintTask("https://sms.esrichina.com:6443/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task");  
                    var template = new PrintTemplate();  
                    template.exportOptions = {  
                        width: 800,  
                        height: 600,  
                        dpi: 96  
                    };  
                    template.format = "PDF";  
                    template.layout = "A3 Portrait";  
                    template.preserveScale = false;  
                    var params = new PrintParameters();  
                    params.map = mymap;  
                    params.template = template;  
                    printTask.execute(params, function(evt){  
                       console.log(evt);
                        window.open(evt.url,"_blank");  //在新的标签页打开打印连接
                    });  
                }  
        });
    </script>

</head>
<body>
<div id="viewDiv"></div>
<button id="button" title="打印">打印</button>
</body>
</html>

  值得注意的是打印的地图服务需要公有的(共享给everyone),因为通过打印服务工具来间接访问地图服务时,需要用token,此token都是通过map中服务的token直接传递过来。你也可以打印包含受保护服务的地图,详细操作见下面的连接:
http://enterprise.arcgis.com/zh-cn/server/10.3/create-web-apps/windows/printing-maps-that-contain-secured-services.htm

猜你喜欢

转载自blog.csdn.net/smss007/article/details/79320440