arcgis api for javascript 學習(六) 地圖打印

一、本文應用arcgis api for javascript對發佈的動態地圖進行打印,打印的爲PDF格式,打印出來如圖:javascript

 

二、須要特別注意的是:咱們在運行代碼前,須要打開PrintingTools,如圖css

 

三、而且在啓動後,在URL中,須要修改一下,如圖:html

 

四、運行代碼,點擊打印如圖:java

五、代碼部分:api

<!DOCTYPE html>
<html>
<head>
    <title>地圖打印</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <link rel="stylesheet" href="https://js.arcgis.com/3.29/esri/css/esri.css">
    <script src="https://js.arcgis.com/3.29/"></script>
    <style>
        #map{
            position:relative;
            height:500px;
            width:100%;
        }
    </style>
</head>
<body>

<div id='map'>
</div>
<div>
    <input id="Btn" type="button" value="打印地圖PDF" />
</div>
<script>
    var map,sr,bool = false,initextent,url;
    require([
                "esri/map",
                "esri/layers/MapImage",
                "esri/layers/MapImageLayer",
                "esri/geometry/Extent",
                "esri/SpatialReference",
                "esri/tasks/PrintTask",
                "esri/tasks/PrintTemplate",
                "esri/tasks/PrintParameters",
                "esri/layers/ArcGISDynamicMapServiceLayer",
                "dojo/domReady!"],
            function (
                    Map,
                    MapImage,
                    MapImageLayer,
                    Extent,
                    SpatialReference,
                    PrintTask,
                    PrintTemplate,
                    PrintParameters,
                    ArcGISDynamicMapServiceLayer) {
                sr = new SpatialReference(4326)
                map = new Map("map", {
                    basemap:"satellite"
                });
                //調用動態地圖服務
                var DyLayer=new ArcGISDynamicMapServiceLayer('http://localhost:6080/arcgis/rest/services/dtchina/MapServer');
                map.addLayer(DyLayer);
                var btn=document.getElementById("Btn");
                btn.onclick=function(){
                    btn.setAttribute("disabled","true");
                    btn.value="正在輸出...";
                    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";
                    params.map = map;
                    params.template = template;
                    printMap.execute(params, function(result){
                        console.log(result);
                        if (result != null) {
                            (function(){
                                btn.disabled=false;
                                btn.value="打印地圖PDF";
                            })()
                            window.open(result.url);
                        }
                    })
                }
            });
</script>
</body>
</html>
相關文章
相關標籤/搜索