Vue中实现电子围栏/围栏(高德地图)功能:

2023-11-12

1.思路(大部分与车辆轨迹相同):
【1】先获取key=>官网:https://lbs.amap.com/?ref=https://console.amap.com
【2】下载并导入依赖=》npm install vue-amap -S
【3】使用
2、官网=》右上角控制台

在这里插入图片描述

3、使用过程:

【1】安装依赖:

npm install vue-amap --save-dev

【2】main.js中注册:

import AMap from 'vue-amap'
Vue.use(AMap)
AMap.initAMapApiLoader({
  key: '你申请的key',
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType','AMap.MouseTool','AMap.Polygon', 'AMap.PolyEditor', 'AMap.CircleEditor'],
  // 默认高德 sdk 版本为 1.4.4
  v: '1.4.4'
})

【3】页面中使用:高德地图API【左键绘制,右键结束退出】
获取到的points数组:
在这里插入图片描述
编辑完成的数组:
在这里插入图片描述

/* 
    车辆轨迹
 */
<template>
    <div class="main">
        <section class="section">
                <!-- 地图 -->
                <div id="container" style="width:100%;height:100%" />

                <!-- 控制按钮组 -->
                <div class="ebox">
                    <el-button-group>
                        <el-button type="info" icon="el-icon-circle-plus-outline" @click="drawRectangle">绘制围栏</el-button>
                        <el-button type="primary" icon="el-icon-edit" @click="editRectangle">编辑围栏</el-button>
                        <el-button type="warning" icon="el-icon-delete" @click="cancelRectangle">取消编辑</el-button>
                        <el-button type="success" icon="el-icon-success" @click="saveRectangle">保存围栏</el-button>
                        <el-button type="danger" icon="el-icon-delete" @click="deleRectangle">删除围栏</el-button>
                    </el-button-group>
                </div>
        </section>
    </div>
</template>

<script>
import { geofenceList } from '@/api/manage/fenceAPI.js' //获取围栏数据

