首页 > 其他分享 >uniapp中wx.startLocationUpdateBackground切换到后台仍能间隔上报当前位置

uniapp中wx.startLocationUpdateBackground切换到后台仍能间隔上报当前位置

时间:2023-05-18 11:23:21浏览次数:34  
标签:uniapp startLocationUpdateBackground false success res wx GPS

1.在manifest.json中修改

"mp-weixin" : {
	"appid" : "*******",//自己的appid
	"setting" : {
		"urlCheck" : false
	},
	"usingComponents" : true,
	"requiredPrivateInfos" : [ 
		"chooseLocation", 
		"getLocation",
		"onLocationChange",//配合startLocationUpdateBackground获取当前经纬度
		"startLocationUpdateBackground" //必填
	],
    //加上"location"是为了在权限设置里面增加“使用小程序期间和离开小程序后”
    //获取权限默认选项是“在使用小程序期间”,不能默认“使用小程序期间和离开小程序后”,需要用户自己选择
	"requiredBackgroundModes": ["location"],//必填
	"permission" : {
		"scope.userLocation" : {
			"desc" : "你的位置信息将用于小程序位置接口的效果展示"
		}
	}
},

2.在你所需要添加的页面增加一下代码

getRealTimeLocaltion(){
	this.getDeviceGPS()//获取本机GPS状态
	wx.startLocationUpdateBackground({
		type: 'wgs84',
		success: (res) => {
			this.getBackgroundLocation();
		},
		fail: (err) => {
			//授权失败后引导用户打开定位信息
			uni.getSetting({
				success: (res) => {
					var statu = res.authSetting;
					if (!statu["scope.userLocationBackground"]) {
						uni.showModal({
							title: "是否授权在使用期间和离开后!",
							content: "需要获取您当前的位置信息,请在位置信息中选择",
							success: (tip) => {
								if (tip.confirm) {
									uni.openSetting({
										success: (data)=> {
											if (data.authSetting["scope.userLocationBackground"] == true) {
												this.getBackgroundLocation();
											}
										}
									});
								} else {
									console.log('用户拒绝打开设置界面')
								}
							}
						});
					}
				}
			});
		}
	});
},
// 获取本机设备的GPS信息
getDeviceGPS() {
	let system = uni.getSystemInfoSync();
	//如果设备的GPS未开启,locationEnabled这里会返回false,以此判断
	if (system.locationEnabled == false) {
		//GPS未开启,返回false
		this.userGPS = false
		return uni.showModal({
			content: '请在系统设置中开启GPS定位权限',
			showCancel: false,
			confirmText: '确认',
			success: (res) => {
				console.log('res:',res)
			},
		})
	} else {
		//已开启,返回true
		this.userGPS = true
	}
},
getBackgroundLocation() {
	//判断是否也开通了wx.onLocationChange接口且检测本机GPS功能是否开启,GPS不开启不会间接获取
	if (wx.onLocationChange && this.userGPS != false) {=
		wx.onLocationChange((data) => {
            //微信开发工具上面只能触发一次,真机上面是循环获取
            //设置一个公共时间,原因是时间间隔太短,自己设置时间,然后去操作相应的逻辑
			if (this.time == 11){
				// 调取实时位置
				this.reportLocation(this.location);
				this.time = 0;
			}
			this.time ++;
			this.location = data;
		});
	}
},
//调取位置后的操作逻辑
reportLocation(){
    
}

 

标签:uniapp,startLocationUpdateBackground,false,success,res,wx,GPS
From: https://www.cnblogs.com/xmpz/p/17411367.html

相关文章

  • uniapp APP内嵌 h5 解决web项目发布新版本需要清除浏览器缓存的问题
    1、新建index.html,写入禁止缓存的meta<!--设置meta不缓存--><metahttp-equiv="Expires"content="0"><metahttp-equiv="Pragma"content="no-cache"><metahttp-equiv="Cache-control"content="......
  • uniapp测试环境下安卓端和iOS端打包流程
    方法步骤:1.选中你要打包的项目 2.打包对应的应用系统,以下分为安卓端和iOS端 一、安卓端:安卓证书申请流程如下: 参考地址:https://ask.dcloud.net.cn/article/35777二、iOS端: iOS端证书申请流程如下:1.描述: 2.准备环境3.登录iOSDevCenter打开网站 iOSDe......
  • 关于uniapp条件编译ifdef, ifndef, endif
     标识含义: 示例  转载于https://lequ7.com/guan-yu-uniappuniapp-tiao-jian-bian-yi-ifdefifndefendif.html......
  • uniapp 自定义顶部导航
    <uni-nav-bardark:fixed="true"shadowcolor="#333"background-color="#f6f6f6"status-barleft-icon="left"left-text="返回"title="识别结果"@clickLeft="goback">......
  • uniapp定时器的使用
    //uniapp中的具体用法:我这里使用到了setIntervaldata(){ return{ timer:null//定时器名称 }; },//一般在页面需要的地方使用,这里我是放在了onshow()里onShow(){ //console.log('onshow'); this.timer=setInterval(function(){ //放入你自己的业务逻辑代码 },3000......
  • uniapp移动端输入监听键盘上正在输入的值
    例如搜狗输入法的英文预测模式下,输入的字符不会马上赋给输入框。 input有个ignoreCompositionEvent属性,是否忽略组件内对文本合成系统事件的处理。为 false 时将触发 compositionstart、compositionend、compositionupdate 事件,且在文本合成期间会触发 input 事件。添加该......
  • uniapp中小程序如何跳转其他小程序
    1.首先在manifest中的源码试图中配置 2.跳转代码,在需要跳转的页面写入 ......
  • uniapp swiper点击切换下一张
    通过改变current,自动切换<template> <viewclass="page"> <swiperclass="swiper":vertical="true":current="current"@tap="switchNext"> <swiper-item> <viewclass="swiper-it......
  • uniapp跳转到一个原生的页面
    一、创建原生页面并配置1.创建原生页面Demo2.配置原生页面继承Activity二、在继承UniModule的类中写对应的页面跳转逻辑三、在uniapp中配置跳转的按钮四、将打包好的本地资源替换到项目中,打包运行1.本地资源打包2.替换掉AndroidStudio项目中的文件3.编译运行,点击进入小镜......
  • uniapp自定义开发一个文本输入框
    开发component中的一个input标签一、在原来的模块上面创建一个新的类TestComponent1.新建TestComponent2.配置json文件二、uniapp准备工作1.在uniapp中写一下刚刚创建的输入框2.打包导出资源3.资源替换复制刚刚生成的本地资源文件夹到AndroidStudio项目中......