HarmonyOS 获取当前定位失败?

真机调用geoLocationManager.getCurrentLocation() 获取当前位置信息时,经常报错:

BussinessError 3301200: Failed to obtain the geographical location.

只有终止进程且关闭再开启日期和时间中自动设置按钮后重新打开app后,才会成功拿到当前位置信息,但是终止进程再重新打开app获取,就又报错了。

阅读 511
1 个回答

1、GNSS信号弱,导致定位超时。

2、网络定位异常,导致定位超时。

3、定位结果不满足定位请求参数中的精度要求(maxAccuracy),导致定位超时。比如maxAccuracy 设置的太小了,导致定位坐标被认定为不符合要求而被忽略,可以尝试改大一些比如100,经测试第一次定位1s左右,后面每次都是几十毫秒。

let startTime = new Date().getTime();
let requestInfo: geoLocationManager.CurrentLocationRequest = {
  'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
  'scenario': geoLocationManager.LocationRequestScenario.UNSET, 'maxAccuracy': 100
};

let locationChange = (err: BusinessError, location: geoLocationManager.Location): void => {
  if (err) {
    console.error('locationChanger: err=' + JSON.stringify(err));
  }
  if (location) {
    console.log('locationChanger: location=' + JSON.stringify(location));
    this.message = '定位信息:' + JSON.stringify(location) + '\n 花费时间:' + (new Date().getTime() - startTime) / 1000
  }
};
try {
  geoLocationManager.getCurrentLocation(requestInfo, locationChange);
} catch (err) {
  console.error("errCode:" + err.code + ",errMessage:" + err.message);
};

4、系统无缓存位置,导致获取上一次位置失败。

5、系统时间设置错误,导致获取位置失败。

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/errorcode-geolocationmanager-V5\#section3301200-%E5%AE%9A%E4%BD%8D%E5%A4%B1%E8%B4%A5%E6%9C%AA%E8%8E%B7%E5%8F%96%E5%88%B0%E5%AE%9A%E4%BD%8D%E7%BB%93%E6%9E%9C

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进