export default {
    name: 'dashboard',
    data() {
        return {
            formData: {
                carId: '',
                pageNum: 1,//当前页
                pageSize: 10,//页长
                pageTotal: 0,//总数
            },
            
            map: null,
            centerArr: [113.760234, 23.048884],//地图中心位置,不能为空数组【为空数组会报错】
            path: [],//以前绘制的数据
            paths: [], // 当前绘制的多边形经纬度数组
            polygonItem: [], // 地图上绘制的所有多边形对象
            polyEditors: [],// 新增数据=>所有编辑对象数组
            polyEditorsBefore: [],// 以前历史数据=>进入编辑对象数组
        }
    },
    created() {
        this.init()
    },
    mounted() {
        setTimeout(() => {//异步加载(否则报错initMap is not defined)
            this.initMap() 
        }, 1000)
    },
    methods: {
        // 地图初始化
        initMap() {
            this.map = new AMap.Map('container', {
                resizeEnable: true, // 窗口大小调整
                center: this.centerArr, // 中心
                zoom: 15,//放大级别
                showLabel: true, // 是否显示地图文字标记
            })
            // 添加工具栏
            this.map.plugin(['AMap.ToolBar', 'AMap.Scale'], () => {
                const toolbar = new AMap.ToolBar()// 工具条
                const scale = new AMap.Scale()// 比例尺
                this.map.addControl(toolbar)
                this.map.addControl(scale)
            })
        },
        // 绘制多边形
        drawRectangle() {
            const This = this;
            let mouseTool = new AMap.MouseTool(this.map);
            const polygon = mouseTool.polygon({
            //polygon:绘制多边形【线段:polyline;矩形:rectangle;圆:circle】
                strokeColor: 'red',
                strokeOpacity: 0.4,
                strokeWeight: 6,
                fillColor: '#1791fc',
                fillOpacity: 0.2,
                // strokeStyle还支持 solid
                strokeStyle: 'solid',
                // strokeDasharray: [30,10],
            });
            mouseTool.on('draw', function (event) {
                // event.obj 为绘制出来的覆盖物对象
                let polygonItem = event.obj;
                let paths = polygonItem.getPath();//取得绘制的多边形的每一个点坐标
                // console.log('覆盖物对象绘制完成各个点的坐标', paths, event);

                let path = [];  // 编辑的路径
                paths.forEach(v => {
                    path.push([v.lng, v.lat])
                });
                This.paths = path //将新增数据放入paths数组里
                // This.editRectangle();//绘制完成,默认进入编辑状态
                This.polygonItem.push(event.obj);
                // This.map.remove(event.obj); // 删除多边形
                // console.log(polygon, '------polygon-----');
            });
        },
        // 编辑围栏
        editRectangle() {
            const path = this.paths;
			//新增的进入编辑状态
            let polygon = new AMap.Polygon({
                path: path,
                strokeColor: "#FF33FF",
                strokeWeight: 6,
                strokeOpacity: 0.2,
                fillOpacity: 0.2,
                fillColor: '#1791fc',
                zIndex: 50,
            });
            this.map.add(polygon);
            this.polygonItem.push(polygon);
            // 缩放地图到合适的视野级别
            this.map.setFitView([polygon]);

            this.polyEditor = new AMap.PolyEditor(this.map, polygon);
            this.polyEditor.open();
            this.polyEditors.push(this.polyEditor);

			//历史围栏的进入编辑状态
            let polygonBefore = new AMap.Polygon({
                path: this.path,
                strokeColor: "#FF33FF",
                strokeWeight: 6,
                strokeOpacity: 0.2,
                fillOpacity: 0.2,
                fillColor: '#1791fc',
                zIndex: 50,
            });
            this.map.add(polygonBefore);
            this.polygonItem.push(polygonBefore);
            // 缩放地图到合适的视野级别
            this.map.setFitView([polygonBefore]);

            this.polyEditorBefore = new AMap.PolyEditor(this.map, polygonBefore);
            this.polyEditorBefore.open();
            this.polyEditorsBefore.push(this.polyEditorBefore);

            // this.polyEditor.on('addnode', function (event) {
            //     console.info('触发事件:addnode', event)
            //     console.info('修改后的经纬度:', polygon.getPath())
            // });

            // this.polyEditor.on('adjust', function (event) {
            //     console.info('触发事件:adjust', event)
            //     console.info('修改后的经纬度:', polygon.getPath())
            // });

            // this.polyEditor.on('removenode', function (event) {
            //     console.info('触发事件:removenode', event)
            //     console.info('修改后的经纬度:', polygon.getPath())
            // });

            // this.polyEditor.on('end', function (event) {
            //     console.info('触发事件: end', event)
            //     console.info('end修改后的经纬度:', polygon.getPath())
            //     // event.target 即为编辑后的多边形对象
            // });
        },
        // 取消编辑状态
        cancelRectangle() {
            this.polyEditors.forEach(item => { item.close(); });//新增
            this.polyEditorsBefore.forEach(item => { item.close(); });//历史
        },
        //保存围栏
        saveRectangle() {
   			// 取消编辑状态
            this.polyEditors.forEach(item => { item.close(); });
            this.polyEditorsBefore.forEach(item => { item.close(); });
			// 保存  console.log(this.paths,this.path)=>成功(重新刷新页面)
			...
        },
        // 删除围栏
        deleRectangle() {
            this.polyEditors.forEach(item => { item.close(); });// 取消编辑状态
            this.polyEditorsBefore.forEach(item => { item.close(); });// 取消编辑状态
            this.map.clearMap(); // 删除地图所有覆盖物
            //删除=>成功(重新刷新页面)
            ...
        },
        //获取后台数据
        init() {
            const that = this
            let param = {
                carId: this.formData.carId,//string true carID
                pageNum: this.formData.pageNum,//string false 当前页数
                pageSize: this.formData.pageSize,//string false 每页条数
            }
            geofenceList({ param }).then(res => {
                if (res.data.code == 0) {
                    if (res.data.data.list.length==0) {
                        this.$message.error('没有围栏数据')
                        return
                    }
                    that.path=[]
                    that.map.clearMap(); // 删除地图所有覆盖物
                    res.data.data.list.forEach((item, index) => { //同时展示多个围栏
                        that.path.push(item.points)//编辑时,可以一起编辑
                        // this.centerArr = that.path[0]
                        // this.initMap()

                        that.map.add(new AMap.Polygon({
                            path: item.points,
                            strokeColor: "#FF33FF",
                            strokeWeight: 6,
                            strokeOpacity: 0.2,
                            fillOpacity: 0.4,
                            fillColor: "#1791fc",
                            zIndex: 50,
                        }));
                        that.map.setFitView();
                   })
                } else {
                    this.$message.error(res.data.msg)
                }
            })
        },
    }
};
</script>


<style scoped lang="scss">
	@import url("https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css");
</style>
4、效果:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue中实现电子围栏/围栏(高德地图)功能: 的相关文章

