【问题标题】:How to print a specific extent of an ArcGis Map?如何打印 ArcGis 地图的特定范围?
【发布时间】:2015-07-15 15:19:46
【问题描述】:

我正在尝试使用 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 是异步的并返回一个延迟的,我必须使用 'then' 方法。

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

  • 还有其他打印地图特定范围的方法吗?
  • 如果不是,为什么永远不会调用 'then' 方法?

(我使用的是 3.12 JS API)

【问题讨论】:

  • 外观看起来不错,除了您使用的是已弃用的非 AMD 样式。您是否尝试在该函数中设置断点?
  • 是的,我还添加了一个 try/catch。没有错误,但从未调用过“then”部分。

标签: gis arcgis esri arcgis-js-api


【解决方案1】:

您的代码在我看来不错,但显然您没有发布所有的 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>

【讨论】:

  • 你的代码和我的唯一区别是,当我点击一个按钮时,所有的代码都会被调用,而且我的地图有好几层......
  • 我用 require 替换了我的 dojo.addOnLoad() ,就像您在示例中所做的那样,并删除了我的所有层,现在它正在工作...
  • 你的小提琴代码帮了大忙。在我的应用程序中,我创建了一个用于打印的地图并将其隐藏,因此我的打印输出与可查看的地图范围无关。
猜你喜欢
  • 2013-06-15
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 2021-12-25
  • 2013-04-15
  • 1970-01-01
相关资源
最近更新 更多