[前端] Vue+ ArcGIS JavaScript APi详解

2111 0
黑夜隐士 2022-11-8 09:26:09 | 显示全部楼层 |阅读模式
目录

    版本环境搭建
      新建vue增加ArcGIS JavaScript 包引用拷贝资源信息
    页面测试


版本

Vue 2
ArcGIS JavaScript 4.22
注意 ArcGIS JavaScript3.x 和ArcGIS JavaScript 4.x框架差异较大

环境搭建


新建vue

可以使用vue ui创建项目

增加ArcGIS JavaScript 包引用

package.json
  1. "dependencies": {
  2.     "core-js": "^3.8.3",
  3.     "vue": "^2.6.14",   
  4.     "@arcgis/core":"4.22.2",
  5.     "ncp":"^2.0.0"
  6.   },
  7.   "devDependencies": {
  8.     "@babel/core": "^7.12.16",
  9.     "@babel/eslint-parser": "^7.12.16",
  10.     "@vue/cli-plugin-babel": "~5.0.0",
  11.     "@vue/cli-plugin-eslint": "~5.0.0",
  12.     "@vue/cli-service": "~5.0.0",
  13.     "eslint": "^6.8.0",
  14.     "eslint-plugin-vue": "^5.2.3",   
  15.     "vue-template-compiler": "^2.6.14"  
  16.   },
复制代码
ncp: 主要用于拷贝资源信息
@arcgis/core 是arcgis_js仓库

拷贝资源信息

package.json中配置copy命令
  1. "scripts": {
  2.     "serve": "vue-cli-service serve",
  3.     "build": "vue-cli-service build",
  4.     "lint": "vue-cli-service lint",
  5.     "copy": "ncp ./node_modules/@arcgis/core/assets ./public/assets"
  6.   },
复制代码
安装完依赖后运行 copy命令
  1. yarn
  2. yarn copy
  3. yarn serve
  4. -------------------
  5. npm i
  6. npm run copy
  7. npm run serve
复制代码
运行完copy命令后,会将arcgis相关资源拷贝到public/assets目录下
全局引入
main.js
  1. import '@arcgis/core/assets/esri/themes/light/main.css'
  2. import esriConfig from '@arcgis/core/config.js'
  3. esriConfig.assetsPath = './assets'
复制代码
页面测试

helloworld.vue
  1. <template>
  2.   <div class="hello">
  3.     <div id="map" class="map" v-show="flag == 'map'">
  4.     </div>
  5.     <div id="earth" class="map" v-show="flag == 'earth'"></div>
  6.   </div>
  7. </template>
  8. <script>
  9. import Map from '@arcgis/core/Map'
  10. import MapView from '@arcgis/core/views/MapView'
  11. import MapImageLayer from '@arcgis/core/layers/MapImageLayer'
  12. import ElevationLayer from '@arcgis/core/layers/ElevationLayer'
  13. import BaseElevationLayer from '@arcgis/core/layers/BaseElevationLayer'
  14. import SpatialReference from '@arcgis/core/geometry/SpatialReference'
  15. import SceneView from '@arcgis/core/views/SceneView'
  16. import Basemap from '@arcgis/core/Basemap'
  17. import TileLayer from '@arcgis/core/layers/TileLayer'
  18. export default {
  19.   name: 'HelloWorld',
  20.   data() {
  21.     return {
  22.       mapView: null,
  23.       map: null,
  24.       map3d: null,
  25.       flag: 'earth'
  26.     }
  27.   },
  28.   mounted() {
  29.     this.initBasemap()
  30.   },
  31.   methods: {
  32.     initBasemap() {
  33.       const self = this
  34.       //二维
  35.       const mapImageLayer = new MapImageLayer({
  36.         url: "http://192.168.3.156:6080/arcgis/rest/services/xiangyang/jichang/MapServer"
  37.       })
  38.       this.map = new Map({
  39.         // basemap: basemap,
  40.         layers: [mapImageLayer]
  41.       })
  42.       this.mapView = new MapView({
  43.         container: 'map',
  44.         map: self.map,
  45.         spatialReference: new SpatialReference({
  46.           wkid: 3857
  47.         }),
  48.         rotation: 41.2,
  49.         zoom: 3
  50.       })
  51.       // 三维地形
  52.       const ExaggeratedElevationLayer = BaseElevationLayer.createSubclass({      
  53.         properties: {
  54.           exaggeration: 10
  55.         },
  56.         load: function () {
  57.           // TopoBathy3D contains elevation values for both land and ocean ground
  58.           this._elevation = new ElevationLayer({
  59.             url: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/TopoBathy3D/ImageServer"
  60.           });
  61.           this.addResolvingPromise(
  62.             this._elevation.load().then(() => {
  63.               this.tileInfo = this._elevation.tileInfo;
  64.               this.spatialReference = this._elevation.spatialReference;
  65.               this.fullExtent = this._elevation.fullExtent;
  66.             })
  67.           );
  68.           return this;
  69.         },
  70.         // Fetches the tile(s) visible in the view
  71.         fetchTile: function (level, row, col, options) {
  72.           // calls fetchTile() on the elevationlayer for the tiles
  73.           // visible in the view
  74.           return this._elevation.fetchTile(level, row, col, options).then(
  75.             function (data) {
  76.               const exaggeration = this.exaggeration;
  77.               for (let i = 0; i < data.values.length; i++) {
  78.                 // Multiply the given pixel value
  79.                 // by the exaggeration value
  80.                 data.values[i] = data.values[i] * exaggeration;
  81.               }
  82.               return data;
  83.             }.bind(this)
  84.           );
  85.         }
  86.       });
  87.       const basemap = new Basemap({
  88.         baseLayers: [
  89.           new TileLayer({
  90.             url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"
  91.           }),
  92.           new TileLayer({
  93.             url:
  94.               "https://tiles.arcgis.com/tiles/nGt4QxSblgDfeJn9/arcgis/rest/services/terrain_with_heavy_bathymetry/MapServer"
  95.           }),
  96.         ]
  97.       });
  98.       const elevationLayer = new ExaggeratedElevationLayer();
  99.       this.map3d = new Map({
  100.         basemap: basemap,
  101.         ground: {
  102.           layers: [elevationLayer]
  103.         }
  104.       });
  105.       const view = new SceneView({
  106.         container: "earth",
  107.         map: this.map3d,
  108.         alphaCompositingEnabled: true,
  109.         qualityProfile: "high",
  110.         camera: {
  111.           position: [-55.039, 14.948, 19921223.3],
  112.           heading: 2.03,
  113.           tilt: 0.13
  114.         },
  115.         environment: {
  116.           background: {
  117.             type: "color",
  118.             color: [255, 252, 244, 0]
  119.           },
  120.           starsEnabled: true,
  121.           atmosphereEnabled: true,
  122.           lighting: {
  123.             type: "virtual"
  124.           }
  125.         },
  126.       });
  127.       this.map3d.ground = {
  128.         layers: [elevationLayer]
  129.       };
  130.     }
  131.   }
  132. }
  133. </script>
  134. <!-- Add "scoped" attribute to limit CSS to this component only -->
  135. <style scoped>
  136. .hello {
  137.   width: 100%;
  138.   height: 100%;
  139. }
  140. .map {
  141.   width: 100%;
  142.   height: 100%;
  143. }
  144. </style>
复制代码
demo地址
https://gitee.com/wolf_pro/vue_arcgis4.22.git
到此这篇关于Vue+ ArcGIS JavaScript APi的文章就介绍到这了,更多相关Vue+ ArcGIS JavaScript APi内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行