随机推荐

  • 手把手教你,把3D模型从stl格式导出iges格式的方法

    工具 Hypermesh 注意 下载和安装视频在我的上传资源里面 记得安装路径不能有中文 自己的操作账户名也不能是中文的 方法 第一 按照如下步骤 导入stl模型 第二步 点击Shaded 按钮 显示实体网格 第三步 点击Geom下的sur
  • 自己搭的12V 电机驱动电路设计

    1 单MOS管驱动电路 采用P75NF75为主角构成 2 双MOS管电机驱动电路设计 采用D4184管组合 3 半桥驱动电路设计 采用BTS7960芯片 两路半桥构成全桥驱动电路
  • s3c6410 android 移植Step by step

    Report 2009 03 05 转载请说明出处 不得用于商业用途 Linux forum id hongjiujing Porting android on s3c6410 Environment ubuntu 8 10 Board X
  • KeilMDK编译错误Error: L6218E: Undefined symbol __aeabi_assert (referred from xxx.o).

    问题描述 AirPressure AirPressure axf Error L6218E Undefined symbol aeabi assert referred from mbrtu o 问题原因 Error L6218E Unde
  • 爬虫乱码UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa0’ in position

    在Python中将网址写入文件的时候 会碰到 UnicodeEncodeError gbk codec can t encode character xa0 in position 0这个问题 其实就是在windows中 新建的文本文件的默
  • Connecting the Dots: 基于图神经网络的多元时间序列预测——学习合集

    关于源代码和数据集的理解可以参考以下几篇文章 1 github源代码 数据集 安装环境要求 2 原论文百度云链接 提取码 1234 3 帮助理解论文的文章1 4 帮助理解论文的文章2 5 帮助理解论文的文章3 6 交通流量预测数据集解读 7
  • UnityVR--小程序8--激光门伤害

    本文使用Line Renderer组件 在门框中画出一道激光线 被线照射到的主角将会扣分 另外 激光仅检测ragCast层 所以主角必须添加到ragCast层中 与坦克对战小程序 UnityVR 小程序7 坦克对战 的设置相同 1 在场景中
  • MySQL多表查询 (超详细)

    一 多表关系 项目开发中 在进行数据库表结构设计时 会根据业务需求及业务模块之间的关系 分析并设计表结构 由于业务之间相互关联 所以各个表结构之间也存在着各种联系 基本上分为三种 一对多 多对一 多对多 一对一 1 1 一对多 案例 部门与
  • 在线算法(online algorithm)--竞争性分析

    文章目录 一 Problem Setting 1 1 competitve analysis 1 2 page replacement 二 Deterministic Online Algorithms 2 1 deterministic
  • 穆土 的学习和生活网站

    前端学习 哔哩哔哩 哔哩哔哩中有很多的 up 主 都有学习的资源 你可以学到等多东西 个人关注的 up 主 pink 老师 黑马培训的一位老师 他带领了很多人进入了前端的领域 我看他的 html css js 的入门商品 珠峰培训官方 周萧
  • 任意版本pytorch-gpu环境搭建方法

    任意版本pytorch gpu环境搭建方法 tortorish的博客 CSDN博客 网上关于pytorch gpu环境搭建的教程非常多 但若在国内 直接使用pytorch官网上的命令 经常会遇到下载过慢的情况 而若使用清华源 阿里源等网站
  • WindowBuilder for Eclipse 3.7

    http download eclipse org windowbuilder WB integration 3 7 MyEclipse10 7 1和MyEclipse10 7的下载地址 http downloads myeclipseid
  • 深聊自动化测试之:10年小鱼给你10条建议,让你在自动化界占据一个墙角

    10年小鱼的自动化建议 1 哪一刻 让你想起了自动化 1 1 执行回归测试 1 2 压测场景执行并发 1 3 UI稳定 接口不断升级 2 七问 是否了解自动化风险 2 1 团队成员的资历 2 2 自动化成本投入产出比 2 3 慎重对待UI级
  • Python中eval()函数的使用

    今天给大家分享一下Python中的eval 函数 如果感觉博主的文章还不错的话 希望大家点赞支持一下博主 文章目录 eval 函数 语法 实例 实例1 实例2 实例3 eval 函数 eval 函数用来执行一个字符串表达式 并返回表达式的值
  • 【Spring传播机制底层原理】

    一 Spring的事务传播机制 Spring的事务传播机制是Spring框架中最核心的机制之一 它能够灵活地控制多个事务方法的执行顺序 提交或回滚等行为 在Spring中 事务是通过TxManager来管理的 TxManager是一个接口
  • Hyper-V:无法打开虚拟机,因为虚拟机监控程序未运行

    管理员权打开 cmd 窗口 输入 bcdedit set hypervisorlaunchtype Auto 前提是机器已经开启了 虚拟化和开启了虚拟机监控程序
  • kafka 应用实战

    一 Java 中使用 kafka 进行通信 依赖
  • 孟岩谈通证(11):通证所代表的多维价值观

    通证Token都有那些多维价值观呢 我还是你举一个特别具体的例子来说明 我有一个朋友他开了一个社区 是专门做中小学教育课外辅导老师之间交流的 社区里大家互相交换教学资料 试题等等材料 你说这个东西有没有价值 教书育人 然后帮助孩子们成长 这
  • ssh远程连接Ubuntu(局域网和非局域网)

    文章目录 前言 1 局域网 远程连接 2 非局域网 远程连接 3 Zerotier常用命令 4 远程桌面控制 总结 前言 我们通常使用ssh连接虚拟机中的Ubuntu 方便学习 但是当在项目中遇到远程控制主机的时候 发现ssh连接不到外网主
  • Vue中实现电子围栏/围栏(高德地图)功能:

    1 思路 大部分与车辆轨迹相同 1 先获取key gt 官网 https lbs amap com ref https console amap com 2 下载并导入依赖 npm install vue amap S 3 使用 2 官网