vue+ArcGIS API for JavaScript(1)——加载地图

前言
因为要做一个和ArcGIS相关的项目,所以记录一下自己的学习过程。
我使用的版本 :
VScode
vue-cli 版本:4.5.13
ArcGIS API for JavaScript 文档(4.20版本): ArcGIS API for JavaScript
主要还是跟着文档走
正文
有多种方式来将ArcGIS API for JavaScript引入自己的应用程序中,我是通过ES模块来构建的项目。

1. 先安装模块。

npm install @arcgis/core

安装之后看一下node_modules文件夹中是否有@arcgis和@esri,有就表示安装成功。
在这里插入图片描述
2. 加载模块
创建新组件MapView,引入Map和MapView:

import Map from '@arcgis/core/Map'
import MapView from '@arcgis/core/views/MapView'

3. 创建地图

const myMap = new Map({
      basemap: "streets-vector"
    })
地图实例就是一个包含图层的简单容器,还有其它的属性和方法。

Map类文档
4. 创建二维视图

const view = new MapView({
      container: "viewDiv", // Reference to the DOM node that will contain the view
      map: myMap, // References the map object created in step 3
      zoom: 7, // Sets zoom level based on level of detail (LOD)
      center: [15, 65] // Sets center point of view using longitude,latitude
    })
view.when(function(){
      alert('地图加载完毕')
    // All the resources in the MapView and the map have loaded. Now execute additional processes
    }, function(error){
    // Use the errback function to handle when the view doesn't load properly
    console.log("The view's resources failed to load: ", error);

创建MapView的实例必须至少有两个对象,一个是Map实例和DOM元素,分别再map属性和container属性设置。MapView 和地图中的所有资源都已加载,.when()就可以执行额外的进程
5. 设计页面

 <div id="viewDiv">
 </div>
 #viewDiv {
    width: 100%;
    height: 100%;     
  }

最后展示完整代码:
组件代码:

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

<script>
import Map from '@arcgis/core/Map'
import MapView from '@arcgis/core/views/MapView'
export default {
  data () {
    return {
      Map,
      MapView
    }
  },
  mounted () {
    const myMap = new Map({
      basemap: "streets-vector"
    })
    const view = new MapView({
      container: "viewDiv", // Reference to the DOM node that will contain the view
      map: myMap, // References the map object created in step 3
      zoom: 7, // Sets zoom level based on level of detail (LOD)
      center: [15, 65] // Sets center point of view using longitude,latitude
    })
    view.when(function(){
      alert('地图加载完毕')
    // All the resources in the MapView and the map have loaded. Now execute additional processes
    }, function(error){
    // Use the errback function to handle when the view doesn't load properly
    console.log("The view's resources failed to load: ", error);
});
  },
  components: {

  }
}
</script>

<style>
 #viewDiv {
    width: 100%;
    height: 100%;     
  }
</style>

App.vue代码

<template>
  <div id="app">
    <MapView/>
  </div>
</template>

<script>
import MapView from './components/MapView.vue'
export default {
  name: 'App',
  components: {
    MapView
  }
}
</script>

<style>
  html,body,#app{
    height: 100%;
  }
</style>

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,下面是一个简单的示例: 1. 在 Vue 项目中安装 arcgis for js 和 axios: ``` npm install --save @arcgis/core axios ``` 2. 在需要使用查询功能的组件中引入模块: ```javascript import * as esriLoader from "@arcgis/core"; // 引入 arcgis for js import axios from "axios"; // 引入 axios ``` 3. 在组件的 `mounted()` 生命周期中初始化地图和查询任务: ```javascript mounted() { // 初始化地图 esriLoader.loadModules(["esri/Map", "esri/views/MapView"]).then(([Map, MapView]) => { const map = new Map({ basemap: "streets-navigation-vector", }); const view = new MapView({ container: "map-container", map: map, center: [-118.80500, 34.02700], zoom: 13, }); }); // 初始化查询任务 esriLoader.loadModules(["esri/tasks/QueryTask", "esri/tasks/support/Query"]).then(([QueryTask, Query]) => { this.queryTask = new QueryTask({ url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3", // 查询图层的 URL }); this.query = new Query(); this.query.returnGeometry = false; // 不返回几何对象 this.query.outFields = ["*"]; // 返回所有字段 }); }, ``` 4. 在查询按钮的点击事件中执行查询: ```javascript methods: { onQuery() { const whereClause = "STATE_NAME = 'California'"; // 查询条件 this.query.where = whereClause; this.queryTask.execute(this.query).then((result) => { const features = result.features; const data = features.map((feature) => { return feature.attributes; }); this.tableData = data; // 将查询结果赋值给表格数据 }); }, }, ``` 5. 在模板中添加查询按钮和表格: ```html <div id="map-container"></div> <button @click="onQuery">查询</button> <table> <thead> <tr> <th v-for="field in fields">{{ field }}</th> </tr> </thead> <tbody> <tr v-for="row in tableData"> <td v-for="field in fields">{{ row[field] }}</td> </tr> </tbody> </table> ``` 6. 在 `data` 中定义表格数据和字段列表: ```javascript data() { return { queryTask: null, query: null, tableData: [], fields: ["STATE_NAME", "POP2000", "POP2007"], }; }, ``` 这样就可以在 Vue 中使用 arcgis for js 的查询功能,并将查询结果以表格形式返回。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

子时不睡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值