web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit 85661545f97b8a89c37ac23708f59dcfd84276c4
parent a76c174a82647e62577387baf9b17be44bd9f793
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri, 23 Aug 2024 13:23:18 +0000

client: add capacitor geolocation api

Diffstat:
Mclient/package.json | 1+
Aclient/src/capacitor/geolocation.ts | 31+++++++++++++++++++++++++++++++
Mclient/src/capacitor/index.ts | 8+++++++-
Mclient/src/types.ts | 13+++++++++++++
Mclient/src/utils.ts | 9++++++---
5 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/client/package.json b/client/package.json @@ -12,6 +12,7 @@ "@capacitor/core": "^6.1.2", "@capacitor/device": "^6.0.0", "@capacitor/dialog": "^6.0.0", + "@capacitor/geolocation": "^6.0.0", "@capacitor/haptics": "^6.0.0", "@capacitor/network": "^6.0.0", "@capacitor/preferences": "^6.0.0", diff --git a/client/src/capacitor/geolocation.ts b/client/src/capacitor/geolocation.ts @@ -0,0 +1,31 @@ +import { Geolocation, type Position } from '@capacitor/geolocation'; +import type { IClientGeolocation, IClientGeolocationPosition } from '../types'; +import { fmt_location_coords } from '../utils'; + +export class CapacitorClientGeolocation implements IClientGeolocation { + private parse_geolocation_position(position: Position): IClientGeolocationPosition { + const pos: IClientGeolocationPosition = { + lat: fmt_location_coords(position.coords.latitude), + lng: fmt_location_coords(position.coords.longitude), + accuracy: position.coords.accuracy || undefined, + altitude: position.coords.altitude || undefined, + altitude_accuracy: position.coords.altitudeAccuracy || undefined + }; + return pos; + } + + private async get_current_position(): Promise<IClientGeolocationPosition | undefined> { + try { + const position = await Geolocation.getCurrentPosition(); + return this.parse_geolocation_position(position); + } catch (e) { }; + } + + public async current(): Promise<IClientGeolocationPosition | undefined> { + try { + const position = await this.get_current_position(); + if (position) return position; + } catch (e) { }; + } +} + diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts @@ -1,11 +1,12 @@ import { Capacitor } from "@capacitor/core"; -import type { IClient, IClientBrowser, IClientDatePicker, IClientDevice, IClientDialog, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare, IClientWifi } from "../types"; +import type { IClient, IClientBrowser, IClientDatePicker, IClientDevice, IClientDialog, IClientGeolocation, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare, IClientWifi } from "../types"; import { parse_platform } from "../utils"; import { CapacitorClientBrowser } from "./browser"; import { CapacitorClientDatePicker } from "./date-picker"; import { CapacitorClientDevice } from "./device"; import { CapacitorClientDialog } from "./dialog"; +import { CapacitorClientGeolocation } from "./geolocation"; import { CapacitorClientHaptics } from "./haptics"; import { CapacitorClientKeystore } from "./keystore"; import { CapacitorClientNetwork } from "./network"; @@ -25,6 +26,7 @@ export class ClientCapacitor implements IClient { private _dialog: IClientDialog = new CapacitorClientDialog(); private _browser: IClientBrowser = new CapacitorClientBrowser(); private _dates: IClientDatePicker = new CapacitorClientDatePicker(); + private _geo: IClientGeolocation = new CapacitorClientGeolocation(); public get platform() { return this._platform; @@ -69,4 +71,8 @@ export class ClientCapacitor implements IClient { public get dates() { return this._dates; } + + public get geo() { + return this._geo; + } }; \ No newline at end of file diff --git a/client/src/types.ts b/client/src/types.ts @@ -13,6 +13,7 @@ export type IClient = { dialog: IClientDialog; browser: IClientBrowser; dates: IClientDatePicker; + geo: IClientGeolocation; }; export type IClientPlatform = `androiď` | `ios` | `web`; @@ -111,3 +112,15 @@ export type IClientDatePickerPresent = { export type IClientDatePicker = { present(opts: IClientDatePickerPresent): Promise<string | undefined>; }; + +export type IClientGeolocationPosition = { + lat: number; + lng: number; + accuracy: number | undefined; + altitude: number | undefined; + altitude_accuracy: number | undefined; +}; + +export type IClientGeolocation = { + current(): Promise<IClientGeolocationPosition | undefined>; +}; diff --git a/client/src/utils.ts b/client/src/utils.ts @@ -8,5 +8,9 @@ export function parse_platform(str: string): IClientPlatform { return str; default: return `web`; - } -} -\ No newline at end of file + }; +}; + +export function fmt_location_coords(number: number): number { + return Math.round(number * 1e7) / 1e7; +};