UNPKG

@dderevjanik/termux-api

Version:

This library allows you to interact with your Android device from Node.js using termux-api

92 lines (88 loc) 1.86 kB
import { exec } from "child_process"; export interface Location { /** * Latitude of the location * @default 37.19334072 */ latitude: number; /** * Longitude of the location * @default 21.176107897 */ longitude: number; /** * Altitude of the location * @default 639.22107621775 */ altitude: number; /** * Accuracy of the location * @default 53.24557489035055 */ accuracy: number; /** * Vertical accuracy of the location * @default 44.45618533322194 */ vertical_accuracy: number; /** * Bearing of the location * @default 0.0 */ bearing: number; /** * Speed of the location * @default 0.0 */ speed: number; /** * Elapsed time in milliseconds * @default 38 */ elapsedMs: number; /** * Provider of the location * @default "gps" */ provider: "gps" | "network" | "passive"; } export interface LocationOptions { /** * Location provider * @default "gps" */ provider?: "gps" | "network" | "passive"; /** * Kind of request to make * @default "once" */ request?: "once" | "last" | "updates"; } /** * Get the device location * * **Requires permission** `ACCESS_FINE_LOCATION` */ export async function location( options: LocationOptions = {} ): Promise<Location> { const opts: LocationOptions = { provider: "gps", request: "once", ...options, }; return new Promise<Location>((resolve, reject) => { const command = `termux-location -p ${opts.provider} -r ${opts.request}`; exec(command, (error, stdout, stderr) => { if (error) { return reject(`Error: ${error.message}`); } if (stderr) { return reject(`Error: ${stderr}`); } const output = stdout.trim(); const location: Location = JSON.parse(output); return resolve(location); }); }); }