arcgis api for 4.x 学习笔记-在Vue 2.X项目中部署arcgis api for javascript 4.x(二)

一、介绍

为了更好的学习arcgis api for javascript 4.x,我使用vue cli 4.x创建了一个vue的项目,具体的创建方式请百度。由于项目是vue项目,所以我使用的是上一篇文章提到的,使用ES模块构建arcgis api for javascript 4.x。

二、部署

将模块安装到您的项目中:

npm install @arcgis/core

复制资源:

需要将API的资产(包括样式,图像,字体和本地化文件)从该 @arcgis/core/assets文件夹复制到构建文件夹。一种简单的方法是配置在构建过程中运行的NPM脚本。例如,使用npm ncp在package.json中安装和配置脚本以复制文件夹,具体步骤如下:
  1. 安装ncp
npm i ncp

2.配置package.json脚本

v2-ebf468cf4314d695d19329b4aaea659f_b.jpg

package.json里面添加
 "copy": "ncp ./node_modules/@arcgis/core/assets ./public/assets",
 "postinstall": "npm run copy"

v2-09a0cc1dbc77efc27d8e1619075b6976_b.jpg

执行postinstall,将API的资产(包括样式,图像,字体和本地化文件)从该@arcgis/core/assets文件夹复制到构建文件夹

配置CSS

<style lang="less">
@import url('~@arcgis/core/assets/esri/themes/dark/main.css');

body, html, #app {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
</style>
App.vue文件中添加 @importurl('~@arcgis/core/assets/esri/themes/dark/main.css'); 引入arcgis api的样式。(必须添加,否则样式是乱的)

三、最后

万事具备,我们可以使用arcgis api了

  <template>
  <div id="viewDiv"></div>
  </template>

ES模块导入的代码段:

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Map from '@arcgis/core/Map';
import MapView from '@arcgis/core/views/MapView';

@Component({ name: 'MapIndex' })
export default class MapIndex extends Vue {
  view: MapView | undefined;

  mounted() {
    const map = new Map({
      basemap: 'topo-vector',
    });

    this.view = new MapView({
      container: 'viewDiv',
      map,
      zoom: 4,
      center: [15, 65], // longitude, latitude
    });
  }
}
</script>

效果:

v2-d80a9e2265b42b3d19066fddc4ec9919_b.jpg

成功创建地图!

四、样式调整

#viewDiv {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}

注意:如果上述操作步骤都完成但是还是看不到地图,在没有报错的情况下,看一下是否是map的viewDiv(dom容器)或者body、html的width和height没有设置成100%

v2-aa588ae804e6013edce69d17c7d315a1_b.jpg

/* 去掉地图聚焦边框 */
.esri-view-surface--inset-outline:focus::after {
  outline: none !important;
}

mounted() {
    const map = new Map({
      basemap: 'streets',
    });

    this.view = new MapView({
      container: 'viewDiv',
      map,
      zoom: 4,
      center: [102, 24], // longitude, latitude
    });
    /* 去掉地图logo */
    this.view.ui.remove('attribution');
  }

demo源代码地址

jz991724/jsapiArcgis4.x

上一篇:arcgis api for 4.x 学习笔记-在Vue 2.X项目中部署arcgis api for javascript 4.x(一)_u012917880的博客-CSDN博客

下一篇:arcgis api for 4.x 学习笔记-在Vue 2.X项目中部署arcgis api for javascript 4.x(三)_u012917880的博客-CSDN博客

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现使用 QueryTask 绘制多边形查询并将查询结果以表格形式返回,需要进行以下步骤: 1. 引入 ArcGIS API For JavaScript 3.X 版本和 Vue 框架。 2. 在 Vue 实例创建地图,并添加绘图工具和表格。 ```javascript // 创建地图 var map = new esri.Map("map", { basemap: "streets" }); // 添加绘图工具 var tb = new esri.toolbars.Draw(map); tb.on("draw-end", doQuery); // 添加表格 new Vue({ el: '#table', data: { features: [] } }); ``` 3. 在绘制完成后触发 doQuery 函数进行查询。 ```javascript function doQuery(evt) { tb.deactivate(); var query = new esri.tasks.Query(); query.geometry = evt.geometry; query.outFields = ["*"]; // 查询所有字段 var queryTask = new esri.tasks.QueryTask("your service url"); queryTask.execute(query, showResults); // 执行查询 } ``` 4. 在 showResults 函数处理查询结果,并将结果渲染到表格。 ```javascript function showResults(featureSet) { var features = featureSet.features; var data = []; for (var i = 0; i < features.length; i++) { var feature = features[i]; var attributes = feature.attributes; var row = []; for (var j in attributes) { row.push(attributes[j]); } data.push(row); } // 渲染表格 var app = new Vue({ el: '#table', data: { features: data } }); } ``` 完整代码示例: ```html <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" /> <title>QueryTask Demo</title> <link rel="stylesheet" href="https://js.arcgis.com/3.38/esri/css/esri.css"> <style> #map { width: 100%; height: 500px; } #table { margin-top: 20px; } </style> </head> <body> <div id="map"></div> <div id="table"> <table> <thead> <tr> <th>Field 1</th> <th>Field 2</th> <th>Field 3</th> </tr> </thead> <tbody> <tr v-for="feature in features"> <td>{{feature[0]}}</td> <td>{{feature[1]}}</td> <td>{{feature[2]}}</td> </tr> </tbody> </table> </div> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.min.js"></script> <script src="https://js.arcgis.com/3.38/"></script> <script> var map = new esri.Map("map", { basemap: "streets" }); // 添加绘图工具 var tb = new esri.toolbars.Draw(map); tb.on("draw-end", doQuery); // 添加表格 new Vue({ el: '#table', data: { features: [] } }); function doQuery(evt) { tb.deactivate(); var query = new esri.tasks.Query(); query.geometry = evt.geometry; query.outFields = ["*"]; // 查询所有字段 var queryTask = new esri.tasks.QueryTask("your service url"); queryTask.execute(query, showResults); } function showResults(featureSet) { var features = featureSet.features; var data = []; for (var i = 0; i < features.length; i++) { var feature = features[i]; var attributes = feature.attributes; var row = []; for (var j in attributes) { row.push(attributes[j]); } data.push(row); } // 渲染表格 var app = new Vue({ el: '#table', data: { features: data } }); } </script> </body> </html> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花姐夫Jun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值