Vue.js2+ArcGIS3一、初始化地图

Github

ArcGIS

ArcGIS 官方文档

一、安装 ArcGIS 官方加载依赖 esri-loader

esri-loader 官网


npm install esri-loader --save-dev

二、初始化地图

InitMap.vue


<template>
  <div id="map-container"
       style="width:100%;height:100%;">
    <div id="HomeButton"></div>
    <div id="LocateButton"></div>
    <div id="navigation-toolbar"
         class="navigation-toolbar">
      <div class="nt-translation">
        <button id="resize"></button>
        <button id="east"></button>
        <button id="west"></button>
        <button id="south"></button>
        <button id="north"></button>
      </div>
      <div class="nt-zoom">
        <div class="rule"></div>
        <button id="site"></button>
        <button id="zoomUp"></button>
        <button id="zoomDowm"></button>
      </div>
    </div>
  </div>
</template>
<script>
import { loadCss, loadModules } from 'esri-loader'
export default {
  name: 'InitMap',
  data() {
    return {
      navToolbar: null,
      map: '',
      gisConstructor: {}, // gis 构造函数
      gisModules: [
        'esri/toolbars/navigation', // 地图导航工具
        'dojo/_base/declare',
        'esri/dijit/OverviewMap', // 小地图
        'esri/dijit/Scalebar', // 比例尺
        'esri/dijit/LocateButton', // 当前位置
        'esri/dijit/HomeButton', // 返回到地图默认开始范围
        'esri/layers/OpenStreetMapLayer', // OSM
        'esri/geometry/Extent', // 范围
        'esri/map',
      ],
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    /**
     * @name: 地图点击事件
     */
    mapClickFun() {
      this.map.on('click', e => {
        console.log(e)
      })
    },

    /**
     * @name: 初始化地图
     */
    init() {
      // 加载 css
      // loadCss('http://localhost/js.arcgis.com/3.32/esri/css/esri.css')
      loadCss('https://js.arcgis.com/3.32/esri/css/esri.css')
      // 加载模块
      loadModules(this.gisModules, {
        // url: 'http://localhost/js.arcgis.com/3.32/init.js'
        url: 'https://js.arcgis.com/3.32/',
      })
        .then(this.initMap)
        .then(this.mapClickFun)
    },
    initMap(args) {
      // 将 ArcGIS 的每个功能模块都存放到 gisConstructor 中
      for (let k in args) {
        let name = this.gisModules[k].split('/').pop()
        this.gisConstructor[name] = args[k]
      }
      /**
       * 添加 OSM 有两种方式
       * 方式一:在初始化 map 时设置 basemap 属性 basemap: 'osm'
       * 方式二:加载 OpenStreetMapLayer 模块,创建 OSM layer,再添加 layer 到 map 中,可单独对 OSM 进行配置
       */
      let basemap = new this.gisConstructor.OpenStreetMapLayer({
        id: 'OSMlayer',
        visible: true,
        opacity: 1,
        // displayLevels: [1, 2, 3] // 在指定缩放等级下显示
      })
      this.map = new this.gisConstructor.map('map-container', {
        // center: [116.395645038, 39.9299857781],
        // basemap: 'osm',
        zoom: 12,
        logo: false,
        slider: true,
      })
      this.map.addLayer(basemap)

      // 设置初始化范围
      let extent = {
        xmin: -117.1839455,
        ymin: 32.68087830000002,
        xmax: -117.15035189999998,
        ymax: 32.732100979999984,
      }
      /*
        如果坐标系是 4490,初始化范围需要设置空间参考坐标系
        new this.gisConstructor.SpatialReference({
          wkid: 4490
        })
      */
      this.map.setExtent(
        new this.gisConstructor.Extent(
          extent.xmin,
          extent.ymin,
          extent.xmax,
          extent.ymax
        )
      )

      // 返回到地图默认开始范围(现在是根据 center 设置的,如何根据 extent 设置?)
      // let home = new this.gisConstructor.HomeButton(
      //   {
      //     map: this.map,
      //   },
      //   'HomeButton'
      // )
      // home.startup()

      // 当前位置
      // let geoLocate = new this.gisConstructor.LocateButton(
      //   {
      //     map: this.map,
      //   },
      //   'LocateButton'
      // )
      // geoLocate.startup()

      // 比例尺
      // eslint-disable-next-line no-unused-vars
      let scalebar = new this.gisConstructor.Scalebar({
        map: this.map,
        // "dual" displays both miles and kilometers
        // "english" is the default, which displays miles
        // use "metric" for kilometers
        scalebarUnit: 'metric',
      })

      // 小地图
      // let overviewMapDijit = new this.gisConstructor.OverviewMap({
      //   map: this.map,
      //   visible: true,
      // })
      // overviewMapDijit.startup()

      // 自定义导航空间(地图平移,缩放,设置范围等功能)
      let innerDOM = document.getElementById('navigation-toolbar')
      let allBtn = innerDOM.querySelectorAll('button')
      let siteDOM = ''
      for (let i = 0; i < allBtn.length; i++) {
        if (allBtn[i].getAttribute('id') === 'site') {
          siteDOM = allBtn[i]
          siteDOM.style.top = this.map.getLevel() + 20 + 'px'
        }
        allBtn[i].onclick = event => {
          let domID = event.target.getAttribute('id')
          switch (domID) {
            case 'resize':
              this.map.setExtent(
                new this.gisConstructor.Extent(
                  extent.xmin,
                  extent.ymin,
                  extent.xmax,
                  extent.ymax
                )
              )
              break
            case 'east':
              this.map.panRight()
              break
            case 'west':
              this.map.panLeft()
              break
            case 'north':
              this.map.panDown()
              break
            case 'south':
              this.map.panUp()
              break
            case 'zoomUp':
              this.map.setLevel(this.map.getLevel() + 1)
              break
            case 'zoomDowm':
              this.map.setLevel(this.map.getLevel() - 1)
              break
            case 'site':
              // this.map.setLevel()
              break
          }
          siteDOM.style.top = this.map.getLevel() + 20 + 'px'
        }
      }
      siteDOM.style.top = this.map.getLevel() + 20 + 'px'
    },
  },
}
</script>

<style lang="scss">
#map-container {
  position: relative;
}
#HomeButton {
  position: absolute;
  top: 95px;
  left: 20px;
  z-index: 50;
}
#LocateButton {
  position: absolute;
  top: 145px;
  left: 20px;
  z-index: 50;
}
.esriScalebarLabel {
  width: auto !important;
}
.esriOverviewMap.ovwTR {
  top: 50px !important;
}
#navigation-toolbar {
  position: absolute;
  top: 50px;
  left: 100px;
  z-index: 9;
  text-align: center;
  .nt-translation {
    width: 60px;
    height: 60px;
    background-image: url('../assets/images/navtoolbar/ico02.png');
    background-repeat: no-repeat;
    background-size: contain;
    position: relative;
    button {
      width: 20px;
      height: 20px;
      &#resize {
        width: 26px;
        height: 26px;
        background-image: url('../assets/images/navtoolbar/ico03.png');
        background-repeat: no-repeat;
        background-size: contain;
        top: 50%;
        left: 50%;
        margin-left: -13px;
        margin-top: -13px;
      }
      &#east {
        top: 50%;
        right: 0;
        margin-top: -10px;
      }
      &#west {
        top: 50%;
        left: 0;
        margin-top: -10px;
      }
      &#south {
        left: 50%;
        bottom: 0;
        margin-left: -10px;
      }
      &#north {
        left: 50%;
        top: 0;
        margin-left: -10px;
      }
    }
  }
  .nt-zoom {
    position: relative;
    height: 120px;
    margin-top: 20px;
    .rule {
      width: 20px;
      height: 104px;
      background-image: url('../assets/images/navtoolbar/ico01.jpg');
      background-repeat: no-repeat;
      background-size: contain;
      position: absolute;
      top: 0;
      left: 50%;
      margin-left: -10px;
    }
    button {
      width: 16px;
      height: 16px;
      background-repeat: no-repeat;
      background-size: contain;
      &#zoomUp {
        left: 50%;
        top: -18px;
        margin-left: -8px;
        background-image: url('../assets/images/navtoolbar/ico04.png');
      }
      &#zoomDowm {
        left: 50%;
        bottom: -4px;
        margin-left: -8px;
        background-image: url('../assets/images/navtoolbar/ico05.png');
      }
      &#site {
        width: 11px;
        height: 11px;
        left: 50%;
        top: 5px;
        margin-left: -5px;
        background-image: url('../assets/images/navtoolbar/ico06.png');
      }
    }
  }
  button {
    cursor: pointer;
    position: absolute;
    background: none;
    outline: 0;
    border: 0;
  }
}
</style>

