从零开始的VUE+ArcGIS API for JS项目——底图切换工具(天地图,高德)

前言

今天继续在之前的项目上进行自定义工具的编写。主要是实现自定义底图切换的工具,添加天地图,高德等地图作为底图。
上篇文章地址:
从零开始的VUE+ArcGIS API for JS项目——初始化地图应用

添加element plus

编写组件使用到了element plus的相关组件,所以首先需要在项目中引入element plus的依赖包,在package.json 文件中添加依赖,这上一篇文章的package.json中已经添加了。添加之后 需要运行 run pnpm install 命令下载包。

  "dependencies": {
   
    ...
    "@element-plus/icons-vue": "2.3.1",
    "element-plus": "2.7.8",
    ...
  },

运行完成后,修改main.js文件将element 引入到项目中,之后就可以全局使用相关组件了。修改后的代码如下:

import {
    createApp } from 'vue'
import App from './App.vue'

// 引入arcgis 样式
import "@arcgis/core/assets/esri/themes/dark/main.css"

//引入elementui
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

const app = createApp(App)

//注册所有element图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
   
  app.component(key, component)
}

app.use(ElementPlus)
app.mount('#app')

编写底图类库

上一篇文章中,已经初步编写了一个底图控制类,其中只添加了Arcgis的web服务。在这个文件的基础上,进行修改,使用**@arcgis/core/layers/WebTileLayer** 这个接口添加天地图,高德的地图api。使用天地图的地图,首先需要在天地图的官网申请地图key,可以自行百度。下列代码中天地图的地址中需要替换自己申请的key。地图的缩略图放在路径“src/assets”下,自己添加即可。详细代码如下:

import WebTileLayer from '@arcgis/core/layers/WebTileLayer'
import {
   reactive } from "vue";

const gdlayer = new WebTileLayer({
   
  id: 'gaode',
  urlTemplate:'https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={col}&y={row}&z={level}',
  name: '高德地图',
})
//将地址中的key替换为你自己申请的key
const tdimgtLayer = new WebTileLayer({
   
  id:'tdimg',
  urlTemplate: 'http://{subDomain}.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=你自己的key',
  subDomains: ['t0', '
好的,下面是一个简单的示例: 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 的查询功能,并将查询结果以表格形式返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值