@capgo/background-geolocation 7.0.12 → 7.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import type { BackgroundGeolocationPlugin } from "./definitions";
1
+ import type { BackgroundGeolocationPlugin } from './definitions';
2
2
  declare const BackgroundGeolocation: BackgroundGeolocationPlugin;
3
- export * from "./definitions";
3
+ export * from './definitions';
4
4
  export { BackgroundGeolocation };
package/dist/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { registerPlugin } from "@capacitor/core";
2
- const BackgroundGeolocation = registerPlugin("BackgroundGeolocation", {
3
- web: () => import("./web").then((m) => new m.BackgroundGeolocationWeb()),
1
+ import { registerPlugin } from '@capacitor/core';
2
+ const BackgroundGeolocation = registerPlugin('BackgroundGeolocation', {
3
+ web: () => import('./web').then((m) => new m.BackgroundGeolocationWeb()),
4
4
  });
5
- export * from "./definitions";
5
+ export * from './definitions';
6
6
  export { BackgroundGeolocation };
7
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,qBAAqB,GAAG,cAAc,CAC1C,uBAAuB,EACvB;IACE,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,wBAAwB,EAAE,CAAC;CACzE,CACF,CAAC;AAEF,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,qBAAqB,EAAE,CAAC","sourcesContent":["import { registerPlugin } from \"@capacitor/core\";\n\nimport type { BackgroundGeolocationPlugin } from \"./definitions\";\n\nconst BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>(\n \"BackgroundGeolocation\",\n {\n web: () => import(\"./web\").then((m) => new m.BackgroundGeolocationWeb()),\n },\n);\n\nexport * from \"./definitions\";\nexport { BackgroundGeolocation };\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,qBAAqB,GAAG,cAAc,CAA8B,uBAAuB,EAAE;IACjG,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,wBAAwB,EAAE,CAAC;CACzE,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,qBAAqB,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { BackgroundGeolocationPlugin } from './definitions';\n\nconst BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>('BackgroundGeolocation', {\n web: () => import('./web').then((m) => new m.BackgroundGeolocationWeb()),\n});\n\nexport * from './definitions';\nexport { BackgroundGeolocation };\n"]}
package/dist/esm/web.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { WebPlugin } from "@capacitor/core";
2
- import type { BackgroundGeolocationPlugin, StartOptions, Location, CallbackError, SetPlannedRouteOptions } from "./definitions";
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import type { BackgroundGeolocationPlugin, StartOptions, Location, CallbackError, SetPlannedRouteOptions } from './definitions';
3
3
  export declare class BackgroundGeolocationWeb extends WebPlugin implements BackgroundGeolocationPlugin {
4
4
  private static readonly EARTH_RADIUS_M;
5
5
  private watchId;
package/dist/esm/web.js CHANGED
@@ -1,4 +1,4 @@
1
- import { WebPlugin } from "@capacitor/core";
1
+ import { WebPlugin } from '@capacitor/core';
2
2
  export class BackgroundGeolocationWeb extends WebPlugin {
3
3
  constructor() {
4
4
  super(...arguments);
@@ -9,17 +9,17 @@ export class BackgroundGeolocationWeb extends WebPlugin {
9
9
  async start(options, callback) {
10
10
  if (!navigator.geolocation) {
11
11
  callback(undefined, {
12
- name: "GeolocationError",
13
- message: "Geolocation is not supported by this browser",
14
- code: "NOT_SUPPORTED",
12
+ name: 'GeolocationError',
13
+ message: 'Geolocation is not supported by this browser',
14
+ code: 'NOT_SUPPORTED',
15
15
  });
16
16
  return;
17
17
  }
18
18
  if (this.watchId) {
19
19
  callback(undefined, {
20
- name: "GeolocationError",
21
- message: "Geolocation already started",
22
- code: "ALREADY_STARTED",
20
+ name: 'GeolocationError',
21
+ message: 'Geolocation already started',
22
+ code: 'ALREADY_STARTED',
23
23
  });
24
24
  return;
25
25
  }
@@ -36,10 +36,7 @@ export class BackgroundGeolocationWeb extends WebPlugin {
36
36
  time: position.timestamp,
37
37
  };
38
38
  if (this.audio && this.plannedRoute.length > 0) {
39
- const currentPoint = [
40
- position.coords.longitude,
41
- position.coords.latitude,
42
- ];
39
+ const currentPoint = [position.coords.longitude, position.coords.latitude];
43
40
  const offRoute = this.distancePointToRoute(currentPoint) > this.distanceThreshold;
44
41
  if (offRoute == true && this.isOffRoute === false) {
45
42
  this.audio.play();
@@ -49,7 +46,7 @@ export class BackgroundGeolocationWeb extends WebPlugin {
49
46
  callback(location);
50
47
  }, (error) => {
51
48
  const callbackError = {
52
- name: "GeolocationError",
49
+ name: 'GeolocationError',
53
50
  message: error.message,
54
51
  code: error.code.toString(),
55
52
  };
@@ -67,16 +64,16 @@ export class BackgroundGeolocationWeb extends WebPlugin {
67
64
  }
68
65
  }
69
66
  async openSettings() {
70
- console.log("openSettings: Web implementation cannot open native settings");
71
- window.alert("Please enable location permissions in your browser settings");
67
+ console.log('openSettings: Web implementation cannot open native settings');
68
+ window.alert('Please enable location permissions in your browser settings');
72
69
  }
73
70
  async setPlannedRoute(options) {
74
71
  if (!options.soundFile) {
75
- throw new Error("Sound file is required");
72
+ throw new Error('Sound file is required');
76
73
  }
77
74
  if (this.audio) {
78
75
  this.audio.pause();
79
- this.audio.src = "";
76
+ this.audio.src = '';
80
77
  this.audio = undefined;
81
78
  }
82
79
  this.audio = new Audio(options.soundFile);
@@ -92,10 +89,7 @@ export class BackgroundGeolocationWeb extends WebPlugin {
92
89
  const dLat = this.toRadians(lat2 - lat1);
93
90
  const dLon = this.toRadians(lon2 - lon1);
94
91
  const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
95
- Math.cos(this.toRadians(lat1)) *
96
- Math.cos(this.toRadians(lat2)) *
97
- Math.sin(dLon / 2) *
98
- Math.sin(dLon / 2);
92
+ Math.cos(this.toRadians(lat1)) * Math.cos(this.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
99
93
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
100
94
  return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;
101
95
  }
@@ -113,14 +107,12 @@ export class BackgroundGeolocationWeb extends WebPlugin {
113
107
  // If cos(C) < 0, the angle is obtuse.
114
108
  // Angle at B (lineStart)
115
109
  // Use a small epsilon to handle floating point inaccuracies in division by zero
116
- const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) /
117
- (2 * dist_A_B * dist_B_C + Number.EPSILON);
110
+ const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) / (2 * dist_A_B * dist_B_C + Number.EPSILON);
118
111
  if (cos_B < 0) {
119
112
  return dist_A_B;
120
113
  }
121
114
  // Angle at C (lineEnd)
122
- const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) /
123
- (2 * dist_A_C * dist_B_C + Number.EPSILON);
115
+ const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) / (2 * dist_A_C * dist_B_C + Number.EPSILON);
124
116
  if (cos_C < 0) {
125
117
  return dist_A_C;
126
118
  }
@@ -1 +1 @@
1
- {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,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GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;YACvC,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;YACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,0BAA0B,CAC9C,KAAK,EACL,SAAS,EACT,OAAO,CACR,CAAC;YACF,IAAI,QAAQ,GAAG,WAAW,EAAE,CAAC;gBAC3B,WAAW,GAAG,QAAQ,CAAC;YACzB,CAAC;QACH,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;;AAzMuB,uCAAc,GAAG,OAAO,AAAV,CAAW","sourcesContent":["import { WebPlugin } from \"@capacitor/core\";\n\nimport type {\n BackgroundGeolocationPlugin,\n StartOptions,\n Location,\n CallbackError,\n SetPlannedRouteOptions,\n} from \"./definitions\";\n\nexport class BackgroundGeolocationWeb\n extends WebPlugin\n implements BackgroundGeolocationPlugin\n{\n private static readonly EARTH_RADIUS_M = 6371000;\n\n private watchId: number | undefined;\n private plannedRoute: [number, number][] = [];\n private audio: HTMLAudioElement | undefined;\n private isOffRoute = true;\n private distanceThreshold = 50;\n\n async start(\n options: StartOptions,\n callback: (position?: Location, error?: CallbackError) => void,\n ): Promise<void> {\n if (!navigator.geolocation) {\n callback(undefined, {\n name: \"GeolocationError\",\n message: \"Geolocation is not supported by this browser\",\n code: \"NOT_SUPPORTED\",\n });\n return;\n }\n\n if (this.watchId) {\n callback(undefined, {\n name: \"GeolocationError\",\n message: \"Geolocation already started\",\n code: \"ALREADY_STARTED\",\n });\n return;\n }\n\n this.watchId = navigator.geolocation.watchPosition(\n (position) => {\n const location: Location = {\n latitude: position.coords.latitude,\n longitude: position.coords.longitude,\n accuracy: position.coords.accuracy,\n altitude: position.coords.altitude,\n altitudeAccuracy: position.coords.altitudeAccuracy,\n simulated: false,\n bearing: position.coords.heading,\n speed: position.coords.speed,\n time: position.timestamp,\n };\n if (this.audio && this.plannedRoute.length > 0) {\n const currentPoint: [number, number] = [\n position.coords.longitude,\n position.coords.latitude,\n ];\n const offRoute =\n this.distancePointToRoute(currentPoint) > this.distanceThreshold;\n if (offRoute == true && this.isOffRoute === false) {\n this.audio.play();\n }\n this.isOffRoute = offRoute;\n }\n callback(location);\n },\n (error) => {\n const callbackError: CallbackError = {\n name: \"GeolocationError\",\n message: error.message,\n code: error.code.toString(),\n };\n callback(undefined, callbackError);\n },\n {\n enableHighAccuracy: true,\n timeout: 10000,\n maximumAge: options.stale ? 300000 : 0,\n },\n );\n }\n\n async stop(): Promise<void> {\n if (this.watchId) {\n navigator.geolocation.clearWatch(this.watchId);\n delete this.watchId;\n }\n }\n\n async openSettings(): Promise<void> {\n console.log(\"openSettings: Web implementation cannot open native settings\");\n window.alert(\"Please enable location permissions in your browser settings\");\n }\n\n async setPlannedRoute(options: SetPlannedRouteOptions): Promise<void> {\n if (!options.soundFile) {\n throw new Error(\"Sound file is required\");\n }\n if (this.audio) {\n this.audio.pause();\n this.audio.src = \"\";\n this.audio = undefined;\n }\n this.audio = new Audio(options.soundFile);\n this.plannedRoute = options.route || [];\n this.distanceThreshold = options.distance || 50;\n }\n\n private toRadians(degrees: number): number {\n return (degrees * Math.PI) / 180;\n }\n\n private haversine(\n point1: [number, number],\n point2: [number, number],\n ): number {\n const [lon1, lat1] = point1;\n const [lon2, lat2] = point2;\n const dLat = this.toRadians(lat2 - lat1);\n const dLon = this.toRadians(lon2 - lon1);\n\n const a =\n Math.sin(dLat / 2) * Math.sin(dLat / 2) +\n Math.cos(this.toRadians(lat1)) *\n Math.cos(this.toRadians(lat2)) *\n Math.sin(dLon / 2) *\n Math.sin(dLon / 2);\n\n const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n\n return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;\n }\n\n private distancePointToLineSegment(\n point: [number, number],\n lineStart: [number, number],\n lineEnd: [number, number],\n ): number {\n // Calculate the distances between the three points using Haversine\n const dist_A_B = this.haversine(point, lineStart);\n const dist_A_C = this.haversine(point, lineEnd);\n const dist_B_C = this.haversine(lineStart, lineEnd);\n\n // Handle the edge case where the line segment is a single point\n if (dist_B_C === 0) {\n return dist_A_B;\n }\n\n // Check if the angles at the line segment's endpoints are obtuse.\n // We use the Law of Cosines (c^2 = a^2 + b^2 - 2ab*cos(C))\n // If cos(C) < 0, the angle is obtuse.\n\n // Angle at B (lineStart)\n // Use a small epsilon to handle floating point inaccuracies in division by zero\n const cos_B =\n (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) /\n (2 * dist_A_B * dist_B_C + Number.EPSILON);\n if (cos_B < 0) {\n return dist_A_B;\n }\n\n // Angle at C (lineEnd)\n const cos_C =\n (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) /\n (2 * dist_A_C * dist_B_C + Number.EPSILON);\n if (cos_C < 0) {\n return dist_A_C;\n }\n\n // If both angles are acute, the closest point is on the line segment itself.\n // We can calculate the distance (height of the triangle) using its area.\n\n // 1. Calculate the semi-perimeter of the triangle ABC\n const s = (dist_A_B + dist_A_C + dist_B_C) / 2;\n\n // 2. Calculate the area using Heron's formula\n const area = Math.sqrt(\n Math.max(0, s * (s - dist_A_B) * (s - dist_A_C) * (s - dist_B_C)),\n );\n\n // 3. The distance is the height of the triangle from point A to the base BC\n // Area = 0.5 * base * height => height = 2 * Area / base\n return (2 * area) / (dist_B_C + Number.EPSILON);\n }\n\n private distancePointToRoute(point: [number, number]): number {\n // If the route has less than 2 points, we can't form a segment.\n if (this.plannedRoute.length < 2) {\n if (this.plannedRoute.length === 1) {\n return this.haversine(point, this.plannedRoute[0]);\n }\n return Infinity; // No line segments to measure against\n }\n\n let minDistance = Infinity;\n\n for (let i = 0; i < this.plannedRoute.length - 1; i++) {\n const lineStart = this.plannedRoute[i];\n const lineEnd = this.plannedRoute[i + 1];\n const distance = this.distancePointToLineSegment(\n point,\n lineStart,\n lineEnd,\n );\n if (distance < minDistance) {\n minDistance = distance;\n }\n }\n\n return minDistance;\n }\n}\n"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CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACtD,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;YACvC,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;YACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,0BAA0B,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;YAC5E,IAAI,QAAQ,GAAG,WAAW,EAAE,CAAC;gBAC3B,WAAW,GAAG,QAAQ,CAAC;YACzB,CAAC;QACH,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;;AAlLuB,uCAAc,GAAG,OAAO,AAAV,CAAW","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type {\n BackgroundGeolocationPlugin,\n StartOptions,\n Location,\n CallbackError,\n SetPlannedRouteOptions,\n} from './definitions';\n\nexport class BackgroundGeolocationWeb extends WebPlugin implements BackgroundGeolocationPlugin {\n private static readonly EARTH_RADIUS_M = 6371000;\n\n private watchId: number | undefined;\n private plannedRoute: [number, number][] = [];\n private audio: HTMLAudioElement | undefined;\n private isOffRoute = true;\n private distanceThreshold = 50;\n\n async start(options: StartOptions, callback: (position?: Location, error?: CallbackError) => void): Promise<void> {\n if (!navigator.geolocation) {\n callback(undefined, {\n name: 'GeolocationError',\n message: 'Geolocation is not supported by this browser',\n code: 'NOT_SUPPORTED',\n });\n return;\n }\n\n if (this.watchId) {\n callback(undefined, {\n name: 'GeolocationError',\n message: 'Geolocation already started',\n code: 'ALREADY_STARTED',\n });\n return;\n }\n\n this.watchId = navigator.geolocation.watchPosition(\n (position) => {\n const location: Location = {\n latitude: position.coords.latitude,\n longitude: position.coords.longitude,\n accuracy: position.coords.accuracy,\n altitude: position.coords.altitude,\n altitudeAccuracy: position.coords.altitudeAccuracy,\n simulated: false,\n bearing: position.coords.heading,\n speed: position.coords.speed,\n time: position.timestamp,\n };\n if (this.audio && this.plannedRoute.length > 0) {\n const currentPoint: [number, number] = [position.coords.longitude, position.coords.latitude];\n const offRoute = this.distancePointToRoute(currentPoint) > this.distanceThreshold;\n if (offRoute == true && this.isOffRoute === false) {\n this.audio.play();\n }\n this.isOffRoute = offRoute;\n }\n callback(location);\n },\n (error) => {\n const callbackError: CallbackError = {\n name: 'GeolocationError',\n message: error.message,\n code: error.code.toString(),\n };\n callback(undefined, callbackError);\n },\n {\n enableHighAccuracy: true,\n timeout: 10000,\n maximumAge: options.stale ? 300000 : 0,\n },\n );\n }\n\n async stop(): Promise<void> {\n if (this.watchId) {\n navigator.geolocation.clearWatch(this.watchId);\n delete this.watchId;\n }\n }\n\n async openSettings(): Promise<void> {\n console.log('openSettings: Web implementation cannot open native settings');\n window.alert('Please enable location permissions in your browser settings');\n }\n\n async setPlannedRoute(options: SetPlannedRouteOptions): Promise<void> {\n if (!options.soundFile) {\n throw new Error('Sound file is required');\n }\n if (this.audio) {\n this.audio.pause();\n this.audio.src = '';\n this.audio = undefined;\n }\n this.audio = new Audio(options.soundFile);\n this.plannedRoute = options.route || [];\n this.distanceThreshold = options.distance || 50;\n }\n\n private toRadians(degrees: number): number {\n return (degrees * Math.PI) / 180;\n }\n\n private haversine(point1: [number, number], point2: [number, number]): number {\n const [lon1, lat1] = point1;\n const [lon2, lat2] = point2;\n const dLat = this.toRadians(lat2 - lat1);\n const dLon = this.toRadians(lon2 - lon1);\n\n const a =\n Math.sin(dLat / 2) * Math.sin(dLat / 2) +\n Math.cos(this.toRadians(lat1)) * Math.cos(this.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);\n\n const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n\n return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;\n }\n\n private distancePointToLineSegment(\n point: [number, number],\n lineStart: [number, number],\n lineEnd: [number, number],\n ): number {\n // Calculate the distances between the three points using Haversine\n const dist_A_B = this.haversine(point, lineStart);\n const dist_A_C = this.haversine(point, lineEnd);\n const dist_B_C = this.haversine(lineStart, lineEnd);\n\n // Handle the edge case where the line segment is a single point\n if (dist_B_C === 0) {\n return dist_A_B;\n }\n\n // Check if the angles at the line segment's endpoints are obtuse.\n // We use the Law of Cosines (c^2 = a^2 + b^2 - 2ab*cos(C))\n // If cos(C) < 0, the angle is obtuse.\n\n // Angle at B (lineStart)\n // Use a small epsilon to handle floating point inaccuracies in division by zero\n const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) / (2 * dist_A_B * dist_B_C + Number.EPSILON);\n if (cos_B < 0) {\n return dist_A_B;\n }\n\n // Angle at C (lineEnd)\n const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) / (2 * dist_A_C * dist_B_C + Number.EPSILON);\n if (cos_C < 0) {\n return dist_A_C;\n }\n\n // If both angles are acute, the closest point is on the line segment itself.\n // We can calculate the distance (height of the triangle) using its area.\n\n // 1. Calculate the semi-perimeter of the triangle ABC\n const s = (dist_A_B + dist_A_C + dist_B_C) / 2;\n\n // 2. Calculate the area using Heron's formula\n const area = Math.sqrt(Math.max(0, s * (s - dist_A_B) * (s - dist_A_C) * (s - dist_B_C)));\n\n // 3. The distance is the height of the triangle from point A to the base BC\n // Area = 0.5 * base * height => height = 2 * Area / base\n return (2 * area) / (dist_B_C + Number.EPSILON);\n }\n\n private distancePointToRoute(point: [number, number]): number {\n // If the route has less than 2 points, we can't form a segment.\n if (this.plannedRoute.length < 2) {\n if (this.plannedRoute.length === 1) {\n return this.haversine(point, this.plannedRoute[0]);\n }\n return Infinity; // No line segments to measure against\n }\n\n let minDistance = Infinity;\n\n for (let i = 0; i < this.plannedRoute.length - 1; i++) {\n const lineStart = this.plannedRoute[i];\n const lineEnd = this.plannedRoute[i + 1];\n const distance = this.distancePointToLineSegment(point, lineStart, lineEnd);\n if (distance < minDistance) {\n minDistance = distance;\n }\n }\n\n return minDistance;\n }\n}\n"]}
@@ -2,7 +2,7 @@
2
2
 
3
3
  var core = require('@capacitor/core');
4
4
 
5
- const BackgroundGeolocation = core.registerPlugin("BackgroundGeolocation", {
5
+ const BackgroundGeolocation = core.registerPlugin('BackgroundGeolocation', {
6
6
  web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.BackgroundGeolocationWeb()),
7
7
  });
8
8
 
@@ -16,17 +16,17 @@ class BackgroundGeolocationWeb extends core.WebPlugin {
16
16
  async start(options, callback) {
17
17
  if (!navigator.geolocation) {
18
18
  callback(undefined, {
19
- name: "GeolocationError",
20
- message: "Geolocation is not supported by this browser",
21
- code: "NOT_SUPPORTED",
19
+ name: 'GeolocationError',
20
+ message: 'Geolocation is not supported by this browser',
21
+ code: 'NOT_SUPPORTED',
22
22
  });
23
23
  return;
24
24
  }
25
25
  if (this.watchId) {
26
26
  callback(undefined, {
27
- name: "GeolocationError",
28
- message: "Geolocation already started",
29
- code: "ALREADY_STARTED",
27
+ name: 'GeolocationError',
28
+ message: 'Geolocation already started',
29
+ code: 'ALREADY_STARTED',
30
30
  });
31
31
  return;
32
32
  }
@@ -43,10 +43,7 @@ class BackgroundGeolocationWeb extends core.WebPlugin {
43
43
  time: position.timestamp,
44
44
  };
45
45
  if (this.audio && this.plannedRoute.length > 0) {
46
- const currentPoint = [
47
- position.coords.longitude,
48
- position.coords.latitude,
49
- ];
46
+ const currentPoint = [position.coords.longitude, position.coords.latitude];
50
47
  const offRoute = this.distancePointToRoute(currentPoint) > this.distanceThreshold;
51
48
  if (offRoute == true && this.isOffRoute === false) {
52
49
  this.audio.play();
@@ -56,7 +53,7 @@ class BackgroundGeolocationWeb extends core.WebPlugin {
56
53
  callback(location);
57
54
  }, (error) => {
58
55
  const callbackError = {
59
- name: "GeolocationError",
56
+ name: 'GeolocationError',
60
57
  message: error.message,
61
58
  code: error.code.toString(),
62
59
  };
@@ -74,16 +71,16 @@ class BackgroundGeolocationWeb extends core.WebPlugin {
74
71
  }
75
72
  }
76
73
  async openSettings() {
77
- console.log("openSettings: Web implementation cannot open native settings");
78
- window.alert("Please enable location permissions in your browser settings");
74
+ console.log('openSettings: Web implementation cannot open native settings');
75
+ window.alert('Please enable location permissions in your browser settings');
79
76
  }
80
77
  async setPlannedRoute(options) {
81
78
  if (!options.soundFile) {
82
- throw new Error("Sound file is required");
79
+ throw new Error('Sound file is required');
83
80
  }
84
81
  if (this.audio) {
85
82
  this.audio.pause();
86
- this.audio.src = "";
83
+ this.audio.src = '';
87
84
  this.audio = undefined;
88
85
  }
89
86
  this.audio = new Audio(options.soundFile);
@@ -99,10 +96,7 @@ class BackgroundGeolocationWeb extends core.WebPlugin {
99
96
  const dLat = this.toRadians(lat2 - lat1);
100
97
  const dLon = this.toRadians(lon2 - lon1);
101
98
  const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
102
- Math.cos(this.toRadians(lat1)) *
103
- Math.cos(this.toRadians(lat2)) *
104
- Math.sin(dLon / 2) *
105
- Math.sin(dLon / 2);
99
+ Math.cos(this.toRadians(lat1)) * Math.cos(this.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
106
100
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
107
101
  return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;
108
102
  }
@@ -120,14 +114,12 @@ class BackgroundGeolocationWeb extends core.WebPlugin {
120
114
  // If cos(C) < 0, the angle is obtuse.
121
115
  // Angle at B (lineStart)
122
116
  // Use a small epsilon to handle floating point inaccuracies in division by zero
123
- const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) /
124
- (2 * dist_A_B * dist_B_C + Number.EPSILON);
117
+ const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) / (2 * dist_A_B * dist_B_C + Number.EPSILON);
125
118
  if (cos_B < 0) {
126
119
  return dist_A_B;
127
120
  }
128
121
  // Angle at C (lineEnd)
129
- const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) /
130
- (2 * dist_A_C * dist_B_C + Number.EPSILON);
122
+ const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) / (2 * dist_A_C * dist_B_C + Number.EPSILON);
131
123
  if (cos_C < 0) {
132
124
  return dist_A_C;
133
125
  }
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from \"@capacitor/core\";\nconst BackgroundGeolocation = registerPlugin(\"BackgroundGeolocation\", {\n web: () => import(\"./web\").then((m) => new m.BackgroundGeolocationWeb()),\n});\nexport * from \"./definitions\";\nexport { BackgroundGeolocation };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from \"@capacitor/core\";\nexport class BackgroundGeolocationWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.plannedRoute = [];\n this.isOffRoute = true;\n this.distanceThreshold = 50;\n }\n async start(options, callback) {\n if (!navigator.geolocation) {\n callback(undefined, {\n name: \"GeolocationError\",\n message: \"Geolocation is not supported by this browser\",\n code: \"NOT_SUPPORTED\",\n });\n return;\n }\n if (this.watchId) {\n callback(undefined, {\n name: \"GeolocationError\",\n message: \"Geolocation already started\",\n code: \"ALREADY_STARTED\",\n });\n return;\n }\n this.watchId = navigator.geolocation.watchPosition((position) => {\n const location = {\n latitude: position.coords.latitude,\n longitude: position.coords.longitude,\n accuracy: position.coords.accuracy,\n altitude: position.coords.altitude,\n altitudeAccuracy: position.coords.altitudeAccuracy,\n simulated: false,\n bearing: position.coords.heading,\n speed: position.coords.speed,\n time: position.timestamp,\n };\n if (this.audio && this.plannedRoute.length > 0) {\n const currentPoint = [\n position.coords.longitude,\n position.coords.latitude,\n ];\n const offRoute = this.distancePointToRoute(currentPoint) > this.distanceThreshold;\n if (offRoute == true && this.isOffRoute === false) {\n this.audio.play();\n }\n this.isOffRoute = offRoute;\n }\n callback(location);\n }, (error) => {\n const callbackError = {\n name: \"GeolocationError\",\n message: error.message,\n code: error.code.toString(),\n };\n callback(undefined, callbackError);\n }, {\n enableHighAccuracy: true,\n timeout: 10000,\n maximumAge: options.stale ? 300000 : 0,\n });\n }\n async stop() {\n if (this.watchId) {\n navigator.geolocation.clearWatch(this.watchId);\n delete this.watchId;\n }\n }\n async openSettings() {\n console.log(\"openSettings: Web implementation cannot open native settings\");\n window.alert(\"Please enable location permissions in your browser settings\");\n }\n async setPlannedRoute(options) {\n if (!options.soundFile) {\n throw new Error(\"Sound file is required\");\n }\n if (this.audio) {\n this.audio.pause();\n this.audio.src = \"\";\n this.audio = undefined;\n }\n this.audio = new Audio(options.soundFile);\n this.plannedRoute = options.route || [];\n this.distanceThreshold = options.distance || 50;\n }\n toRadians(degrees) {\n return (degrees * Math.PI) / 180;\n }\n haversine(point1, point2) {\n const [lon1, lat1] = point1;\n const [lon2, lat2] = point2;\n const dLat = this.toRadians(lat2 - lat1);\n const dLon = this.toRadians(lon2 - lon1);\n const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +\n Math.cos(this.toRadians(lat1)) *\n Math.cos(this.toRadians(lat2)) *\n Math.sin(dLon / 2) *\n Math.sin(dLon / 2);\n const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;\n }\n distancePointToLineSegment(point, lineStart, lineEnd) {\n // Calculate the distances between the three points using Haversine\n const dist_A_B = this.haversine(point, lineStart);\n const dist_A_C = this.haversine(point, lineEnd);\n const dist_B_C = this.haversine(lineStart, lineEnd);\n // Handle the edge case where the line segment is a single point\n if (dist_B_C === 0) {\n return dist_A_B;\n }\n // Check if the angles at the line segment's endpoints are obtuse.\n // We use the Law of Cosines (c^2 = a^2 + b^2 - 2ab*cos(C))\n // If cos(C) < 0, the angle is obtuse.\n // Angle at B (lineStart)\n // Use a small epsilon to handle floating point inaccuracies in division by zero\n const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) /\n (2 * dist_A_B * dist_B_C + Number.EPSILON);\n if (cos_B < 0) {\n return dist_A_B;\n }\n // Angle at C (lineEnd)\n const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) /\n (2 * dist_A_C * dist_B_C + Number.EPSILON);\n if (cos_C < 0) {\n return dist_A_C;\n }\n // If both angles are acute, the closest point is on the line segment itself.\n // We can calculate the distance (height of the triangle) using its area.\n // 1. Calculate the semi-perimeter of the triangle ABC\n const s = (dist_A_B + dist_A_C + dist_B_C) / 2;\n // 2. Calculate the area using Heron's formula\n const area = Math.sqrt(Math.max(0, s * (s - dist_A_B) * (s - dist_A_C) * (s - dist_B_C)));\n // 3. The distance is the height of the triangle from point A to the base BC\n // Area = 0.5 * base * height => height = 2 * Area / base\n return (2 * area) / (dist_B_C + Number.EPSILON);\n }\n distancePointToRoute(point) {\n // If the route has less than 2 points, we can't form a segment.\n if (this.plannedRoute.length < 2) {\n if (this.plannedRoute.length === 1) {\n return this.haversine(point, this.plannedRoute[0]);\n }\n return Infinity; // No line segments to measure against\n }\n let minDistance = Infinity;\n for (let i = 0; i < this.plannedRoute.length - 1; i++) {\n const lineStart = this.plannedRoute[i];\n const lineEnd = this.plannedRoute[i + 1];\n const distance = this.distancePointToLineSegment(point, lineStart, lineEnd);\n if (distance < minDistance) {\n minDistance = distance;\n }\n }\n return minDistance;\n }\n}\nBackgroundGeolocationWeb.EARTH_RADIUS_M = 6371000;\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,qBAAqB,GAAGA,mBAAc,CAAC,uBAAuB,EAAE;AACtE,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,wBAAwB,EAAE,CAAC;AAC5E,CAAC;;ACFM,MAAM,wBAAwB,SAASC,cAAS,CAAC;AACxD,IAAI,WAAW,GAAG;AAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC;AAC3B,QAAQ,IAAI,CAAC,YAAY,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AC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE;AAC/D,YAAY,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;AAClD,YAAY,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC;AACpD,YAAY,MAAM,QAAQ,GAAG,IAAI,CAAC,0BAA0B,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC;AACvF,YAAY,IAAI,QAAQ,GAAG,WAAW,EAAE;AACxC,gBAAgB,WAAW,GAAG,QAAQ;AACtC,YAAY;AACZ,QAAQ;AACR,QAAQ,OAAO,WAAW;AAC1B,IAAI;AACJ;AACA,wBAAwB,CAAC,cAAc,GAAG,OAAO;;;;;;;;;"}
1
+ {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst BackgroundGeolocation = registerPlugin('BackgroundGeolocation', {\n web: () => import('./web').then((m) => new m.BackgroundGeolocationWeb()),\n});\nexport * from './definitions';\nexport { BackgroundGeolocation };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class BackgroundGeolocationWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.plannedRoute = [];\n this.isOffRoute = true;\n this.distanceThreshold = 50;\n }\n async start(options, callback) {\n if (!navigator.geolocation) {\n callback(undefined, {\n name: 'GeolocationError',\n message: 'Geolocation is not supported by this browser',\n code: 'NOT_SUPPORTED',\n });\n return;\n }\n if (this.watchId) {\n callback(undefined, {\n name: 'GeolocationError',\n message: 'Geolocation already started',\n code: 'ALREADY_STARTED',\n });\n return;\n }\n this.watchId = navigator.geolocation.watchPosition((position) => {\n const location = {\n latitude: position.coords.latitude,\n longitude: position.coords.longitude,\n accuracy: position.coords.accuracy,\n altitude: position.coords.altitude,\n altitudeAccuracy: position.coords.altitudeAccuracy,\n simulated: false,\n bearing: position.coords.heading,\n speed: position.coords.speed,\n time: position.timestamp,\n };\n if (this.audio && this.plannedRoute.length > 0) {\n const currentPoint = [position.coords.longitude, position.coords.latitude];\n const offRoute = this.distancePointToRoute(currentPoint) > this.distanceThreshold;\n if (offRoute == true && this.isOffRoute === false) {\n this.audio.play();\n }\n this.isOffRoute = offRoute;\n }\n callback(location);\n }, (error) => {\n const callbackError = {\n name: 'GeolocationError',\n message: error.message,\n code: error.code.toString(),\n };\n callback(undefined, callbackError);\n }, {\n enableHighAccuracy: true,\n timeout: 10000,\n maximumAge: options.stale ? 300000 : 0,\n });\n }\n async stop() {\n if (this.watchId) {\n navigator.geolocation.clearWatch(this.watchId);\n delete this.watchId;\n }\n }\n async openSettings() {\n console.log('openSettings: Web implementation cannot open native settings');\n window.alert('Please enable location permissions in your browser settings');\n }\n async setPlannedRoute(options) {\n if (!options.soundFile) {\n throw new Error('Sound file is required');\n }\n if (this.audio) {\n this.audio.pause();\n this.audio.src = '';\n this.audio = undefined;\n }\n this.audio = new Audio(options.soundFile);\n this.plannedRoute = options.route || [];\n this.distanceThreshold = options.distance || 50;\n }\n toRadians(degrees) {\n return (degrees * Math.PI) / 180;\n }\n haversine(point1, point2) {\n const [lon1, lat1] = point1;\n const [lon2, lat2] = point2;\n const dLat = this.toRadians(lat2 - lat1);\n const dLon = this.toRadians(lon2 - lon1);\n const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +\n Math.cos(this.toRadians(lat1)) * Math.cos(this.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);\n const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;\n }\n distancePointToLineSegment(point, lineStart, lineEnd) {\n // Calculate the distances between the three points using Haversine\n const dist_A_B = this.haversine(point, lineStart);\n const dist_A_C = this.haversine(point, lineEnd);\n const dist_B_C = this.haversine(lineStart, lineEnd);\n // Handle the edge case where the line segment is a single point\n if (dist_B_C === 0) {\n return dist_A_B;\n }\n // Check if the angles at the line segment's endpoints are obtuse.\n // We use the Law of Cosines (c^2 = a^2 + b^2 - 2ab*cos(C))\n // If cos(C) < 0, the angle is obtuse.\n // Angle at B (lineStart)\n // Use a small epsilon to handle floating point inaccuracies in division by zero\n const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) / (2 * dist_A_B * dist_B_C + Number.EPSILON);\n if (cos_B < 0) {\n return dist_A_B;\n }\n // Angle at C (lineEnd)\n const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) / (2 * dist_A_C * dist_B_C + Number.EPSILON);\n if (cos_C < 0) {\n return dist_A_C;\n }\n // If both angles are acute, the closest point is on the line segment itself.\n // We can calculate the distance (height of the triangle) using its area.\n // 1. Calculate the semi-perimeter of the triangle ABC\n const s = (dist_A_B + dist_A_C + dist_B_C) / 2;\n // 2. Calculate the area using Heron's formula\n const area = Math.sqrt(Math.max(0, s * (s - dist_A_B) * (s - dist_A_C) * (s - dist_B_C)));\n // 3. The distance is the height of the triangle from point A to the base BC\n // Area = 0.5 * base * height => height = 2 * Area / base\n return (2 * area) / (dist_B_C + Number.EPSILON);\n }\n distancePointToRoute(point) {\n // If the route has less than 2 points, we can't form a segment.\n if (this.plannedRoute.length < 2) {\n if (this.plannedRoute.length === 1) {\n return this.haversine(point, this.plannedRoute[0]);\n }\n return Infinity; // No line segments to measure against\n }\n let minDistance = Infinity;\n for (let i = 0; i < this.plannedRoute.length - 1; i++) {\n const lineStart = this.plannedRoute[i];\n const lineEnd = this.plannedRoute[i + 1];\n const distance = this.distancePointToLineSegment(point, lineStart, lineEnd);\n if (distance < minDistance) {\n minDistance = distance;\n }\n }\n return minDistance;\n }\n}\nBackgroundGeolocationWeb.EARTH_RADIUS_M = 6371000;\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,qBAAqB,GAAGA,mBAAc,CAAC,uBAAuB,EAAE;AACtE,IAAI,GAAG,EAAE,MAAM,m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Y;AACZ,YAAY,OAAO,QAAQ,CAAC;AAC5B,QAAQ;AACR,QAAQ,IAAI,WAAW,GAAG,QAAQ;AAClC,QAAQ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE;AAC/D,YAAY,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;AAClD,YAAY,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC;AACpD,YAAY,MAAM,QAAQ,GAAG,IAAI,CAAC,0BAA0B,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC;AACvF,YAAY,IAAI,QAAQ,GAAG,WAAW,EAAE;AACxC,gBAAgB,WAAW,GAAG,QAAQ;AACtC,YAAY;AACZ,QAAQ;AACR,QAAQ,OAAO,WAAW;AAC1B,IAAI;AACJ;AACA,wBAAwB,CAAC,cAAc,GAAG,OAAO;;;;;;;;;"}
package/dist/plugin.js CHANGED
@@ -1,7 +1,7 @@
1
1
  var capacitorBackgroundGeolocation = (function (exports, core) {
2
2
  'use strict';
3
3
 
4
- const BackgroundGeolocation = core.registerPlugin("BackgroundGeolocation", {
4
+ const BackgroundGeolocation = core.registerPlugin('BackgroundGeolocation', {
5
5
  web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.BackgroundGeolocationWeb()),
6
6
  });
7
7
 
@@ -15,17 +15,17 @@ var capacitorBackgroundGeolocation = (function (exports, core) {
15
15
  async start(options, callback) {
16
16
  if (!navigator.geolocation) {
17
17
  callback(undefined, {
18
- name: "GeolocationError",
19
- message: "Geolocation is not supported by this browser",
20
- code: "NOT_SUPPORTED",
18
+ name: 'GeolocationError',
19
+ message: 'Geolocation is not supported by this browser',
20
+ code: 'NOT_SUPPORTED',
21
21
  });
22
22
  return;
23
23
  }
24
24
  if (this.watchId) {
25
25
  callback(undefined, {
26
- name: "GeolocationError",
27
- message: "Geolocation already started",
28
- code: "ALREADY_STARTED",
26
+ name: 'GeolocationError',
27
+ message: 'Geolocation already started',
28
+ code: 'ALREADY_STARTED',
29
29
  });
30
30
  return;
31
31
  }
@@ -42,10 +42,7 @@ var capacitorBackgroundGeolocation = (function (exports, core) {
42
42
  time: position.timestamp,
43
43
  };
44
44
  if (this.audio && this.plannedRoute.length > 0) {
45
- const currentPoint = [
46
- position.coords.longitude,
47
- position.coords.latitude,
48
- ];
45
+ const currentPoint = [position.coords.longitude, position.coords.latitude];
49
46
  const offRoute = this.distancePointToRoute(currentPoint) > this.distanceThreshold;
50
47
  if (offRoute == true && this.isOffRoute === false) {
51
48
  this.audio.play();
@@ -55,7 +52,7 @@ var capacitorBackgroundGeolocation = (function (exports, core) {
55
52
  callback(location);
56
53
  }, (error) => {
57
54
  const callbackError = {
58
- name: "GeolocationError",
55
+ name: 'GeolocationError',
59
56
  message: error.message,
60
57
  code: error.code.toString(),
61
58
  };
@@ -73,16 +70,16 @@ var capacitorBackgroundGeolocation = (function (exports, core) {
73
70
  }
74
71
  }
75
72
  async openSettings() {
76
- console.log("openSettings: Web implementation cannot open native settings");
77
- window.alert("Please enable location permissions in your browser settings");
73
+ console.log('openSettings: Web implementation cannot open native settings');
74
+ window.alert('Please enable location permissions in your browser settings');
78
75
  }
79
76
  async setPlannedRoute(options) {
80
77
  if (!options.soundFile) {
81
- throw new Error("Sound file is required");
78
+ throw new Error('Sound file is required');
82
79
  }
83
80
  if (this.audio) {
84
81
  this.audio.pause();
85
- this.audio.src = "";
82
+ this.audio.src = '';
86
83
  this.audio = undefined;
87
84
  }
88
85
  this.audio = new Audio(options.soundFile);
@@ -98,10 +95,7 @@ var capacitorBackgroundGeolocation = (function (exports, core) {
98
95
  const dLat = this.toRadians(lat2 - lat1);
99
96
  const dLon = this.toRadians(lon2 - lon1);
100
97
  const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
101
- Math.cos(this.toRadians(lat1)) *
102
- Math.cos(this.toRadians(lat2)) *
103
- Math.sin(dLon / 2) *
104
- Math.sin(dLon / 2);
98
+ Math.cos(this.toRadians(lat1)) * Math.cos(this.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
105
99
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
106
100
  return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;
107
101
  }
@@ -119,14 +113,12 @@ var capacitorBackgroundGeolocation = (function (exports, core) {
119
113
  // If cos(C) < 0, the angle is obtuse.
120
114
  // Angle at B (lineStart)
121
115
  // Use a small epsilon to handle floating point inaccuracies in division by zero
122
- const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) /
123
- (2 * dist_A_B * dist_B_C + Number.EPSILON);
116
+ const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) / (2 * dist_A_B * dist_B_C + Number.EPSILON);
124
117
  if (cos_B < 0) {
125
118
  return dist_A_B;
126
119
  }
127
120
  // Angle at C (lineEnd)
128
- const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) /
129
- (2 * dist_A_C * dist_B_C + Number.EPSILON);
121
+ const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) / (2 * dist_A_C * dist_B_C + Number.EPSILON);
130
122
  if (cos_C < 0) {
131
123
  return dist_A_C;
132
124
  }
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from \"@capacitor/core\";\nconst BackgroundGeolocation = registerPlugin(\"BackgroundGeolocation\", {\n web: () => import(\"./web\").then((m) => new m.BackgroundGeolocationWeb()),\n});\nexport * from \"./definitions\";\nexport { BackgroundGeolocation };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from \"@capacitor/core\";\nexport class BackgroundGeolocationWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.plannedRoute = [];\n this.isOffRoute = true;\n this.distanceThreshold = 50;\n }\n async start(options, callback) {\n if (!navigator.geolocation) {\n callback(undefined, {\n name: \"GeolocationError\",\n message: \"Geolocation is not supported by this browser\",\n code: \"NOT_SUPPORTED\",\n });\n return;\n }\n if (this.watchId) {\n callback(undefined, {\n name: \"GeolocationError\",\n message: \"Geolocation already started\",\n code: \"ALREADY_STARTED\",\n });\n return;\n }\n this.watchId = navigator.geolocation.watchPosition((position) => {\n const location = {\n latitude: position.coords.latitude,\n longitude: position.coords.longitude,\n accuracy: position.coords.accuracy,\n altitude: position.coords.altitude,\n altitudeAccuracy: position.coords.altitudeAccuracy,\n simulated: false,\n bearing: position.coords.heading,\n speed: position.coords.speed,\n time: position.timestamp,\n };\n if (this.audio && this.plannedRoute.length > 0) {\n const currentPoint = [\n position.coords.longitude,\n position.coords.latitude,\n ];\n const offRoute = this.distancePointToRoute(currentPoint) > this.distanceThreshold;\n if (offRoute == true && this.isOffRoute === false) {\n this.audio.play();\n }\n this.isOffRoute = offRoute;\n }\n callback(location);\n }, (error) => {\n const callbackError = {\n name: \"GeolocationError\",\n message: error.message,\n code: error.code.toString(),\n };\n callback(undefined, callbackError);\n }, {\n enableHighAccuracy: true,\n timeout: 10000,\n maximumAge: options.stale ? 300000 : 0,\n });\n }\n async stop() {\n if (this.watchId) {\n navigator.geolocation.clearWatch(this.watchId);\n delete this.watchId;\n }\n }\n async openSettings() {\n console.log(\"openSettings: Web implementation cannot open native settings\");\n window.alert(\"Please enable location permissions in your browser settings\");\n }\n async setPlannedRoute(options) {\n if (!options.soundFile) {\n throw new Error(\"Sound file is required\");\n }\n if (this.audio) {\n this.audio.pause();\n this.audio.src = \"\";\n this.audio = undefined;\n }\n this.audio = new Audio(options.soundFile);\n this.plannedRoute = options.route || [];\n this.distanceThreshold = options.distance || 50;\n }\n toRadians(degrees) {\n return (degrees * Math.PI) / 180;\n }\n haversine(point1, point2) {\n const [lon1, lat1] = point1;\n const [lon2, lat2] = point2;\n const dLat = this.toRadians(lat2 - lat1);\n const dLon = this.toRadians(lon2 - lon1);\n const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +\n Math.cos(this.toRadians(lat1)) *\n Math.cos(this.toRadians(lat2)) *\n Math.sin(dLon / 2) *\n Math.sin(dLon / 2);\n const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;\n }\n distancePointToLineSegment(point, lineStart, lineEnd) {\n // Calculate the distances between the three points using Haversine\n const dist_A_B = this.haversine(point, lineStart);\n const dist_A_C = this.haversine(point, lineEnd);\n const dist_B_C = this.haversine(lineStart, lineEnd);\n // Handle the edge case where the line segment is a single point\n if (dist_B_C === 0) {\n return dist_A_B;\n }\n // Check if the angles at the line segment's endpoints are obtuse.\n // We use the Law of Cosines (c^2 = a^2 + b^2 - 2ab*cos(C))\n // If cos(C) < 0, the angle is obtuse.\n // Angle at B (lineStart)\n // Use a small epsilon to handle floating point inaccuracies in division by zero\n const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) /\n (2 * dist_A_B * dist_B_C + Number.EPSILON);\n if (cos_B < 0) {\n return dist_A_B;\n }\n // Angle at C (lineEnd)\n const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) /\n (2 * dist_A_C * dist_B_C + Number.EPSILON);\n if (cos_C < 0) {\n return dist_A_C;\n }\n // If both angles are acute, the closest point is on the line segment itself.\n // We can calculate the distance (height of the triangle) using its area.\n // 1. Calculate the semi-perimeter of the triangle ABC\n const s = (dist_A_B + dist_A_C + dist_B_C) / 2;\n // 2. Calculate the area using Heron's formula\n const area = Math.sqrt(Math.max(0, s * (s - dist_A_B) * (s - dist_A_C) * (s - dist_B_C)));\n // 3. The distance is the height of the triangle from point A to the base BC\n // Area = 0.5 * base * height => height = 2 * Area / base\n return (2 * area) / (dist_B_C + Number.EPSILON);\n }\n distancePointToRoute(point) {\n // If the route has less than 2 points, we can't form a segment.\n if (this.plannedRoute.length < 2) {\n if (this.plannedRoute.length === 1) {\n return this.haversine(point, this.plannedRoute[0]);\n }\n return Infinity; // No line segments to measure against\n }\n let minDistance = Infinity;\n for (let i = 0; i < this.plannedRoute.length - 1; i++) {\n const lineStart = this.plannedRoute[i];\n const lineEnd = this.plannedRoute[i + 1];\n const distance = this.distancePointToLineSegment(point, lineStart, lineEnd);\n if (distance < minDistance) {\n minDistance = distance;\n }\n }\n return minDistance;\n }\n}\nBackgroundGeolocationWeb.EARTH_RADIUS_M = 6371000;\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,qBAAqB,GAAGA,mBAAc,CAAC,uBAAuB,EAAE;IACtE,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,wBAAwB,EAAE,CAAC;IAC5E,CAAC;;ICFM,MAAM,wBAAwB,SAASC,cAAS,CAAC;IACxD,IAAI,WAAW,GAAG;IAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC;IAC3B,QAAQ,IAAI,CAAC,YAAY,GAAG,EAAE;IAC9B,QAAQ,IAAI,CAAC,UAAU,GAAG,IAAI;IAC9B,QAAQ,IAAI,CAAC,iBAAiB,GAAG,EAAE;IACnC,IAAI;IACJ,IAAI,MAAM,KAAK,CAAC,OAAO,EAAE,QAAQ,EAAE;IACnC,QAAQ,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE;IACpC,YAAY,QAAQ,CAAC,SAAS,EAAE;IAChC,gBAAgB,IAAI,EAAE,kBAAkB;IACxC,gBAAgB,OAAO,EAAE,8CAA8C;IACvE,gBAAgB,IAAI,EAAE,eAAe;IACrC,aAAa,CAAC;IACd,YAAY;IACZ,QAAQ;IACR,QAAQ,IAAI,IAAI,CAAC,OAAO,EAAE;IAC1B,YAAY,QAAQ,CAAC,SAAS,EAAE;IAChC,gBAAgB,IAAI,EAAE,kBAAkB;IACxC,gBAAgB,OAAO,EAAE,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":["import { registerPlugin } from '@capacitor/core';\nconst BackgroundGeolocation = registerPlugin('BackgroundGeolocation', {\n web: () => import('./web').then((m) => new m.BackgroundGeolocationWeb()),\n});\nexport * from './definitions';\nexport { BackgroundGeolocation };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class BackgroundGeolocationWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.plannedRoute = [];\n this.isOffRoute = true;\n this.distanceThreshold = 50;\n }\n async start(options, callback) {\n if (!navigator.geolocation) {\n callback(undefined, {\n name: 'GeolocationError',\n message: 'Geolocation is not supported by this browser',\n code: 'NOT_SUPPORTED',\n });\n return;\n }\n if (this.watchId) {\n callback(undefined, {\n name: 'GeolocationError',\n message: 'Geolocation already started',\n code: 'ALREADY_STARTED',\n });\n return;\n }\n this.watchId = navigator.geolocation.watchPosition((position) => {\n const location = {\n latitude: position.coords.latitude,\n longitude: position.coords.longitude,\n accuracy: position.coords.accuracy,\n altitude: position.coords.altitude,\n altitudeAccuracy: position.coords.altitudeAccuracy,\n simulated: false,\n bearing: position.coords.heading,\n speed: position.coords.speed,\n time: position.timestamp,\n };\n if (this.audio && this.plannedRoute.length > 0) {\n const currentPoint = [position.coords.longitude, position.coords.latitude];\n const offRoute = this.distancePointToRoute(currentPoint) > this.distanceThreshold;\n if (offRoute == true && this.isOffRoute === false) {\n this.audio.play();\n }\n this.isOffRoute = offRoute;\n }\n callback(location);\n }, (error) => {\n const callbackError = {\n name: 'GeolocationError',\n message: error.message,\n code: error.code.toString(),\n };\n callback(undefined, callbackError);\n }, {\n enableHighAccuracy: true,\n timeout: 10000,\n maximumAge: options.stale ? 300000 : 0,\n });\n }\n async stop() {\n if (this.watchId) {\n navigator.geolocation.clearWatch(this.watchId);\n delete this.watchId;\n }\n }\n async openSettings() {\n console.log('openSettings: Web implementation cannot open native settings');\n window.alert('Please enable location permissions in your browser settings');\n }\n async setPlannedRoute(options) {\n if (!options.soundFile) {\n throw new Error('Sound file is required');\n }\n if (this.audio) {\n this.audio.pause();\n this.audio.src = '';\n this.audio = undefined;\n }\n this.audio = new Audio(options.soundFile);\n this.plannedRoute = options.route || [];\n this.distanceThreshold = options.distance || 50;\n }\n toRadians(degrees) {\n return (degrees * Math.PI) / 180;\n }\n haversine(point1, point2) {\n const [lon1, lat1] = point1;\n const [lon2, lat2] = point2;\n const dLat = this.toRadians(lat2 - lat1);\n const dLon = this.toRadians(lon2 - lon1);\n const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +\n Math.cos(this.toRadians(lat1)) * Math.cos(this.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);\n const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n return BackgroundGeolocationWeb.EARTH_RADIUS_M * c;\n }\n distancePointToLineSegment(point, lineStart, lineEnd) {\n // Calculate the distances between the three points using Haversine\n const dist_A_B = this.haversine(point, lineStart);\n const dist_A_C = this.haversine(point, lineEnd);\n const dist_B_C = this.haversine(lineStart, lineEnd);\n // Handle the edge case where the line segment is a single point\n if (dist_B_C === 0) {\n return dist_A_B;\n }\n // Check if the angles at the line segment's endpoints are obtuse.\n // We use the Law of Cosines (c^2 = a^2 + b^2 - 2ab*cos(C))\n // If cos(C) < 0, the angle is obtuse.\n // Angle at B (lineStart)\n // Use a small epsilon to handle floating point inaccuracies in division by zero\n const cos_B = (dist_A_B ** 2 + dist_B_C ** 2 - dist_A_C ** 2) / (2 * dist_A_B * dist_B_C + Number.EPSILON);\n if (cos_B < 0) {\n return dist_A_B;\n }\n // Angle at C (lineEnd)\n const cos_C = (dist_A_C ** 2 + dist_B_C ** 2 - dist_A_B ** 2) / (2 * dist_A_C * dist_B_C + Number.EPSILON);\n if (cos_C < 0) {\n return dist_A_C;\n }\n // If both angles are acute, the closest point is on the line segment itself.\n // We can calculate the distance (height of the triangle) using its area.\n // 1. Calculate the semi-perimeter of the triangle ABC\n const s = (dist_A_B + dist_A_C + dist_B_C) / 2;\n // 2. Calculate the area using Heron's formula\n const area = Math.sqrt(Math.max(0, s * (s - dist_A_B) * (s - dist_A_C) * (s - dist_B_C)));\n // 3. The distance is the height of the triangle from point A to the base BC\n // Area = 0.5 * base * height => height = 2 * Area / base\n return (2 * area) / (dist_B_C + Number.EPSILON);\n }\n distancePointToRoute(point) {\n // If the route has less than 2 points, we can't form a segment.\n if (this.plannedRoute.length < 2) {\n if (this.plannedRoute.length === 1) {\n return this.haversine(point, this.plannedRoute[0]);\n }\n return Infinity; // No line segments to measure against\n }\n let minDistance = Infinity;\n for (let i = 0; i < this.plannedRoute.length - 1; i++) {\n const lineStart = this.plannedRoute[i];\n const lineEnd = this.plannedRoute[i + 1];\n const distance = this.distancePointToLineSegment(point, lineStart, lineEnd);\n if (distance < minDistance) {\n minDistance = distance;\n }\n }\n return minDistance;\n }\n}\nBackgroundGeolocationWeb.EARTH_RADIUS_M = 6371000;\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,qBAAqB,GAAGA,mBAAc,CAAC,uBAAuB,EAAE;IACtE,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,wBAAwB,EAAE,CAAC;IAC5E,CAAC;;ICFM,MAAM,wBAAwB,SAASC,cAAS,CAAC;IACxD,IAAI,WAAW,GAAG;IAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC;IAC3B,QAAQ,IAAI,CAAC,YAAY,GAAG,EAAE;IAC9B,QAAQ,IAAI,CAAC,UAAU,GAAG,IAAI;IAC9B,QAAQ,IAAI,CAAC,iBAAiB,GAAG,EAAE;IACnC,IAAI;IACJ,IAAI,MAAM,KAAK,CAAC,OAAO,EAAE,QAAQ,EAAE;IACnC,QAAQ,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE;IACpC,YAAY,QAAQ,CAAC,SAAS,EAAE;IAChC,gBAAgB,IAAI,EAAE,kBAAkB;IACxC,gBAAgB,OA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ACZ,QAAQ;IACR,QAAQ,OAAO,WAAW;IAC1B,IAAI;IACJ;IACA,wBAAwB,CAAC,cAAc,GAAG,OAAO;;;;;;;;;;;;;;;"}
@@ -35,7 +35,15 @@ func formatLocation(_ location: CLLocation) -> PluginCallResultData {
35
35
  }
36
36
 
37
37
  @objc(BackgroundGeolocation)
38
- public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate {
38
+ public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate, CAPBridgedPlugin {
39
+ public let identifier = "CapgoBackgroundGeolocationPlugin"
40
+ public let jsName = "CapgoBackgroundGeolocation"
41
+ public let pluginMethods: [CAPPluginMethod] = [
42
+ CAPPluginMethod(name: "start", returnType: CAPPluginReturnPromise),
43
+ CAPPluginMethod(name: "stop", returnType: CAPPluginReturnPromise),
44
+ CAPPluginMethod(name: "openSettings", returnType: CAPPluginReturnPromise),
45
+ CAPPluginMethod(name: "setPlannedRoute", returnType: CAPPluginReturnPromise)
46
+ ]
39
47
  private var locationManager: CLLocationManager?
40
48
  private var created: Date?
41
49
  private var allowStale: Bool = false
@@ -0,0 +1,15 @@
1
+ import XCTest
2
+ @testable import CapgoAlarmPlugin
3
+
4
+ class CapgoAlarmTests: XCTestCase {
5
+ func testEcho() {
6
+ // This is an example of a functional test case for a plugin.
7
+ // Use XCTAssert and related functions to verify your tests produce the correct results.
8
+
9
+ let implementation = CapgoAlarm()
10
+ let value = "Hello, World!"
11
+ let result = implementation.echo(value)
12
+
13
+ XCTAssertEqual(value, result)
14
+ }
15
+ }