Skip to content

CLLocationManager:locationerror 事件 ​

locationerror 事件在定位出错时派发,主要在连续更新期间。

语法 ​

manager.addEventListener('locationerror', (event) => { })

事件对象 ​

CLLocationErrorEvent,多出一个字段:

error

CLLocationError。name 是错误的分类,code 和 domain 是系统给的原值。

事件不能取消。target 是这个 manager。

什么时候派发 ​

  • 系统报告定位出错时。连续更新期间没有授权,name 是 'NotAllowedError';暂时定不出位置,name 是 'LocationUnknownError',系统会接着试。
  • 回调在系统报告之后的下一轮执行。

事件名是 locationerror,不是 error。监听 error 不报错,但收不到任何东西。

这个事件的实际派发未在模拟器验证。

示例 ​

js
const manager = new CLLocationManager();
manager.addEventListener('locationerror', (e) => {
  if (e.error.name === 'NotAllowedError') {
    manager.stopUpdatingLocation();
    console.log('没有权限,停止定位');
  } else {
    console.log('定位出错', e.error.name, e.error.code);
  }
});
manager.startUpdatingLocation();

效果(未在模拟器验证):没有授权时输出 没有权限,停止定位;别的错误输出错误的名字和错误码。

对应的 iOS API ​

委托方法 locationManager:didFailWithError:。NSError 是事件的 e.error。

参见 ​