翼度科技»论坛 编程开发 JavaScript 查看内容

Vue+ ArcGIS JavaScript APi详解

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
版本

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.          
  62.           this.addResolvingPromise(
  63.             this._elevation.load().then(() => {
  64.             
  65.               this.tileInfo = this._elevation.tileInfo;
  66.               this.spatialReference = this._elevation.spatialReference;
  67.               this.fullExtent = this._elevation.fullExtent;
  68.             })
  69.           );

  70.           return this;
  71.         },

  72.         // Fetches the tile(s) visible in the view
  73.         fetchTile: function (level, row, col, options) {
  74.           // calls fetchTile() on the elevationlayer for the tiles
  75.           // visible in the view
  76.           return this._elevation.fetchTile(level, row, col, options).then(
  77.             function (data) {
  78.               const exaggeration = this.exaggeration;

  79.               for (let i = 0; i < data.values.length; i++) {
  80.                 // Multiply the given pixel value
  81.                 // by the exaggeration value
  82.                 data.values[i] = data.values[i] * exaggeration;
  83.               }
  84.               return data;
  85.             }.bind(this)
  86.           );
  87.         }
  88.       });


  89.       const basemap = new Basemap({
  90.         baseLayers: [
  91.           new TileLayer({
  92.             url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"
  93.           }),
  94.           new TileLayer({
  95.             url:
  96.               "https://tiles.arcgis.com/tiles/nGt4QxSblgDfeJn9/arcgis/rest/services/terrain_with_heavy_bathymetry/MapServer"
  97.           }),

  98.         ]
  99.       });

  100.       const elevationLayer = new ExaggeratedElevationLayer();

  101.       
  102.       this.map3d = new Map({
  103.         basemap: basemap,
  104.         ground: {
  105.           layers: [elevationLayer]
  106.         }
  107.       });

  108.       const view = new SceneView({
  109.         container: "earth",
  110.         map: this.map3d,
  111.         alphaCompositingEnabled: true,
  112.         qualityProfile: "high",
  113.         camera: {
  114.           position: [-55.039, 14.948, 19921223.3],
  115.           heading: 2.03,
  116.           tilt: 0.13
  117.         },
  118.         environment: {
  119.           background: {
  120.             type: "color",
  121.             color: [255, 252, 244, 0]
  122.           },
  123.           starsEnabled: true,
  124.           atmosphereEnabled: true,
  125.           lighting: {
  126.             type: "virtual"
  127.           }
  128.         },


  129.       });


  130.       this.map3d.ground = {
  131.         layers: [elevationLayer]
  132.       };

  133.     }
  134.   }
  135. }
  136. </script>

  137. <!-- Add "scoped" attribute to limit CSS to this component only -->
  138. <style scoped>
  139. .hello {
  140.   width: 100%;
  141.   height: 100%;
  142. }

  143. .map {
  144.   width: 100%;
  145.   height: 100%;
  146. }
  147. </style>
复制代码
demo地址
https://gitee.com/wolf_pro/vue_arcgis4.22.git
到此这篇关于Vue+ ArcGIS JavaScript APi的文章就介绍到这了,更多相关Vue+ ArcGIS JavaScript APi内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/article/266733.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具