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

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

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

在这里插入图片描述

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

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

二、 引入插件

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.value
                that.scope = that.point.scope
                map.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.point
                const 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 = this
            that.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); //根据所选坐标重新创建Marker
            that.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>
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: vue-baidu-map是基于百度地图API开发的Vue组件,它能够在Vue项目中轻松地集成百度地图。而离线百度地图是指在没有网络连接的情况下能够使用百度地图。 正常情况下,百度地图需要联网才能显示地图和获取相关数据。但是在有些场景下,比如地下车库或者偏远山区等网络较差或者没有网络的地方,就无法正常使用百度地图。离线百度地图就是解决这个问题的解决方案,它通过事先将地图数据下载保存在本地,使得在没有网络连接的情况下也能够正常使用百度地图。 对于vue-baidu-map来说,它的离线功能也是很重要的。通过使用vue-baidu-map的离线百度地图,开发者可以轻松地针对不同的场景选择不同的地图方案,提高用户的使用体验。而且离线百度地图对于一些隐私性、安全性要求高的应用场景也更加有优势。 总之,vue-baidu-map离线百度地图是一个非常实用与方便的工具,在实际生产中可以提供更好的用户体验,也为开发者提供了更多的选择与便捷。 ### 回答2: vue-baidu-map离线百度地图是一款基于Vue.js框架开发的百度地图组件库,主要解决的是在无网络或网络不稳定的情况下,无法使用在线地图的问题。该组件库提供的离线地图可以在无网络的情况下正常使用,并且具有与在线地图相同的功能和可视化效果。 在vue-baidu-map离线百度地图组件中,我们可以使用百度地图提供的基础地图、卫星地图、混合地图等多种地图样式,并且支持地名搜索、地图缩放、位置探测、路线规划等常用功能。此外,在使用vue-baidu-map离线百度地图时,我们也可以通过添加自定义数据层、自定义覆盖层等方式,对地图进行更加丰富的扩展和定制。 总之,vue-baidu-map离线百度地图是一款非常实用的地图组件库,它可以帮助我们在网络不稳定或者无网络的情况下,依然可以正常使用百度地图,并且具有完整的百度地图功能和美观的地图样式。如果你正在建设一个应用程序,需要集成离线地图功能,那么vue-baidu-map离线百度地图可能正是你需要的工具。 ### 回答3: vue-baidu-map是一个可嵌入Vue.js网站的地图组件,它集成了百度地图的API,可以让开发者轻松实现地图功能。 离线百度地图指的是一种不需要联网即可使用百度地图,也就是地图数据被下载到本地储存设备上。vue-baidu-map支持使用离线地图,这就意味着用户可以在没有网络的环境下依然能够使用地图功能。 使用vue-baidu-map离线百度地图的优势在于,它提供了更好的用户体验和更高的灵活性。首先,用户无需担心网络不稳定、信号弱的问题,可以随时随地使用地图进行定位、浏览、搜索等操作;其次,开发者可以根据具体需求选择下载特定区域的地图数据,以减少数据流量和缩短加载时间;还可以根据不同需求切换在线地图和离线地图,达到最佳的用户体验。 总之,vue-baidu-map离线百度地图为开发者提供了便捷灵活的地图开发工具,让用户可以更加自由地使用地图服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值