CLLocationManager: locationupdate event
The locationupdate event fires when the CLLocationManager has a new location.
Syntax
manager.addEventListener('locationupdate', (event) => { })Event object
CLLocationUpdateEvent, with one extra field:
locations
An array of CLLocation, with at least one item, ordered from earliest to latest; the last one is the newest. The system sometimes reports several locations together. This field is read-only; assigning to it throws TypeError.
Not cancelable. target is the manager.
When it fires
- During continuous updates (after
startUpdatingLocation()and beforestopUpdatingLocation()), each time there's a new location. - When
requestLocation()gets a location. - The callback runs on the turn after the system reports the location.
Examples
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();Effect: prints a line for each new location: latitude, longitude, and the time it was measured.
Corresponding iOS API
The delegate method locationManager:didUpdateLocations:. The locations parameter is the event's e.locations.
See also
locationerror: location failures- Getting the user's location: picking usable locations by accuracy and time
- Installing
@nacrejs/core-location