vue使用@arcgis/core方式開發arcgis for js api地圖項目


注意:在ArcGIS API for JavaScript模塊的導入中,也經歷了很多的變遷,從“dojo”到“esri-loader”等逐漸的變化。而在4.18版本后,新增了一種全新的模式-@arcgis/core模式,可以實現直接在組件頂部引入所需的API模塊,然后在組件代碼中使用。

預覽 : https://volodyan.github.io/vue_url_arcgis-preview/#/

1.在創建vue項目后,安裝@arcgis/core@4.22.2

npm install @arcgis/core@4.22.2

image

image

2.在組件中,使用 import 語句加載單個模塊

import "@arcgis/core/assets/esri/themes/light/main.css";

import Map from "@arcgis/core/Map";

import MapView from "@arcgis/core/views/MapView";

全部代碼如下:
點擊查看代碼
<template>
  <div class="MapView">
    <div id="viewDiv">
      <div id="el-select-theme">
        <el-select
          v-model="theme"
          placeholder="請選擇"
          @change="BasemapChangeFun"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
    </div>
  </div>
</template>
<script>
import "@arcgis/core/assets/esri/themes/light/main.css";
import Map from "@arcgis/core/Map";
import MapView from "@arcgis/core/views/MapView";

export default {
  name: "MapView",
  data() {
    return {
      options: [
        {
          value: "dark-gray-vector",
          label: "vector",
        },
        {
          value: "hybrid",
          label: "hybrid",
        },
        {
          value: "topo",
          label: "topo",
        },
        {
          value: "streets",
          label: "streets",
        },
        {
          value: "dark-gray",
          label: "dark-gray",
        },
        {
          value: "osm",
          label: "osm",
        },
        {
          value: "oceans",
          label: "海洋",
        },
      ],
      theme: "",
      mapview: "",
    };
  },
  mounted() {
    this.loadArcgisMap();
  },
  beforeDestroy() {
    this.DestroyArcgisMap();
  },
  methods: {
    loadArcgisMap() {
      const map = new Map({
        basemap: "topo-vector",
      });

      this.mapview = new MapView({
        container: "viewDiv",
        map: map,
        center: [-80, 35],
        extent: {
          spatialReference: {
            latestWkid: 3857,
            wkid: 102100,
          },
          xmin: -59087672,
          ymin: 4108613,
          xmax: -43095009,
          ymax: 8340167,
        },
      });
      this.mapview.ui.components = []; //清空所有ESRI自帶的組件

      this.mapview.popup.dockEnabled = false;
      this.mapview.popup.collapseEnabled = false;
      this.mapview.popup.dockOptions = {
        buttonEnabled: false,
        breakpoint: false,
        position: "bottom-left",
      }; //禁用停靠
      this.mapview.popup.actions = null;
      this.mapview.popup = null;
      this.mapview.ui.add("el-select-theme", "top-right");
    },
    BasemapChangeFun(val) {
      this.mapview.map.basemap = val;
    },
    DestroyArcgisMap() {
      this.mapview.destroy();
      this.mapview.map = null;
      this.mapview.container = null;
    },
  },
};
</script>
 
<style  lang="scss" scoped>
.MapView {
  width: 100%;
  height: 100%;
  #viewDiv {
    width: 100%;
    height: 100%;
  }
}
</style>

3.執行npm install ncp -g命令,全局安裝ncp

npm install ncp -g

4.在package.json文件里修改運行命令,如下:

"serve": "npm run copy && vue-cli-service serve"

"build": "npm run copy && vue-cli-service build",

"copy": "ncp ./node_modules/@arcgis/core/assets ./public/assets"

package.json如下

點擊查看代碼
{
  "name": "vue_url_arcgis",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "npm run copy && vue-cli-service serve",
    "build": "npm run copy && vue-cli-service build",
    "copy": "ncp ./node_modules/@arcgis/core/assets ./public/assets"
  },
  "dependencies": {
    "@arcgis/core": "^4.22.2",
    "core-js": "^3.8.3",
    "element-ui": "^2.15.6",
    "vue": "^2.6.14",
    "vue-router": "^3.5.1",
    "vuex": "^3.6.2"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-plugin-router": "~5.0.0",
    "@vue/cli-plugin-vuex": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "sass": "^1.32.7",
    "sass-loader": "^12.0.0",
    "vue-template-compiler": "^2.6.14"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]
}

5.運行 npm run serve命令啟動項目,在public文件下會新增assets文件夾,assets文件夾是編譯后的arcgis地圖相關文件,如圖所示:

image

6.運行后的預覽圖,如下:

image

7.運行npm run build打包項目,打包后的項目如下:

image

8.發布在iis服務器上測試

(1).在iis上添加網站

image

(2).將默認文檔上移至首位

image

(3).預覽發布后的網站

image

(4).瀏覽器預覽

image

Github源碼: https://github.com/volodyan/vue_url_arcgis

參考文檔如下:

1.https://xuqwblog.blog.csdn.net/article/details/111376081

2.https://blog.csdn.net/tjx1997/article/details/119610327

3.https://developers.arcgis.com/javascript/latest/es-modules/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM