Location and motion
Read location and motion, count steps, watch regions and record motion in the background.
cx.Locate gets one fix, cx.WatchLocation streams fixes and cx.WatchMotion streams accelerometer, gyroscope, magnetometer and fused device-motion samples. Each returns a *core.SensorStream whose Stop cancels it, as the scope that started it ending does; callbacks run on the engine goroutine. The permissions are core.PermissionLocation, PermissionBackgroundLocation and PermissionMotion (permissions): starting a stream never asks, and one without its permission fails with core.SensorNotAllowed.
cx.RequestPermission(core.PermissionLocation, func(s core.PermissionStatus) { if !s.Allowed() { return } cx.Locate(core.LocationOptions{}, func(f core.LocationFix, err error) { if err == nil { showPosition(f.Latitude, f.Longitude, f.Approximate) } })})Locate waits for a new fix, 30 seconds by default (Timeout); only MaxAge lets it take a cached one, which says so with Cached and Age. Approximate tells a fix the user allowed only approximately; Android refreshes such a fix at most every ten minutes, so give Locate a MaxAge for it there. Coordinates are WGS 84; a measurement the provider lacks has Valid false, and Altitude is ellipsoidal where MSLAltitude is above mean sea level. Motion uses the device’s natural axes, acceleration in m/s² (+9.81 on z lying face up), rotation in rad/s and the magnetic field in µT, every 20 ms by default, held to 10 ms–1 s. Under load a stream keeps the newest sample per sensor and counts the ones it replaced in Dropped.
Streams go on while the app is inactive and pause in the background, marking the first sample after it with Gap; a pending Locate keeps the rest of its timeout. LocationOptions.Background keeps a location stream going in the background under PermissionBackgroundLocation: Apple apps add the location background mode, and on Android a foreground service shows a notification with NotificationTitle and NotificationBody and keeps the app running after its activity ends. Nothing starts a process the system ended.
apple: info: NSLocationWhenInUseUsageDescription: Shows where you are on the map. NSLocationUsageDescription: Shows where you are on the map. # macOS NSMotionUsageDescription: Counts your reps. UIBackgroundModes: [location] # background streamsandroid: permissions: [ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION, FOREGROUND_SERVICE, FOREGROUND_SERVICE_LOCATION]iOS and watchOS give location and motion, macOS location, tvOS one fix at a time and Android both through the framework’s own providers. Linux gets fixes and streams from GeoClue 2 on the system bus, whose agent asks the user as the app’s first request starts; since GeoClue cannot be asked without prompting, the permission reads as not asked until the app has asked in that run, and background location is unavailable. Windows gets them from WinRT’s Geolocator; the permission reads as denied while location is off for the app, as granted once the app has asked in that run and been allowed, and otherwise as not asked, and background location is unavailable there too. A secure browser supplies geolocation fixes and, on mobile, accelerometer, gyroscope and fused motion readings. Call cx.RequestPermission(core.PermissionMotion, done) from a press handler on Safari. The browser reports location unavailable without navigator.geolocation, motion unavailable on desktops and for magnetometer streams.
Fitness, regions and bounded motion
Section titled “Fitness, regions and bounded motion”cx.WatchSteps, cx.WatchActivity and cx.WatchPressure return scoped streams. Ask for core.PermissionActivityRecognition before steps/activity, and before pressure on Apple. Apple supplies all three through Core Motion; Android supplies step counters and pressure, and reports activity classification unavailable. Counts belong to the sample’s Since window; resumed windows carry Gap. Pressure is in pascals, with optional relative altitude in meters. Missing distances, floors and altitude have Valid false.
cx.WatchGeofences takes core.GeofenceOptions with one to twenty distinct GeofenceRegion circles (WGS 84 coordinates and radius in meters). It reports GeofenceInside, GeofenceOutside or GeofenceUnknown per region. Background requires background location access/declarations and Apple’s Always authorization; Android also needs a notification title. Stop or disposal removes its registrations; no subscription survives process death.
cx.RequestPreciseLocation("route", done) explicitly asks for precise authorization and answers a boolean, never a position. In apple.info, declare NSLocationTemporaryUsageDescriptionDictionary: {route: Explains why this feature needs precision.}. Android needs both coarse and fine permissions. A false answer may leave approximate location authorized.
cx.WatchBackgroundMotion(core.BackgroundMotionOptions{Motion: core.MotionOptions{}, Duration: 20 * time.Second, NotificationTitle: "Recording motion"}, sample, failed) requests a finite live stream. It needs AccessMotion and AccessBackgroundMotion. iOS may expire its execution lease sooner. Android apps set android.background_motion: true and declare FOREGROUND_SERVICE; the nonsticky service stops within three minutes even if the requested duration is longer. Duration or OS expiry reports SensorTimedOut. Stop/disposal ends the provider and lease; nothing records after termination. Other targets report unavailable.
Headless tests feed FeedSteps, FeedActivity, FeedPressure and FeedGeofence, and set PreciseLocationAnswer for accuracy requests. Physical fitness, region wake-up and suspended-background behavior still need device acceptance.