LynxGeolocation
Finds where the phone is, once or as it moves, even with your app in the background, and hears when it enters or leaves an area. Lynx has no navigator.geolocation, so this is its stand-in, shaped like it. import { LynxGeolocation } from '@blotch/angular-lynx';
- iOS: Core Location. iOS asks the person the first time, and lets them turn off Precise Location, which reads as
approximate. - Android:
LocationManager, without Google Play services. Android 12 and later let the person choose approximate or precise in the prompt.
Turn it on
Add permissions.location to the build target's options in angular.json, with the reason iOS shows when it asks:
What it adds:
- iOS:
NSLocationWhenInUseUsageDescription, the reason iOS's prompt shows. - Android:
ACCESS_COARSE_LOCATIONandACCESS_FINE_LOCATION, which Android asks for at runtime. Location hardware is declared not required, so Google Play still offers the app to devices without it.
Without it, the native code behind LynxGeolocation is left out of both apps. App Store Connect would flag an app whose code uses Core Location without saying why. state() is then unsupported, and every call rejects with unavailable.
Two more settings go further. Each needs permissions.location too:
A run tracker needs only backgroundLocation. A watch the app starts on the screen carries on in the background with the usual "while using the app" permission. Google Play reviews every app that declares ACCESS_BACKGROUND_LOCATION, and asks for a video of the app's disclosure, so leave locationAlways out unless the app watches areas.
App Review rejects an app with the location background mode that has no use for it (guideline 2.5.4). Without the settings, the app has none of their code.
See Turn on device features for every setting.
Usage
Shops stands for the app's own service.
To follow the phone as it moves, start a watch. Started in a constructor, it stops when the component is destroyed:
Ask when the person can see why, such as when they tap "Find shops near me", rather than as the app launches. The system soon stops asking:
- iOS asks once.
- Android 11 and later stop asking after a second refusal.
- Earlier Android versions stop once the person ticks "Don't ask again".
A person the system no longer asks has to allow it in Settings, where openAppSettings() takes them. getCurrentPosition() and watchPosition() ask by themselves. Call requestPermission() to ask at a moment of your choosing.
A watch runs while the app is in use, unless it's a background watch. iOS stops a watch while the app is in the background, and Android slows it to a few positions an hour. Stop a watch the app doesn't need there (LynxAppState says when).
Follow the phone in the background
With backgroundLocation on, a watch started with background goes on with the app in the background and the phone locked, as a run or a delivery is followed:
- Start it while the app is on the screen. It needs only the "while using the app" permission then. Android rejects one started from the background with
failed. - The person sees it. iOS shows its location indicator in the status bar while the app is away. Android shows a notification, which brings the app back when tapped. Android 13 and later show the notification only to an app allowed notifications, but the watch goes on either way.
- Stop it when the run ends. It drains the battery for as long as it runs.
Hear when the phone enters an area
With permissions.locationAlways on, watchRegion() calls back with enter as the phone comes into a circle, and exit as it leaves, with the app open or in the background. The phones watch areas themselves, sparing the battery, so there's no watch to keep going:
- It needs the precise location all the time, and asks for it first, as
requestPermission({ always: true })does. Without it, it rejects withdenied. entercomes at once when the phone is in the area as the watch starts. A phone outside it hears nothing until it comes in.- It's a little late. The app hears about an area some seconds or minutes after the phone crosses it, and about areas under about 150 meters across late or not at all.
- Only while the app runs. An app the person or the phone has closed isn't started again for an area, and its areas stop with it.
- iOS watches up to 20 areas for an app at once, and ends those past it with
failed. - The iOS Simulator can't watch areas.
watchRegion()rejects withfailedthere, even with the location allowed all the time. Try areas on a phone, or an Android emulator, which can.
Ask for the location all the time when the person can see why. Google Play requires the app to say what it's for, on a screen of its own, before it asks. Then call requestPermission({ always: true }):
- iOS asks once. If the person hasn't been asked for the location at all, it's one prompt: "Allow While Using App" grants Always for now, and iOS asks the person whether to keep it the first time it would tell the app about an area in the background. Otherwise it's a second prompt, to change to Always. iOS doesn't say when the person keeps "While Using" there, nor that it showed no prompt (as after "Allow Once"), so it resolves as the prompt closes, or after 20 seconds without one.
always()follows a later answer. - Android 10 asks in a prompt. Android 11 and later open the app's location settings, where the person picks "Allow all the time", and resolve as they come back.
API
LynxGeolocation
state
Whether the app may find the phone, and how closely, as a signal. Reading it never asks.
- It's read when the service is created. Until that first answer comes, a moment later, it's
unknown. - It's kept up to date as the person answers the prompt or turns Location on or off.
- It's read again each time the app comes back to the foreground, since the person can change it in Settings meanwhile.
always
Whether the app may find the phone while it isn't in use too, as a signal: iOS's Always, or Android's "Allow all the time" (any location permission before Android 10). watchRegion() needs it.
- It's false while
statedoesn't allow the location, in an app withoutpermissions.locationAlways, and always in the web preview, where a page finds the phone only while it's open. - It's kept up to date as
stateis. - iOS reports it straight away when the person allows the location in a prompt the app asked for Always with. iOS asks them later whether to keep it, and it turns false if they say no.
requestPermission(options?)
Shows the system's prompt if the person hasn't been asked, and resolves with the state after they answer.
Once they've answered, it resolves at once without showing anything. Android is the exception: it asks again after a refusal, until the system stops asking (see denied below).
With { always: true }, it then asks for the location all the time, which always reports once the person has answered (see Hear when the phone enters an area). It rejects with unavailable in an app without permissions.locationAlways.
In the web preview, the browser asks only as the page reads the location, so it reads it once. That can take up to 10 seconds even when the browser already has its answer: show the app as busy until it resolves. always asks for nothing more there.
getCurrentPosition(options?)
Finds where the phone is now, asking first if the person hasn't been asked. It rejects with a GeolocationError:
deniedwhen the app isn't allowedoffwhen Location is offtimeoutwhen no position came in time
It rejects with a RangeError when timeout isn't a positive number or maximumAge is negative.
An app the person allowed only approximate gets an approximate position either way.
watchPosition(options, onPosition, config?)
Calls onPosition with where the phone is, and again each time it moves. Several watches can run at once.
- It resolves with a
GeolocationWatchonce the watch has started, after asking for permission if the person hasn't been asked. - It rejects as
getCurrentPosition()does. - Called in an injection context, such as a constructor, the watch stops when the component or service is destroyed. Pass an
injectorto tie it to another one.
A background watch rejects with unavailable in an app without backgroundLocation.
Watching drains the battery, precise most: stop the watch once the app has what it needs.
watchRegion(region, onChange, config?)
Calls onChange with enter when the phone comes into region, and with exit when it leaves (see Hear when the phone enters an area).
- It resolves with a
GeolocationWatchonce the watch has started, after asking for the location all the time if the person hasn't been asked. - It rejects with
deniedwithout the precise location all the time,offwhen Location is off, andunavailablein an app withoutpermissions.locationAlways. It rejects with aRangeErrorfor a latitude, longitude or radius out of range. - Called in an injection context, the watch stops when the component or service is destroyed, as
watchPosition()'s does.
GeolocationWatch
watchPosition() and watchRegion() both resolve with one.
GeolocationPosition
GeolocationState
unknownat first: for a moment after the service is created, before the first answer, it'sunknownwhatever the person chose.deniedon Android: Android 11 and later ask once more after a first refusal, and earlier versions until the person ticks "Don't ask again". After that, and on iOS, the person has to allow it in Settings.
GeolocationError
An Error with a code:
Testing
Each test starts where a fresh install does. The person hasn't been asked, and allows precise location when the app asks. The phone has no position, so getCurrentPosition() rejects with timeout.
setLocation()puts the phone somewhere, sends each running watch the new position, and each areaenterorexitas the phone crosses it.setLocationPermission()sets what the app is allowed.setLocationAlways()sets whether it's allowed all the time.setLocationAnswer()andsetLocationAlwaysAnswer()set what the prompts answer.setLocationEnabled()turns Location off.locationPrompts()counts the prompts shown.
A background watch runs as any other: the fake is an app with both settings.
Related
- Permission reasons: the
locationreason and the otherpermissions - Turn on device features: every opt-in build option
LynxAppState: stop a watch while the app is in the backgroundLynxLinking.openAppSettings(): where a person who refused allows the location
