1.0.0 • Published 2 years ago

@awarns/geolocation v1.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

@awarns/geolocation

npm (scoped) npm

This module includes tasks to regularly obtain the location of the phone.

This plugin acts as a wrapper on top of the nativescript-context-apis plugin (from the same authors), offering GNSS location acquisition tasks. Acquire phone's location, on demand, even in background.

Install the plugin using the following command line instruction:

ns plugin add @awarns/geolocation

Usage

After installing and setting up this plugin, you'll have access to two different tasks to acquire the location of the phone. The result, will be a Geolocation record, described below.

Tasks

Task nameDescription
acquirePhoneGeolocationAllows to acquire the most accurate location available, obtained from the phone, among a few, on demand. The amount of locations to be collected and decide on is configurable
acquireMultiplePhoneGeolocationAllows to repeatedly acquire phone locations. Similarly to the single acquisition task, each reported value can be chosen among a few acquired values. Scans will happen for as long as there is execution time remaining (3 minutes max. or shortly before the next time-scheduled task execution, whatever occurs earlier)

Note: All the tasks require fine location permission and active location services for their execution. Each task will automatically request what is missing during framework's initialization

Acquire a single GNSS location

To register this task for its use, you just need to import it and call its generator function inside your application's task list:

import { Task } from '@awarns/core/tasks';
import { acquirePhoneGeolocationTask } from '@awarns/geolocation';

export const demoTasks: Array<Task> = [
      acquirePhoneGeolocationTask(/* optional */ { bestOf: 3, timeout: 10000 }),
];

Task generator parameters:

NameTypeDescription
bestOfnumberThe number of locations to be collected, to pick the one that is the most accurate. The more locations being requested, the more the task will take to run. The default value is 3
timeoutnumberLimit the maximum time to be spent collecting locations, in milliseconds. The default value is 10000 (10s).

Task output events:

Example usage in the application task graph:

on(
  'startEvent',
  run('acquirePhoneGeolocation')
    .every(1, 'minutes')
    .cancelOn('stopEvent')
);

on('geolocationAcquired', run('writeRecords'));

Note: To use the writeRecords, task the persistence package must be installed and configured. See persistence package docs.

Acquire GNSS locations in batch

To register this task for its use, you just need to import it and call its generator function inside your application's task list:

import { Task } from '@awarns/core/tasks';
import { acquireMultiplePhoneGeolocationTask } from '@awarns/geolocation';

export const demoTasks: Array<Task> = [
  acquireMultiplePhoneGeolocationTask(/* optional */ { bestOf: 1, timeout: 15000 }),
];

Task generator parameters:

NameTypeDescription
bestOfnumberThe number of locations to be collected for each final record being reported, to pick the one that is the most accurate from a subset. The more locations being requested, the lower total amount of locations being reported. This means, in the hypothetical case where there's time to collect 6 locations, with a bestOf value of 1, the 6 locations will be reported, whereas with a bestOf value of 3, in the same situation, only 2 locations will be reported, being these 2 the most accurate among the 2 subsets of 3 locations. The default value is 1 (each location being collected ends being reported)
timeoutnumberLimit the maximum time to be spent collecting each location, in milliseconds. The default value is 15000 (15s).

Task output events:

Example usage in the application task graph:

on(
  'startEvent',
  run('acquireMultiplePhoneGeolocation', { maxInterval: 10000 /* (Optional) Maximun interval between location acquisitions (this includes the time it takes to obtain all the locations in a reporting subset, if bestOf > 1), unlimited by default */ })
    .every(1, 'minutes')
    .cancelOn('stopEvent')
);

on('geolocationAcquired', run('writeRecords'));

Note: To use the writeRecords task, the persistence package must be installed and configured. See persistence package docs.

Events

NamePayloadDescription
geolocationAcquiredGeolocation | Array\Indicates that one or more new GNSS locations have been acquired

Records

Geolocation

PropertyTypeDescription
idstringRecord's unique id
typestringAlways geolocation
changeChangeAlways none. Locations never start or end, they represent spatio-temporal snapshots of where the phone was at a given time. To detect when the phone started and ended being in a place, use the geofencing package
timestampDateThe local time when the location was acquired
latitudenumberThe latitude of where the phone is located at
longitudenumberThe longitude of where the phone is located at
altitudenumberThe altitude of where the phone is located at
verticalAccuracynumberThe estimated error in the latitude
horizontalAccuracynumberThe estimated error in the longitude
speednumberThe estimated speed of the phone by the time the location was acquired
directionnumberThe estimated direction of the phone by the time the location was acquired

License

Apache License Version 2.0