esri-loader 方法:

  • getScript () 从库里面获取 js 文件
  • isLoaded () 检测模块是否加载完成
  • loadModules( [ ], options) 用于加载 arcgis 模块
  • loadCss( url ) 用于加载 css 文件
  • loadScript({url: “xxxxxxxx” }) 将 js 加载到页面上

如果将依赖部署到了本地或其他服务器上,需要修改 init.js,否则依赖可能会加载失败,在 init.js 中搜索:


baseUrl:

修改后边地址,例如:


baseUrl:"http://localhost/js.arcgis.com/3.32/dojo"

也可以设置为动态地址,通过 js 获取当前文件所在的服务器地址:


baseUrl:document.scripts[document.scripts.length - 1].src.substring(0, document.scripts[document.scripts.length - 1].src.lastIndexOf("/") + 1) + "/dojo"
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
ArcGIS JavaScript API 中,可以使用小部件(Widget)来添加一些地图上的交互元素,如鹰眼视图(Overview Map)。 如果你想在 Vue 2 中使用 ArcGIS JavaScript API 来实现鹰眼小工具,可以按照以下步骤操作: 1. 在你的 Vue 2 项目中安装 ArcGIS JavaScript API。可以通过 npm 或者 CDN 等方式进行安装。 2. 在 Vue 的组件中引入 ArcGIS JavaScript API 并创建一个小部件(Widget)。 ```javascript <template> <div> <div ref="mapViewNode"></div> <div ref="overviewMapDiv"></div> </div> </template> <script> import EsriMap from "esri/Map"; import MapView from "esri/views/MapView"; import OverviewMap from "esri/widgets/OverviewMap"; export default { name: "MapView", data() { return { mapView: null, overviewMap: null }; }, mounted() { this.initMap(); }, methods: { initMap() { const map = new EsriMap({ basemap: "streets-navigation-vector" }); this.mapView = new MapView({ container: this.$refs.mapViewNode, map: map, center: [-118.80500, 34.02700], zoom: 13 }); this.overviewMap = new OverviewMap({ view: this.mapView, container: this.$refs.overviewMapDiv, visible: true }); this.mapView.ui.add(this.overviewMap, { position: "bottom-right" }); } } }; </script> ``` 在上面的代码中,我们首先引入了 EsriMap、MapView 和 OverviewMap 等需要使用的模块。然后在组件的 `mounted` 方法中调用 `initMap` 方法来初始化地图和小工具。 在 `initMap` 方法中,我们首先创建一个地图,然后在地图上创建一个 MapView。接着,我们创建一个 OverviewMap 小工具,并将其添加到 MapView 的 UI 中。 最后,在组件的模板中,我们需要添加两个容器,分别用来放置 MapView 和 OverviewMap。 ```html <div ref="mapViewNode"></div> <div ref="overviewMapDiv"></div> ``` 这样,就可以在 Vue 2 中使用 ArcGIS JavaScript API 来实现鹰眼小工具了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值