您现在的位置是:网站首页> 编程资料编程资料
vue中百度地图定位及附近搜索功能使用步骤_vue.js_
2023-05-24
255人已围观
简介 vue中百度地图定位及附近搜索功能使用步骤_vue.js_
1.地图初始化相关
申请账号 => 创建应用 => 生成key值 => 引入百度地图,替换key值
在出口html(public/html)文件下引入标签
2.获取当前定位
文档:lbsyun.baidu.com/index.php?t…
创建地图容器 可以为其他id名, 但必须有 用来展示地图, 地图大小与container大小一致
获取当前位置
*注意,初始化地图,获取定位时,必须在mounted钩子中,因为要先获取到地图容器
对于全局变量需要加上window(脚手架规则)
// 地图获取当前定位 getPosition(){ // 创建地图实例 const map = new window.BMapGL.Map('container') // 创建浏览器定位实例 var geolocation = new window.BMapGL.Geolocation(); let that = this geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ // 创建点标记 var mk = new BMapGL.Marker(r.point); // 在地图上添加点标记 map.addOverlay(mk); // 将地图平移至中心点 map.panTo(r.point); console.log('您的位置:' + r.point.lng + ',' + r.point.lat); that.position.signLongitude = r.point.lng that.position.signLatitude = r.point.lat // 创建点坐标 const point = new window.BMapGL.Point(r.point.lng, r.point.lat) // 初始化地图,设置中心点坐标和地图级别 map.centerAndZoom(point, 30) // 逆向编码 var myGeo = new BMapGL.Geocoder(); myGeo.getLocation(new BMapGL.Point(r.point.lng,r.point.lat), function(result){ if (result){ that.address = result.address // 获取逆编程的地址结果 } }); }else{ alert('failed' + this.getStatus()); } }); }, 钉钉签到定位完整代码
2022年08月26日地址微调
{{address}}
签到{{dateNow}}未签到
3.根据当前定位地址附近搜索建议
文档:lbsyun.baidu.com/jsdemo.htm#…
根据当前定位的结果,给出建议相关列表
html相关:
js相关:
mounted () { // 创建地图实例 const map = new window.BMapGL.Map('container-map') // 创建浏览器定位实例 var geolocation = new window.BMapGL.Geolocation(); let that = this geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ // 创建点标记 var mk = new BMapGL.Marker(r.point); // 在地图上添加点标记 map.addOverlay(mk); // 将地图平移至中心点 map.panTo(r.point); // console.log('您的位置:' + r.point.lng + ',' + r.point.lat); const point = new window.BMapGL.Point(r.point.lng, r.point.lat) that.lng = r.point.lng that.lat = r.point.lat console.log(r.point.lng, r.point.lat); map.centerAndZoom(point, 30) // 逆向编码 var myGeo = new BMapGL.Geocoder(); myGeo.getLocation(new BMapGL.Point(r.point.lng,r.point.lat), function(result){ if (result){ console.log(result.address); // 获取到当前定位的结果, 调用搜索建议 that.getLocation(result.address) } }); }else{ alert('failed' + this.getStatus()); } }); // const point = new window.BMapGL.Point(116.404, 39.915) // map.centerAndZoom(point, 30) }, methods:{ // 搜索建议 getLocation(address){ var map = new BMapGL.Map("container-map"); var mPoint = new BMapGL.Point(this.lng, this.lat); map.enableScrollWheelZoom(); map.centerAndZoom(mPoint,15); // 绘制圆形范围覆盖物 var circle = new BMapGL.Circle(mPoint,1000,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.3, strokeOpacity: 0.3}); map.addOverlay(circle); var local = new BMapGL.LocalSearch(map, {renderOptions: {map: map, autoViewport: false}}); // 定义搜索地址,以及范围距离 local.searchNearby(address,mPoint,1000); console.log(local); this.addressList = local._arrPois }, // 点击选择地址 lng 经度 lat 维度 chooseMap(addressItem){ this.addressDetail=addressItem.address // 经度 const lng = addressItem.marker.latLng.lng // 维度 const lat = addressItem.marker.latLng.lat this.$router.replace(`/signIn?address=${this.addressDetail}&planId=${this.$route.query.planId}&lng=${lng}&lat=${lat}`) // that.$emit('getAddress', this.addressDetail) } } 完整代码:
{{address}}