Repository navigation
Expand file tree
/
Copy pathlocation.ts
More file actions
53 lines (49 loc) · 1.64 KB
/
Copy pathlocation.ts
File metadata and controls
53 lines (49 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Utils } from "@nativescript/core";
declare const com: any;
export type CurrentLocation = {
latitude: number;
longitude: number;
accuracyMeters: number | null;
timestampMs: number;
};
/** Resolve one current foreground location using Android's LocationManager. */
export function getCurrentLocation(): Promise<CurrentLocation> {
if (!global.isAndroid) {
return Promise.reject(new Error("Weather location is only available on Android."));
}
const context = Utils.android.getApplicationContext();
if (!context) {
return Promise.reject(new Error("Android application context unavailable."));
}
return new Promise<CurrentLocation>((resolve, reject) => {
const provider = new com.faceclaw.app.FaceclawLocationProvider(context);
let settled = false;
const listener = new com.faceclaw.app.FaceclawLocationListener({
onLocation: (
latitude: number,
longitude: number,
accuracyMeters: number,
timestampMs: number,
) => {
if (settled) return;
settled = true;
provider.setListener(null);
resolve({
latitude: Number(latitude),
longitude: Number(longitude),
accuracyMeters: Number(accuracyMeters) >= 0 ? Number(accuracyMeters) : null,
timestampMs: Number(timestampMs),
});
},
onError: (message: string) => {
if (settled) return;
settled = true;
provider.setListener(null);
reject(new Error(String(message)));
},
});
// Retain the proxy through the Java provider until one terminal callback.
provider.setListener(listener);
provider.start();
});
}