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

uniapp小程序持续获取用户位置信息,后台位置获取

做一个小程序持续获取用户位置信息的功能,即使小程序切换到后台也能继续获取,getLocation这个api只有小程序在前台才能获取位置,所以不用这个

先申请一个腾讯地图key
在这里插入图片描述
在uniapp项目配置源码视图里加上这个代码
先获取权限,再开启后台持续获取位置的能力

"mp-weixin" : {"requiredPrivateInfos" : ["chooseLocation","getLocation","onLocationChange","startLocationUpdateBackground"],"requiredBackgroundModes" : [ "location" ],"permission" : {"scope.userLocation" : {"desc" : "方便您使用更完整的功能"},"scope.startLocationUpdateBackground" : {"desc" : "方便获取您的位置"}}
},

下面是使用页面的代码

//获取位置权限
setPosition(){const thle = this;uni.getSetting({success(res) {console.log('权限权限',res.authSetting)if (!res.authSetting['scope.userLocation']) {uni.authorize({scope: 'scope.userLocation',success() {console.log("用户成功授权位置信息")thle.keepPosition()},fail() {console.log("用户拒绝授权位置信息,再次提示用户授权")thle.showRefuseLocationPermission('nonono')}})}else if(!res.authSetting['scope.userLocationBackground']){//后台获取位置变化的权限uni.authorize({scope: 'scope.userLocationBackground',success() {console.log("用户成功授权位置信息")thle.keepPosition()},fail() {console.log("用户拒绝授权位置信息,再次提示用户授权")thle.showRefuseLocationPermission('nonono')}})}else{thle.keepPosition() }}})
},
//持续上传位置
keepPosition(){const thle = this;// uni.startLocationUpdate({//   success: res => console.log('开启前台位置成功'),//   fail: err => console.error('开启前台位置失败:', err),// });//开启前后台位置获取uni.startLocationUpdateBackground({success: res =>{console.log('前后台位置成功')uni.onLocationChange(rr=> {console.log('持续位置:',rr);console.log('纬度:' + rr.latitude);console.log('经度:' + rr.longitude);thle.updataPosition(rr.latitude,rr.longitude)});},fail: err => console.error('前后台位置失败:', err),});
},
//更新位置
updataPosition(latitude,longitude){const thle = this;console.log('更新位置',latitude,longitude)uni.request({url: `https://apis.map.qq.com/ws/geocoder/v1/?key=${'腾讯地图key'}&location=`+ latitude + ',' +longitude,success: res => {console.log('解析成功',res) let address = res.data.result.address;//经纬度解析后的地址}})
},
// 用户拒绝授权的展示
showRefuseLocationPermission(e) {const that = this;uni.showModal({title: "提示",content: "需要获取用户位置以及后台定位权限",confirmText: "前往设置",showCancel: false,success(res) {if (res.confirm) {uni.openSetting({success: (res) => {console.log("打开设置成功", res);if (res.authSetting['scope.userLocation']) {console.log('成功授权userLocation')} else {console.log('用户未授权userLocation')// 递归调用本函数,(函数套函数)that.showRefuseLocationPermission()}},fail: (err) => {console.log("打开设置失败", err)}})}}})
},
http://www.lryc.cn/news/446557.html

相关文章:

  • 优化算法(五)—梯度下降算法(附MATLAB程序)
  • TypeScript 设计模式之【单例模式】
  • UDP与TCP那个传输更快
  • 如何把PDF样本册转换为网址链接
  • centos7 semanage 离线安装 SELinux
  • 磨具生产制造9人共用一台工作站
  • Qt clicked()、clicked(bool)、toggled(bool)信号的区别和联系
  • nginx实现负载均衡的分发策略
  • 【Python】用代码片段掌握Python核心功能
  • JVM 内存模型
  • Linux2.6* 内核默认支持的文件系统
  • PMP--二模--解题--111-120
  • idea 创建多模块项目
  • redis Redis-Cluster常用命令与Redis性能监控
  • 《C++中的随机数生成器:探索随机之美》
  • 为什么推荐使用英文版LabVIEW
  • 【Moveit2】move_group_interface_tutorial中文注释
  • JavaScript window的open和close用法
  • 经典sql题(十四)炸裂函数的恢复
  • 【vue2】组件写法
  • 5G 扬帆新质跃,技术蝶变开新篇-第七届“绽放杯”5G应用征集大赛 5G应用融合技术专题赛圆满收官
  • 3d gaussian splatting公式推导
  • 金属增材制造咋突破?纳米纹理粉末如何助力金属增材制造?
  • openpnp - 为了防止物料操作混乱,做一张物料分布位置图清晰一些
  • 懒人帮美食系统小程序的设计
  • David律所代理Jose Martin幽默水果版权首发维权,尚未TRO
  • 读构建可扩展分布式系统:方法与实践15可扩展系统的基本要素
  • 解决VisualStudio msvsmon.exe远程调试器未启动问题
  • 如果淘汰是注定的,那么读书还有意义吗?
  • Python 中 三种常用的绘图方式 ! ! !