为了页面的美观,这里我们使用的UI库为: Ant Design Vue

项目源码仓库地址:https://github.com/LuckRain7/arcgis-api-for-javascript-vue

1. 首先创建工具菜单组件

创建文件 src\components\ToolBar.vue

并通过组件通信写好对应接口

<template><div class="toolbar"><!-- 使用按钮组 --><a-button-group><!-- 地图切换按钮 --><a-dropdown><a-menu slot="overlay"><a-menu-item key="1" @click="baseMapChange(1)">矢量</a-menu-item><a-menu-item key="2" @click="baseMapChange(2)">影像</a-menu-item></a-menu><a-button type="primary"><a-icon type="global" />底图 <a-icon type="down" /></a-button></a-dropdown><!-- 地图切换按钮 END--><a-dropdown><a-menu slot="overlay"><a-menu-item key="1" @click="measurement(1)">开始测量</a-menu-item><a-menu-item key="2" @click="measurement(0)">取消测量</a-menu-item></a-menu><a-button type="primary"><a-icon type="tool" />测量 <a-icon type="down" /></a-button></a-dropdown><!-- 标绘 GO--><a-dropdown><a-menu slot="overlay"><a-menu-item key="1" @click="draw('POINT')">点</a-menu-item><a-menu-item key="2" @click="draw('POLYLINE')">线</a-menu-item><a-menu-item key="3" @click="draw('POLYGON')">面</a-menu-item><a-menu-item key="4" @click="draw('CIRCLE')">圆形</a-menu-item><a-menu-item key="5" @click="draw('RECTANGLE')">长方形</a-menu-item><a-menu-item key="6" @click="draw('stop')">停止标绘</a-menu-item><a-menu-item key="7" @click="draw('delete')">清除标绘</a-menu-item></a-menu><a-button type="primary"><a-icon type="highlight" />标绘 <a-icon type="down" /></a-button></a-dropdown><!-- 标绘 END--><a-button type="primary" @click="showLegend"><a-icon type="unordered-list" />图例</a-button><a-button type="primary" @click="showLayerList"><a-icon type="database" />图层</a-button></a-button-group></div>
</template><script>
export default {name: "ToolBar",methods: {// 开启测量measurement(type) {this.$emit("measurement", type);},// 底图切换baseMapChange(type) {this.$emit("baseMapChange", type);},// 标绘draw(type) {this.$emit("draw", type);},// 显示图例showLegend() {this.$emit("showLegend");},// 显示图层showLayerList() {this.$emit("showLayerList");},},
};
</script><style lang="less">
.toolbar {position: absolute;top: 80px;right: 40px;height: 40px;width: auto;z-index: 99;
}
</style>

在 src\App.vue 显示页面引入,并提供组件通讯接口

<template>
<!-- 工具条组件 -->
<tool-bar@measurement="measurement"@baseMapChange="baseMapChange"@draw="draw"@showLegend="showLegend"@showLayerList="showLayerList"
></tool-bar><!-- 测量组件 -->
<measurement:show="isShowMeasurement"@closMmeasurement="measurement"
></measurement>
</template>
<script>
import ToolBar from "./components/ToolBar.vue";
components: {ToolBar
},
methods: {// 测量measurement(type) {switch (type) {case 0:this.isShowMeasurement = false;Map.MeasurementClose();break;case 1:this.isShowMeasurement = true;}},/* 地图切换 */baseMapChange(type) {Map.baseMapChange(type);},// 标绘draw(type) {Map.drawActive(type);},
},
</script>

效果图:

1. 底图切换

在 example\src\map\init.js 文件中添加底图切换函数

当约定的 Type = 1 时,使用 addLayer 方法添加矢量图层并移除影像图层

当约定的 Type = 2 时,使用 addLayer 方法添加影像图层并移除矢量图层

ps: addLayer方法是对 map.addLayer()的二次封装

baseMapChange(type) {if (type === this.baseMap.type) return; // 防止重复加载// 添加 影像if (type === 2) {this.addLayer([this.baseMap.rasterMap, this.baseMap.rasterMapAnnotation],[0, 1]);this.removeLayer(this.baseMap.vectorMap);this.baseMap.type = 2;}// 添加 矢量else {this.addLayer(this.baseMap.vectorMap, 0);this.removeLayer([this.baseMap.rasterMap,this.baseMap.rasterMapAnnotation,]);this.baseMap.type = 1;}
}

在 example\src\App.vue 中进行应用

/* 地图切换 */
baseMapChange(type) {Map.baseMapChange(type);
},

效果图:

2. 测量组件

这里需要在 src\map\init.js 中加载 ArcGIS 的测量和单位模块("esri/dijit/Measurement""esri/units")

ps: 模块与下方的导出函数一定要一一对应

#  example\src\map\init.js
loadModules(["esri/map","tdlib/SDTDTLayer","tdlib/SDRasterLayer","tdlib/SDRSAnnoLayer","esri/geometry/Extent","esri/SpatialReference",
+    "esri/dijit/Measurement",
+    "esri/units","dojo/parser",],config.loadConfig
).then(([Map, // 地图模块SDTDTLayer, // 山东天地图矢量地图SDRasterLayer, // 山东天地图影像地图SDRSAnnoLayer, // 山东天地图影像地图注记Extent, // 范围模块SpatialReference, // 坐标系模块
+      Measurement, //测量模块
+      Units, // 单位模块Parser, // 样式解析模块])

并进行相关配置 example\src\map\init.js(以下是增量代码,不是文件中实际位置)

// 测量工具初始化
this.measurement = new Measurement({map: this.map,defaultLengthUnit: Units.KILOMETERS,defaultAreaUnit: Units.SQUARE_KILOMETERS,},document.getElementById("measurement")
);
this.measurement.startup();// 关闭测量工具
MeasurementClose() {this.measurement.clearResult(); // 清除地图图案// 拿到开启的工具名称 并关闭已开启的工具this.measurement.getTool() &&this.measurement.setTool(this.measurement.getTool().toolName, false);
}

创建一个用来展示测量组件 src\components\Measurement.vue

<template><div class="measurementdiv" v-show="show"><div class="div-header"><span class="title">测量组件</span><span class="close" @click="close">X</span></div><div id="measurement"></div></div>
</template><script>
export default {name: "Measurement",props: {show: Boolean,},methods: {close() {this.$emit("closMmeasurement", 0);},},
};
</script>

在页面中引入 src\App.vue

<template>
<!-- 测量组件 -->
<measurement:show="isShowMeasurement"@closMmeasurement="measurement"
></measurement>
</template>
<script>methods: {// 测量measurement(type) {switch (type) {case 0:this.isShowMeasurement = false;Map.MeasurementClose();break;case 1:this.isShowMeasurement = true;}},}
</script>

效果图:

3. 比例尺组件

这里需要在 src\map\init.js 中加载 ArcGIS 的比例尺模块("esri/dijit/Scalebar")

ps: 模块与下方的导出函数一定要一一对应

loadModules([
+    "esri/dijit/Scalebar",],config.loadConfig
).then(([
+      Scalebar, // 比例尺模块])

初始化比例尺

Scalebar({map: this.map,attachTo: "bottom-left",scalebarUnit: "metric",scalebarStyle: "line",
});

4. 标绘组件

非常抱歉,写到这里时我重构了代码,大家可以去代码仓库进行查看,重构的目的是为了更加的模块化。

这里需要在 src\map\modules\draw.js 中加载 ArcGIS 的画图模块、点样式模块、线样式模块、填充样式模块、图形模块和图形图层模块("esri/toolbars/draw""esri/symbols/SimpleMarkerSymbol""esri/symbols/SimpleLineSymbol""esri/symbols/SimpleFillSymbol""esri/graphic""esri/layers/GraphicsLayer")

/**  Description: 标绘工具*  Author: LuckRain7*  Date: 2020-05-07 17:05:55*/
import { loadModules } from "esri-loader";
import config from "../config";function drawInit() {loadModules(["esri/toolbars/draw", // 画图"esri/symbols/SimpleMarkerSymbol", // 点"esri/symbols/SimpleLineSymbol", // 线"esri/symbols/SimpleFillSymbol", // 面"esri/graphic", // 图形模块"esri/layers/GraphicsLayer", // 图形图层模块],config.loadConfig).then(([Draw,SimpleMarkerSymbol,SimpleLineSymbol,SimpleFillSymbol,Graphic,GraphicsLayer,]) => {this.GraphicsLayer = GraphicsLayer;this.Graphic = Graphic;this.Draw = Draw;this.SimpleMarkerSymbol = SimpleMarkerSymbol;this.SimpleLineSymbol = SimpleLineSymbol;this.SimpleFillSymbol = SimpleFillSymbol;// 添加图形图层this.DrawGraphics = new GraphicsLayer({ id: "drawLayer" });// 设置图层坐标系this.DrawGraphics.SpatialReference = new this.SpatialReference({wkid: 4490,});// 将图层加载到地图上,图层设置为 7this.map.addLayer(this.DrawGraphics, 7);// 实例化画图this.draw = new Draw(this.map);//定义图形样式this.draw.markerSymbol = new SimpleMarkerSymbol();this.draw.lineSymbol = new SimpleLineSymbol();this.draw.fillSymbol = new SimpleFillSymbol();// 添加画图的监听事件this.draw.on("draw-complete", drawEndEvent.bind(this));}).catch((err) => {console.error(err);});
}// 内置函数 画完后将图形加载到图形图层
function drawEndEvent(evt) {//添加图形到地图let symbol;if (evt.geometry.type === "point" || evt.geometry.type === "multipoint") {symbol = this.draw.markerSymbol;} else if (evt.geometry.type === "line" || evt.geometry.type === "polyline") {symbol = this.draw.lineSymbol;} else {symbol = this.draw.fillSymbol;}// 获取图形样式let tx = this.Graphic(evt.geometry, symbol);// 将图形样式加载到地图上this.DrawGraphics.add(tx);
}// 设置说话图形
function drawActive(type) {let tool = null;switch (type) {case "POINT":tool = "POINT";break;case "POLYLINE":tool = "POLYLINE";break;case "POLYGON":tool = "POLYGON";break;case "CIRCLE":tool = "CIRCLE";break;case "RECTANGLE":tool = "RECTANGLE";break;case "stop":this.draw.deactivate(); // 停止画图break;case "delete":this.draw.deactivate(); // 停止画图this.DrawGraphics.clear(); // 清除图层break;}if (tool !== null) {this.draw.activate(this.Draw[tool]); //激活对应的绘制工具}
}export { drawInit, drawActive };

在 导出文件中引入 src\map\index.js

import { MeasurementClose } from "./modules/Measurement.js";// 导入标绘功能
ArcGIS.prototype.drawInit = drawInit;
ArcGIS.prototype.drawActive = drawActive;

在组件中使用即可

效果图:

推荐阅读

  • vue + ArcGIS 地图应用系列二:加载地图

  • vue + ArcGIS 地图应用系列一:arcgis api本地部署(开发环境)

关注我! 不迷路

vue + ArcGIS 地图应用系列三:添加常规的地图组件相关推荐

  1. ArcGIS制图技巧系列(3)—让地图更有立体感

    ArcGIS制图技巧系列(3)-让地图更有立体感 by 李远祥 在前面的章节中,我们已经介绍过各种的地图效果,如发光效果,山体阴影效果,植被填充效果等,所有的这些效果不外乎是各种技术的叠加和技巧的使用 ...

  2. php网页地图上自定义,如何添加在线自定义地图

    在奥维互动地图浏览器中,除内置的在线地图外,用户还可以添加自定义地图,如在线电子地图和航拍图等,以满足用户对特定地图的需求. 1.正常添加在线电子地图的前提条件 (1)被添加的地图采用墨卡托投影方式, ...

  3. 乐高无限自己地图无法服务器,乐高无限地图模组怎么添加-乐高无限地图模组添加方法-7k7k游戏...

    乐高无限地图模组怎么添加,很多玩家不清楚,小编今天就带来解答,下面一起来看看吧! 乐高无限地图模组添加方法详解 有了MOD模组,这些统统都能实现!下面阿帅为大家重磅推荐的生存模式的全新玩法:地图模组绝 ...

  4. vue源码分析系列三:render的执行过程和Virtual DOM的产生

    render 手写 render 函数,仔细观察下面这段代码,试想一下这里的 createElement 参数是什么 . new Vue({el: '#application',render(crea ...

  5. 【Vue.js源码解析 三】-- 模板编译和组件化

    前言 笔记来源:拉勾教育 大前端高薪训练营 阅读建议:建议通过左侧导航栏进行阅读 模板编译 模板编译的主要目的是将模板 (template) 转换为渲染函数 (render) <div> ...

  6. 百度地图升级6:添加百度个性化地图及注意事项!

    首先说明一下百度的个性化地图 建立百度账号(秘钥啥的就不说了,此文章为升级篇) 打开百度地图开放平台-登录-特色服务平台-个性化地图       http://lbsyun.baidu.com/api ...

  7. Android 百度地图开发(三)--- 实现比例尺功能和替换自带的缩放组件

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/11821523 貌似有些天没有写博客了,前段时间在忙找工作的事,面试了几家公司,表示反响 ...

  8. ArcGIS API for Silverlight 入门学习笔记(三):基础地图实例

    该实例主要是包含六部分:地图范围.坐标.动画效果.全屏.比例尺.进度条. 前期准备工作 前台代码0 <UserControl x:Class="APIforSilverlightSam ...

  9. 激光SLAM导航系列(三)Costmap(代价地图)(上)

    Costmap(代价地图)(上) Costmap是机器人收集传感器信息建立和更新的二维或三维地图,可以从下图简要了解. 上图中,红色部分代表costmap中的障碍物,蓝色部分表示通过机器人内切圆半径膨 ...

最新文章

  1. 2020春招即将来袭,送你110道Python面试真题
  2. vbs结束进程代码_物联网学习教程—Linux系统编程之进程控制
  3. apache java windows_Apache for Windows 安装
  4. linux下运行js挖矿,利用 JavaScript 代码挖矿
  5. git搭建局域网服务器
  6. 这回真的是挤时间了-PHP基础(三)
  7. 计算机视觉会议与专家(重排版)
  8. mysql报错注入实战_MySQL手工注入实战
  9. learn python app v3_Python3 采集APP数据及相关配置
  10. Vue3+CLI4 使用Element-ui
  11. ASP.NET MVC:WebViewPage.cs
  12. 查看maven,JDK版本号
  13. Microsemi Libero使用技巧4——使用命令行模式下载程序
  14. JAVA超市管理系统
  15. Word处理控件Aspose.Words功能演示:在C#中将DOC或DOCX转换为HTML
  16. 实数取整(指针专题)
  17. 1697_python编程_assertions and exceptions
  18. Linux Polkit权限提升漏洞(CVE-2021-3560)
  19. 微信小程序 请求数据
  20. python UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 0: invalid start byte

热门文章

  1. php实现云盘下载不限速,【合集】【已更新第五种】五种百度云下载不限速方法+软件...
  2. 实现数据中心间互通的纽带——DCI技术
  3. 如何运用Common Neighbor方法进行链路预测
  4. 免费23年的Java开始收费了,程序员怎么办?
  5. 用java制作扑克牌_利用java实现简单的扑克牌小游戏
  6. java 设计模式之三-模版模式
  7. 怎样寻找最佳爱人:一个微积分求解的离散数学问题
  8. 喜剧悲剧:《Friends》(六人行/老友记)纪念
  9. 如何突破卫星影像建模难点?重建大师这样做!
  10. 基于SIP协议的IP电话系统设计与实现