uni-app getLocation()无法获取定位,一直fail()

在使用uni-app开发过程中,遇到uni.getLocation()在微信开发者工具和真机上始终返回fail()的问题。原因是微信基础库2.117.0版本后对getLocation接口增加了频率限制。解决方案是改用onLocationChange()接口来获取定位。
摘要由CSDN通过智能技术生成

问题描述:在微信开发者工具和真机调试时,uni.getLocation()获取定位失败,一直fail()。

问题原因:
微信官方文档,从基础库 2.117.0版本起,将对getLocation()接口增加频率限制。改为使用onLocationChange()。
在这里插入图片描述
代码:

getDingwei(){//获取当前设备定位
	let that = this;
	uni.getSetting({
		success(res) {
			//地理位置
			//#ifdef MP-WEIXIN 
			if (res.authSetting['scope.userLocation']) {
				uni.authorize({
					scope: 'scope.userLocation',
					success(res) {
						uni.showLoading({
							title: '正在加载中...',
							mask: true
						});
						// 从基础库 2.117.0版本起,将对getLocation()接口增加频率限制。故改为onLocationChange()。
						wx.startLocationUpdate({  
							success: res => {
								console.log('开启小程序接收位置消息成功')
								wx.onLocationChange(function (res){
									// console.log(res.latitude,res.longitude) 
									// 3、经纬度转换成详细地址
									that.confirmClockloadCity(res.longitude,res.latitude)
							 	})
						 	},  
							fail: err => console.error('开启小程序接收位置消息失败:', err),  
							complete: msg => console.log('调用开启小程序接收位置消息API完成')  
						})  
						// 以前的方法
						// uni.getLocation(function (res) {
						// 	type: 'gcj02',
						// 	success:function (res) {
						// 		// 3、经纬度转换成详细地址
						// 		console.log("----66----");
						// 		that.confirmClockloadCity(res.longitude,res.latitude)
						// 	},
						// 	fail(err) {
						// 		console.log("----fail777----",err);
						// 		uni.showModal({
						// 			title: '提示',
						// 			content: '定位失败,你未开启定位权限,点击开启定位权限',
						// 			success: function (res) {
						// 			  if (res.confirm) {
						// 				uni.openSetting({
						// 				  success: function (res) {
						// 					if (res.authSetting['scope.userLocation']) {
						// 						uni.showLoading({
						// 							title: '正在加载中...',
						// 							mask: true
						// 						});
						// 						uni.getLocation({
						// 							type: 'gcj02',
						// 							success:function (res) {
						// 					           that.confirmClockloadCity(res.longitude,res.latitude)
						// 							}
						// 						})
						// 					} else {
						// 						console.log('用户未同意地理位置权限')
						// 					}
						// 				  }
						// 				})
						// 			  }
						// 			}
						// 		})
						// 	},
						// complete() {
						// 	console.log("----complete888----");
						// }
						// })
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: '定位失败,你未开启定位权限,点击开启定位权限',
							success: function (res) {
							  if (res.confirm) {
								uni.openSetting({
								  success: function (res) {
									if (res.authSetting['scope.userLocation']) {
										console.log('5555*')
										uni.showLoading({
											title: '正在加载中...',
											mask: true
										});
										uni.getLocation({
											type: 'gcj02',
											success:function (res) {
													that.confirmClockloadCity(res.longitude,res.latitude)
											}
										 })
									 } else {
											console.log('用户未同意地理位置权限')
									 }
								  }
								})
							  }
							}
						})
					}
				})
			} else {
				console.log('用户未同意地理位置权限')
				uni.showModal({
					title: '提示',
					content: '定位失败,你未开启定位权限,点击开启定位权限',
					success: function (res) {
						if (res.confirm) {
							uni.openSetting({
								success: function (res) {
									if (res.authSetting['scope.userLocation']) {
										// console.log('5555*')
										uni.showLoading({
											title: '正在加载中...',
											mask: true
										});
										uni.getLocation({
											type: 'gcj02',
											success:function (res) {
													that.confirmClockloadCity(res.longitude,res.latitude)
											}
										})
									} else {
										// console.log('00000*')
										console.log('用户未同意地理位置权限')
									}
								}
							})
						}
					}
				})
			}
			//#endif
		}
	})
},
confirmClockloadCity: function (longitude, latitude) {//调高德地图API,用经纬度反推详细的街道地址
	let that = this	
    uni.request({
		url: 'https://restapi.amap.com/v3/geocode/regeo',
		data: {
			key: 'c8d499635271ab4f9d449d35911e2cf1',
			location: longitude + "," + latitude,
			extensions: "all",
		},
		success: function (res) {
    		var adressStr = res.data.regeocode.formatted_address
			that.endServiceParamObj.adress = adressStr;
			that.endServicePost();// 调结束服务的接口
		},
		fail: function (res) {
			console.log('获取地理位置失败')
		}
    })			  
},

参考的其他答案:
https://ask.dcloud.net.cn/question/90273

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值