HSLocationWatcher
TypeAn independent location tracking object.
Create via hs.location.addWatcher(). Call start() to begin receiving
updates, and set a callback to handle them.
The callback receives (event, data):
| Event | Data |
|---|---|
"location" |
a locationTable |
"error" |
an error message string |
"authorizationChanged" |
the new status string ("authorized", "denied", "restricted", "notDetermined") |
Example:
const w = hs.location.addWatcher()
w.setCallback((event, data) => {
if (event === 'location') console.log(data.latitude, data.longitude)
})
w.start()
Properties
distanceFilter
number
The minimum distance in metres the device must move before a new update
is delivered. Defaults to `kCLDistanceFilterNone` (all movements reported).
Methods
start() -> HSLocationWatcher
Starts location updates. The callback must be set first.
Declaration
start() -> HSLocationWatcher
Returns
HSLocationWatcher
self, for chaining
Example
const w = hs.location.addWatcher()
w.setCallback((ev, d) => console.log(ev, d)).start()
stop() -> HSLocationWatcher
Stops location updates.
Declaration
stop() -> HSLocationWatcher
Returns
HSLocationWatcher
self, for chaining
Example
w.stop()
setCallback(fn) -> HSLocationWatcher
Sets the callback function invoked when location events occur.
Declaration
setCallback(fn) -> HSLocationWatcher
Parameters
| Name | Type | Description |
|---|---|---|
| fn | JSValue | `function(event, data)` — see type documentation for event names |
Returns
HSLocationWatcher
self, for chaining
Example
w.setCallback((event, data) => {
if (event === 'location') console.log(data.latitude, data.longitude)
})
location() -> [AnyHashable: Any]
Returns the most recently received location, or null if none yet.
Declaration
location() -> [AnyHashable: Any]
Returns
[AnyHashable: Any]
a locationTable, or null
Example
const loc = w.location()
if (loc) console.log(`${loc.latitude}, ${loc.longitude}`)