Skip to content

CLLocationManager:locationupdate 事件 ​

locationupdate 事件在 CLLocationManager 有了新的位置时派发。

语法 ​

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

事件对象 ​

CLLocationUpdateEvent,多出一个字段:

locations

CLLocation 的数组,至少一个,按时间从早到晚,最后一个是最新的。系统有时把几个位置一起报。这个字段只读,给它赋值抛 TypeError。

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

什么时候派发 ​

示例 ​

js
const manager = new CLLocationManager();
manager.addEventListener('locationupdate', (e) => {
  const latest = e.locations[e.locations.length - 1];
  console.log(latest.coordinate.latitude, latest.coordinate.longitude,
              latest.timestamp.toISOString());
});
manager.startUpdatingLocation();

效果:每次有新位置输出一行:纬度、经度、测量的时间。

对应的 iOS API ​

委托方法 locationManager:didUpdateLocations:。locations 参数是事件的 e.locations。

参见 ​