arcgis for js实现地图截图、地图打印功能

 更新时间:2024年12月21日 12:13:15   作者:我不当帕鲁谁当帕鲁  
这篇文章主要介绍了arcgis for js实现地图截图、地图打印功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧

地图截图

效果

实现

复制运行即可

要实现复杂的截图保存可以参考 官网案例

<!DOCTYPE html>
<html lang="zn">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #mapview {
        width: 100vw;
        height: 100vh;
      }
      * {
        margin: 0;
        padding: 0;
      }
      #btn {
        position: fixed;
        right: 30px;
        top: 10px;
        z-index: 999;
        width: 100px;
        height: 40px;
        cursor: pointer;
      }
    </style>
    <link rel="stylesheet" href="https://js.arcgis.com/4.23/esri/themes/light/main.css" rel="external nofollow"  rel="external nofollow"  />
    <script src="https://js.arcgis.com/4.23/"></script>
  </head>
  <body>
    <button id="btn">地图截图</button>
    <div id="mapview"></div>
    <script>
      require(['esri/Map', 'esri/views/MapView', 'esri/layers/FeatureLayer'], function (
        Map,
        MapView,
        FeatureLayer
      ) {
        // 初始化底图
        window.map = new Map({
          basemap: 'dark-gray-vector'
        })
        // 创建2维视图
        let view = new MapView({
          container: 'mapview',
          map: map,
          zoom: 11,
          center: [104.783916597735, 32.55699155692144] // 设置地图的初始化中心点坐标
        })
        document.querySelector('#btn').addEventListener('click', function () {
          view
            .takeScreenshot({
              area: {
                x: 0,
                y: 0,
                width: view.width,
                height: view.height
              },
              format: 'png'
            })
            .then(screenshot => {
              // 直接下载
              const base64 = screenshot.dataUrl.toString() // imgSrc 就是base64哈
              const byteCharacters = atob(base64.replace(/^data:image\/(png|jpeg|jpg);base64,/, ''))
              const byteNumbers = new Array(byteCharacters.length)
              for (let i = 0; i < byteCharacters.length; i++) {
                byteNumbers[i] = byteCharacters.charCodeAt(i)
              }
              const byteArray = new Uint8Array(byteNumbers)
              const blob = new Blob([byteArray], {
                type: undefined
              })
              const aLink = document.createElement('a')
              aLink.download = '图片名称.jpg' //这里写保存时的图片名称
              aLink.href = URL.createObjectURL(blob)
              aLink.setAttribute('crossOrigin', 'anonymous')
              aLink.click()
            })
        })
      })
    </script>
  </body>
</html>

地图打印

使用arcgis自带的打印组件,可自选导出格式,方向等等

缺点是如果地图上有MapImageLayer等图层就会失效

效果

实现

<!DOCTYPE html>
<html lang="zn">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #mapview {
        width: 100vw;
        height: 100vh;
      }
      * {
        margin: 0;
        padding: 0;
      }
      #btn {
        position: fixed;
        right: 30px;
        top: 10px;
        z-index: 999;
        width: 100px;
        height: 40px;
        cursor: pointer;
      }
    </style>
    <link rel="stylesheet" href="https://js.arcgis.com/4.23/esri/themes/light/main.css" rel="external nofollow"  rel="external nofollow"  />
    <script src="https://js.arcgis.com/4.23/"></script>
  </head>
  <body>
    <button id="btn">地图打印</button>
    <div id="mapview"></div>
    <script>
      require([
        'esri/Map',
        'esri/views/MapView',
        'esri/layers/FeatureLayer',
        'esri/Graphic',
        'esri/widgets/Print'
      ], function (Map, MapView, FeatureLayer, Graphic, Print) {
        // 初始化底图
        window.map = new Map({
          basemap: 'dark-gray-vector'
        })
        // 创建2维视图
        let view = new MapView({
          container: 'mapview',
          map: map,
          zoom: 11,
          center: [104.783916597735, 32.55699155692144] // 设置地图的初始化中心点坐标
        })
        document.querySelector('#btn').addEventListener('click', function () {
          const print = new Print({
            view: view,
            label: '提取',
            // 最好指定为自己的打印服务
            printServiceUrl: 'https://utility.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task'
          })
          // 将小部件添加到视图的右下角
          view.ui.add(print, 'bottom-right')
        })
      })
    </script>
  </body>
</html>

注意

打印服务最好使用自己的arcgisServer服务, 如何开启自行百度,当然这种事直接呼叫公司gis工程师咯

到此这篇关于arcgis for js实现地图截图、地图打印的文章就介绍到这了,更多相关arcgis for js地图截图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论