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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:在微信开发者工具和真机调试时,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

### 处理 Uni-app 中的 APP 定位权限 在 Uni-app 开发环境中,处理应用程序的定位权限涉及多个方面。对于 Android 和 iOS 平台而言,确保能够请求并获得用户的地理位置授权至关重要。 #### 权限声明与配置 为了使应用能够在启动时向用户询问是否授予访问设备位置的权利,在 `manifest` 文件内需指定相应的权限设置[^1]。这不同于传统的微信小程序中的 `app.json` 配置方式;Uni-app 使用的是更统一化的 manifest 结构来定义全局属性和服务端口等信息。 针对 Android 应用来说,还需要特别关注 `AndroidManifest.xml` 的编辑工作,因为某些特定版本可能需要额外的手动干预才能正常运作[^3]。 #### 请求权限逻辑编写 当检测到当前环境不支持自动获取当前位置或是之前被拒绝过该权限,则应该主动引导用户前往系统设置界面手动开启此选项。下面给出了一段 JavaScript 代码片段用于展示如何判断是否有权读取地理坐标数据,并采取适当措施: ```javascript // 判断是否具有定位权限 function checkLocationPermission() { const status = plus.permissions.getPermissions(); if (!status['android.permission.ACCESS_FINE_LOCATION']) { // 如果没有精确定位权限 alert('未获取定位权限'); // 提醒用户去打开权限 plus.runtime.openURL('/system/setting/location'); // 打开系统的定位服务开关 return false; } try { uni.getLocation({ type: 'wgs84', success(res) { console.log(`经度:${res.longitude},纬度:${res.latitude}`); }, fail(err) { console.error('获取位置失败', err); // 可能是因为用户关闭了GPS等原因造成的错误 // 此处可以根据实际情况做出相应提示给用户 if (err.errCode === -1 || err.errMsg.indexOf('auth deny') !== -1) { // 用户拒绝授予权限时的操作... plus.nativeUI.confirm( "您已拒绝使用定位功能, 是否现在重新开启?", function(index){ if (index == 0){ plus.runtime.openURL("/system/setting/app"); // 跳转至本应用详情页让用户修改权限 }else{ } },'提示' ); } } }); } catch(e) { console.warn('调用getLocation接口异常:', e.message); } } ``` 上述函数首先会检查是否存在必要的权限,如果没有则尝试指导用户完成操作。一旦成功取得许可之后便可以直接调用 `uni.getLocation()` 获取经纬度数值[^2]。 #### 注意事项 - 对于不同操作系统间存在的差异性要有所准备,比如 iOS 上可能会有更多关于隐私保护方面的考量。 - 当遇到因网络状况不佳而导致的服务不可达情况时也应给予合理的反馈机制告知使用者具体情况。 - 在设计 UI 流程上尽可能简化步骤减少摩擦成本提高用户体验满意度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值