如何打印 ArcGis 地图的特定范围?

How to print a specific extent of an ArcGis Map?

我正在尝试使用 JS API(不是显示的扩展)在 Arcgis 地图上打印特定区域。

我没有找到执行此操作的任何方法或选项,所以我尝试更改扩展然后打印地图:

var extent = new esri.geometry.Extent(
    -620526.0922336339, 
    5993991.149960931, 
    108988.90572005256, 
    6293624.300838808, 
    myMap.spatialReference
);

myMap.setExtent(extent, true).then(function() {
    console.log('setExtend is finished');

    var template = new esri.tasks.PrintTemplate();
    template.exportOptions = {
        width : 500,
        height : 500
    };
    template.format = 'jpg';
    template.layout = 'MAP_ONLY';

    var params = new esri.tasks.PrintParameters();
    params.map = myMap;
    params.template = template;

    var printTask = new esri.tasks.PrintTask(urlToThePrintServer);
    printTask.execute(params);
});

由于 setExtent 是异步的并且 return 是延迟的,所以我必须使用 'then' 方法。

我可以看到地图在移动,但延迟似乎不起作用...(我没有看到 console.log())。

(我使用的是 3.12 JS API)

你的代码对我来说看起来不错,但显然你没有 post 全部 JavaScript 或任何 HTML。也许您不需要所需的模块。或者您的代码可能在加载地图之前尝试 运行,尽管这不太可能,因为正如您所说,地图确实会移动。或者可能是其他问题。

我在 http://jsfiddle.net/06jtccx0/ 放了一个完整的工作示例。希望您可以将其与您正在做的事情进行比较,并找出您的代码有什么问题。为方便起见,这里是相同的代码:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css">
    <style>
      html, body, #map {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
      }
      body {
        background-color: #FFF;
        overflow: hidden;
        font-family: "Trebuchet MS";
      }
    </style>
    <script src="http://js.arcgis.com/3.13/"></script>
    <script>
      var myMap;
      var urlToThePrintServer = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task";

      require(["esri/map", "dojo/domReady!"], function(Map) {
        myMap = new Map("map", {
          basemap: "topo",  //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
          center: [-122.45, 37.75], // longitude, latitude
          zoom: 13
        });
        myMap.on("load", function(map) {
          var extent = new esri.geometry.Extent(
              -620526.0922336339, 
              5993991.149960931, 
              108988.90572005256, 
              6293624.300838808, 
              myMap.spatialReference
          );

          myMap.setExtent(extent, true).then(function() {
            console.log('setExtend is finished');
            require([
                "esri/tasks/PrintTemplate",
                "esri/tasks/PrintParameters",
                "esri/tasks/PrintTask"
                ], function(
                  PrintTemplate,
                  PrintParameters,
                  PrintTask
                  ) {

              var template = new PrintTemplate();
              template.exportOptions = {
                  width : 500,
                  height : 500
              };
              template.format = 'jpg';
              template.layout = 'MAP_ONLY';

              var params = new PrintParameters();
              params.map = myMap;
              params.template = template;

              var printTask = new PrintTask(urlToThePrintServer);
              printTask.execute(params, function(response) {
                console.log("The printed document is at " + response.url);
                window.open(response.url);
              });
            });
          });
        });
      });
    </script>
  </head>

  <body>
    <div id="map"></div>
  </body>
</html>