uniapp uni.getLocation定位错误信息汇总

文章讨论了在Android和iOS系统中,当定位服务未开启或未授权给微信及小程序时,出现的不同错误提示。在Android系统中,无提示的错误代码表明问题,而在iOS系统中,用户会收到系统级别的定位设置提示。

安卓:

系统没开启定位,无提示
errMsg: "getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF"

系统未授权微信定位,无提示
errMsg: "getLocation:fail system permission denied"

小程序未授权定位,无提示
errMsg: "getLocation:fail auth deny"

ios:

系统没开启定位,会提示设置定位
errMsg: "getLocation:fail system permission denied"

系统未授权微信定位,无提示
errMsg: "getLocation:fail system permission denied"

小程序未授权定位,无提示
errMsg: "getLocation:fail auth deny"

参考链接:https://www.pudn.com/news/6228d6d09ddf223e1ad203ee.html

// services/LocationService.uts import type { FormattedLocation, LocationSuccessResult } from '@/utils/location'; export class LocationService { private static instance: LocationService | null = null; private isRunning: boolean = false; private intervalMs: number = 3000; // 回调函数数组(支持多个监听者) private listeners: ((loc: FormattedLocation) => void)[] = []; public static getInstance(): LocationService { if (!this.instance) { this.instance = new LocationService(); } return this.instance; } start(): void { if (this.isRunning) { console.log("定位服务已在运行"); return; } this.isRunning = true; console.log("【LocationService】启动原生级定位循环"); // 使用原生循环(Android Handler / iOS NSTimer) this.nativeLoop(); } stop(): void { this.isRunning = false; this.nativeClear(); console.log("【LocationService】已停止"); } addListener(callback: (loc: FormattedLocation) => void): void { this.listeners.push(callback); } removeListener(callback: (loc: FormattedLocation) => void): void { const index = this.listeners.indexOf(callback); if (index !== -1) { this.listeners.splice(index, 1); } } private nativeLoop(): void { // 模拟原生循环(实际应使用平台特定实现) this.doLocation(); if (this.isRunning) { setTimeout(() => { this.nativeLoop(); // 递归调用,但需注意黑屏仍可能暂停 }, this.intervalMs); } } private nativeClear(): void { // 清理原生定时器(此处省略具体实现) } private doLocation(): void { uni.getLocation({ type: 'wgs84', success: (res: LocationSuccessResult) => { const formatted: FormattedLocation = { lat: res.latitude, lng: res.longitude, spd: res.speed ?? 0, acc: res.accuracy ?? 999, alt: res.altitude ?? 0 }; console.log(`📍 定位成功: ${formatted.lat.toFixed(6)}, ${formatted.lng.toFixed(6)}`); // 通知所有监听者 for (const cb of this.listeners) { try { cb(formatted); } catch (e) { console.error("回调执行出错", e); } } }, fail: (err: any) => { } }); } } 上方代码报错15:01:30.748 [plugin:uni:app-uts] 编译失败 15:01:30.748 ‌⁠error: 参数类型不匹配:实际类型为 'Function1<LocationSuccessResult, Unit>',预期类型为 'Function1<@ParameterName(...) GetLocationSuccess, Unit>?'。错误详情链接: https://doc.dcloud.net.cn/uni-app-x/uts/compiler-known-issues.html#error17‌ 15:01:30.748 at utils/LocationService.uts:66:15 15:01:30.748 64 | uni.getLocation({ 15:01:30.748 65 | type: 'wgs84', 15:01:30.748 66 | success: (res: LocationSuccessResult) => { 15:01:30.748 | ^ 15:01:30.748 67 | const formatted: FormattedLocation = { 15:01:30.748 68 | lat: res.latitude,⁠ 15:01:30.748 找不到名称“not”。参考: https://doc.dcloud.net.cn/uni-app-x/uts/compiler-known-issues.html#error18 15:01:30.748 at utils/LocationService.uts:13:9 15:01:30.748 11 | 15:01:30.748 12 | public static getInstance(): LocationService { 15:01:30.748 13 | if (!this.instance) { 15:01:30.748 | ^ 15:01:30.748 14 | this.instance = new LocationService(); 15:01:30.749 15 | }⁠ 15:01:30.795 代码编译报错
11-25
14:49:43.350 [plugin:uni:app-uts] 编译失败 14:49:43.351 ‌⁠error: 参数类型不匹配:实际类型为 'Function1<LocationSuccessResult, Unit>',预期类型为 'Function1<@ParameterName(...) GetLocationSuccess, Unit>?'。错误详情链接: https://doc.dcloud.net.cn/uni-app-x/uts/compiler-known-issues.html#error17‌ 14:49:43.351 at utils/LocationService.uts:66:15 14:49:43.351 64 | uni.getLocation({ 14:49:43.351 65 | type: 'wgs84', 14:49:43.351 66 | success: (res: LocationSuccessResult) => { 14:49:43.351 | ^ 14:49:43.351 67 | const formatted: FormattedLocation = { 14:49:43.351 68 | lat: res.latitude,⁠ 14:49:43.351 找不到名称“errMsg”。参考: https://doc.dcloud.net.cn/uni-app-x/uts/compiler-known-issues.html#error18 14:49:43.351 at utils/LocationService.uts:87:87 14:49:43.351 85 | }, 14:49:43.351 86 | fail: (err: any) => { 14:49:43.351 87 | __f__('error','at utils/LocationService.uts:87',"【LocationService】定位失败:", err?.errMsg || err); 14:49:43.351 | ^ 14:49:43.351 88 | 14:49:43.351 89 | // 特殊处理超时和权限拒绝⁠ 14:49:43.351 ‌‌⁠error: Condition type mismatch: inferred type is 'Any' but 'Boolean' was expected.‌ [上报Bug] 14:49:43.351 at utils/LocationService.uts:87:97 14:49:43.351 85 | }, 14:49:43.352 86 | fail: (err: any) => { 14:49:43.352 87 | __f__('error','at utils/LocationService.uts:87',"【LocationService】定位失败:", err?.errMsg || err); 14:49:43.352 | ^ 14:49:43.352 88 | 14:49:43.352 89 | // 特殊处理超时和权限拒绝⁠ 14:49:43.352 ‌‌⁠error: Condition type mismatch: inferred type is 'Boolean?' but 'Boolean' was expected.‌ [上报Bug] 14:49:43.352 at utils/LocationService.uts:90:12 14:49:43.352 88 | 14:49:43.352 89 | // 特殊处理超时和权限拒绝 14:49:43.352 90 | if (err?.errMsg?.includes("auth deny")) { 14:49:43.352 | ^ 14:49:43.352 91 | this.stop(); 14:49:43.352 92 | uni.showToast({ title: "定位权限被拒绝", icon: "none" });⁠ 14:49:43.352 找不到名称“errMsg”。参考: https://doc.dcloud.net.cn/uni-app-x/uts/compiler-known-issues.html#error18 14:49:43.352 at utils/LocationService.uts:90:17 14:49:43.352 88 | 14:49:43.352 89 | // 特殊处理超时和权限拒绝 14:49:43.352 90 | if (err?.errMsg?.includes("auth deny")) { 14:49:43.352 | ^ 14:49:43.352 91 | this.stop(); 14:49:43.352 92 | uni.showToast({ title: "定位权限被拒绝", icon: "none" });⁠
11-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值