当前位置: 首页 > news >正文

vue 百度地图 使用 vue-baidu-map 进行当前位置定位和范围展示

vue 百度地图 使用 vue-baidu-map 进行当前位置定位和范围展示(考勤打卡)

  • 一、创建百度地图账号,获取秘钥
  • 二、 引入插件
    • 1、安装vue-baidu-map
    • 2、在main.js中引入
  • 三、 简单使用

最近写项目的时候,做到了考勤打卡的模块内容,需要选择考勤打卡的位置信息以及打卡的范围展,所以做出以下的记录,方便大家参考学习(如下图展示)

在这里插入图片描述

一、创建百度地图账号,获取秘钥

首先得有百度地图的账号,点此链接(百度地图)

二、 引入插件

1、安装vue-baidu-map

npm install vue-baidu-map --save

2、在main.js中引入

import Vue from 'vue'
import BaiduMap from 'vue-baidu-map'Vue.use(BaiduMap, {ak: '此处为百度地图申请的密钥'
})

三、 简单使用

以下就不多介绍了,直接上完整代码

// 引入
npm install vue-baidu-map --save

引入map.vue页面

// point传值  gainLocation 获取点位的信息
<map :point="point"  @gainLocation="gainLocation" ></map>data() {return {point:{"lng": 120.306731,  //坐标"lat": 31.581733, value:'三阳广场',    // 位置信息scope:50			// 范围}}},gainLocation(row){console.log(row)}

创建map.vue页面

<template><div><el-button @click="open">打开地图</el-button<el-dialog title="地图" :visible.sync="dialogs" v-if="dialogs" ><div><el-autocomplete style="width: 100%" :popper-append-to-body="false"v-model="value" :fetch-suggestions="querySearchAsync" :trigger-on-focus="false" placeholder="输入地址查找点位信息"@select="handleSelect" ><i slot="prefix" class="el-input__icon el-icon-search"></i><template slot-scope="{ item }"><div class="flexs"><i class="el-icon-location" style="font-size:25px;color:#409eff"></i><div style="margin-left:15px" class="flcol"><span style="color:#409eff;">{{ item.title }}</span><span style="color:#999">{{ item.address }}</span></div></div></template></el-autocomplete></div><div ><baidu-map class="map"  :center="circleCenter" :zoom="zoom" :scroll-wheel-zoom="true" @ready="handler" /></div><span slot="footer" class="dialog-footer"><el-button @click="subMit" type="primary">确 定</el-button><el-button @click="dialogs = false">取消</el-button></span></el-dialog></div>
</template><script>
import { BaiduMap, } from "vue-baidu-map";
export default {components: {BaiduMap,},props:['point'],data() {return {dialogs: false,value: '',circleCenter: { // 点位信息lng: 116.404,lat: 39.915},map: {},scope:50, // 范围zoom:20,  // 地图 视线大小circleStyle:{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.3, strokeOpacity: 0.3},}},mounted() {//通过浏览器的Geolocation API获取经纬度if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(this.showPosition);} else {console.log("Geolocation is not supported by this browser.");}},methods: {showPosition(position) {const latitude = position.coords.latitude;const longitude = position.coords.longitude;this.circleCenter = {lng: longitude,lat: latitude,};},handler({ BMap, map }) {let that = this// 此处是根据组件传递展示范围和定位信息 (根据自己要求更改)if (that.point) {that.circleCenter = {lng: that.point.lng,lat: that.point.lat}that.value = that.point.valuethat.scope = that.point.scopemap.centerAndZoom(new BMap.Point(that.circleCenter.lng, that.circleCenter.lat));var marks = new BMap.Marker(this.circleCenter); map.addOverlay(marks); var circle = new BMap.Circle(that.circleCenter,that.scope,that.circleStyle);map.addOverlay(circle);}that.map = map;var geocoder = new BMap.Geolocation(); //通过百度地图 创建地址解析器的实例   获取经纬度geocoder.getCurrentPosition(function (res) {var point = res.pointconst currentLocation = [res.longitude, res.latitude];console.log( "当前位置经纬度", currentLocation,res.address.province, res.address.city);if (!that.point) {var gc = new BMap.Geocoder(); gc.getLocation(point,function(rs){that.value = rs.address})that.circleCenter = {lng: currentLocation[0],lat: currentLocation[1],};map.centerAndZoom(new BMap.Point(currentLocation[0], currentLocation[1]));var marks = new BMap.Marker(point); map.addOverlay(marks); var circle = new BMap.Circle(that.circleCenter,that.scope,that.circleStyle);map.addOverlay(circle);}});},open() {this.dialogs = true},   querySearchAsync(str, cb) {var options = {onSearchComplete: function (res) {console.log(res,111);//检索完成后的回调函数var s = [];if (local.getStatus() == BMAP_STATUS_SUCCESS) {for (var i = 0; i < res.getCurrentNumPois(); i++) {s.push(res.getPoi(i));}cb(s); //获取到数据时,通过回调函数cb返回到<el-autocomplete>组件中进行显示} else {cb(s);}},};var local = new BMap.LocalSearch(this.map, options); //创建LocalSearch构造函数local.search(str); //调用search方法,根据检索词str发起检索console.log(str);},handleSelect(item) {let that = thisthat.value = item.address +'-' +item.title; //记录详细地址,含建筑物名that.circleCenter = { //记录当前选中地址坐标lng: item.point.lng,lat: item.point.lat,};that.map.clearOverlays(); //清除地图上所有覆盖物const marks = new BMap.Marker(item.point); //根据所选坐标重新创建Markerthat.map.addOverlay(marks); //将覆盖物重新添加到地图中that.map.panTo(item.point); //将地图的中心点更改为选定坐标点const circle = new BMap.Circle(that.circleCenter,that.scope,that.circleStyle);that.map.addOverlay(circle);},subMit(){const obj = { ...this.circleCenter,value:this.value }this.$alert(obj)this.$emit('gainLocation',obj)}}
}
</script><style scoped>
.map {margin-top: 20px;width: 100%;height: 300px;
}.flexs {display: flex;align-items: center;width: 100%;border-bottom: 1px solid #f5f5f5;
}
.flcol{display: flex;flex-direction: column;
}
</style>
http://www.lryc.cn/news/337755.html

相关文章:

  • 使用idea运行程序,发现控制台的中文出现乱码
  • 基于javassm实现的大学生兼职信息系统
  • O2OA开发平台如何查看数据表结构?
  • 心理测评性格测试矩阵版h5微信抖音QQ快手小程序app开源版开发
  • 【蓝桥杯】十六进制转八进制 C++实现
  • 明明设置数字居中对齐,为什么excel的数字却不居中?
  • 深入解析API技术:原理、实现与应用
  • C语言——数组指针变量
  • Redis的过期策略与内存淘汰机制原理及实践
  • 【24届数字IC秋招总结】提前批面试经验1——小米、百度昆仑芯、长鑫存储
  • 第7章、ReactRedux 实战 - 登录注册验证;
  • 16路HDMI+AV流媒体IPTV高清编码器JR-3216HD
  • vscode 配置文件settings.json和c_cpp_properties.json的作用
  • 【postgresql 基础入门】入门教程成形了,八大章节,涵盖库,表,事务,约束,数据类型,聚集函数,轻松入门
  • 【计算机毕业设计】人事管理系统——后附源码
  • OceanBase V4.2 MySQL模式下,如何通过DBLINK实现跨数据源访问
  • 再谈C语言——理解指针(一)
  • day21-二叉树part08
  • 【WPF应用42】WPF中的 GroupBox 控件详解
  • LeetCode-72. 编辑距离【字符串 动态规划】
  • 多张静图合成gif怎么做?一键极速合成gif
  • Es中bool 查询中的四个(must must_not should filter)
  • Docker容器嵌入式开发:Docker Ubuntu18.04配置mysql数据库
  • C++类和对象中上篇
  • 基于linux进一步理解核间通讯
  • 应用实战|从头开始开发记账本2:基于模板快速开始
  • 学习前端第二十天(条件分支:if 和 ‘?‘;逻辑运算符)
  • C++11的更新介绍(lamada、包装器)
  • Golang 实现一个简单的 RPC 服务
  • Linux系统(centos,redhat,龙芯,麒麟等)忘记密码,怎么设置新的密码