@capgo/background-geolocation 8.0.31 → 8.0.33
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.
- package/README.md +297 -13
- package/android/build.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +17 -0
- package/android/src/main/java/com/capgo/capacitor_background_geolocation/BackgroundGeolocation.java +281 -2
- package/android/src/main/java/com/capgo/capacitor_background_geolocation/GeofenceBootReceiver.java +58 -0
- package/android/src/main/java/com/capgo/capacitor_background_geolocation/GeofenceBroadcastReceiver.java +83 -0
- package/android/src/main/java/com/capgo/capacitor_background_geolocation/GeofenceStore.java +255 -0
- package/android/src/main/java/com/capgo/capacitor_background_geolocation/GeofenceTransitionWorker.java +35 -0
- package/dist/docs.json +740 -4
- package/dist/esm/definitions.d.ts +317 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +17 -1
- package/dist/esm/web.js +108 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +108 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +108 -0
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapgoBackgroundGeolocationPlugin/CapgoCapacitorBackgroundGeolocationPlugin.swift +425 -4
- package/ios/Tests/CapgoBackgroundGeolocationPluginTests/CapgoBackgroundGeolocationPluginTests.swift +5 -0
- package/package.json +17 -10
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PluginListenerHandle } from '@capacitor/core';
|
|
1
2
|
/**
|
|
2
3
|
* The options for configuring for location updates.
|
|
3
4
|
*
|
|
@@ -190,6 +191,231 @@ export interface SetPlannedRouteOptions {
|
|
|
190
191
|
*/
|
|
191
192
|
distance: number;
|
|
192
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Options for configuring native geofence transition handling.
|
|
196
|
+
*
|
|
197
|
+
* When `url` is provided, native iOS and Android code sends a JSON `POST`
|
|
198
|
+
* whenever a monitored region is entered or exited. This keeps geofence
|
|
199
|
+
* handling useful when the WebView is suspended.
|
|
200
|
+
*
|
|
201
|
+
* @since 8.0.30
|
|
202
|
+
*/
|
|
203
|
+
export interface GeofenceSetupOptions {
|
|
204
|
+
/**
|
|
205
|
+
* Endpoint that receives geofence transition payloads.
|
|
206
|
+
*
|
|
207
|
+
* @since 8.0.30
|
|
208
|
+
* @example "https://api.example.com/geofences"
|
|
209
|
+
*/
|
|
210
|
+
url?: string;
|
|
211
|
+
/**
|
|
212
|
+
* Whether entry transitions should be monitored.
|
|
213
|
+
*
|
|
214
|
+
* @since 8.0.30
|
|
215
|
+
* @default true
|
|
216
|
+
* @example true
|
|
217
|
+
*/
|
|
218
|
+
notifyOnEntry?: boolean;
|
|
219
|
+
/**
|
|
220
|
+
* Whether exit transitions should be monitored.
|
|
221
|
+
*
|
|
222
|
+
* @since 8.0.30
|
|
223
|
+
* @default true
|
|
224
|
+
* @example true
|
|
225
|
+
*/
|
|
226
|
+
notifyOnExit?: boolean;
|
|
227
|
+
/**
|
|
228
|
+
* Base JSON payload merged into every native transition POST and listener event.
|
|
229
|
+
*
|
|
230
|
+
* @since 8.0.30
|
|
231
|
+
* @example { "userId": "123" }
|
|
232
|
+
*/
|
|
233
|
+
payload?: Record<string, unknown>;
|
|
234
|
+
/**
|
|
235
|
+
* Whether the plugin should request the native location permission needed for geofencing.
|
|
236
|
+
*
|
|
237
|
+
* iOS geofencing needs Always location authorization. Android background geofencing
|
|
238
|
+
* needs foreground location and, on Android 10+, background location permission.
|
|
239
|
+
*
|
|
240
|
+
* @since 8.0.30
|
|
241
|
+
* @default true
|
|
242
|
+
* @example true
|
|
243
|
+
*/
|
|
244
|
+
requestPermissions?: boolean;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* A circular geofence region.
|
|
248
|
+
*
|
|
249
|
+
* @since 8.0.30
|
|
250
|
+
*/
|
|
251
|
+
export interface AddGeofenceOptions {
|
|
252
|
+
/**
|
|
253
|
+
* Latitude in degrees for the region center.
|
|
254
|
+
*
|
|
255
|
+
* @since 8.0.30
|
|
256
|
+
* @example 40.7128
|
|
257
|
+
*/
|
|
258
|
+
latitude: number;
|
|
259
|
+
/**
|
|
260
|
+
* Longitude in degrees for the region center.
|
|
261
|
+
*
|
|
262
|
+
* @since 8.0.30
|
|
263
|
+
* @example -74.006
|
|
264
|
+
*/
|
|
265
|
+
longitude: number;
|
|
266
|
+
/**
|
|
267
|
+
* Region radius in meters.
|
|
268
|
+
*
|
|
269
|
+
* @since 8.0.30
|
|
270
|
+
* @default 50
|
|
271
|
+
* @example 150
|
|
272
|
+
*/
|
|
273
|
+
radius?: number;
|
|
274
|
+
/**
|
|
275
|
+
* Stable identifier for the geofence.
|
|
276
|
+
*
|
|
277
|
+
* @since 8.0.30
|
|
278
|
+
* @example "office"
|
|
279
|
+
*/
|
|
280
|
+
identifier: string;
|
|
281
|
+
/**
|
|
282
|
+
* Overrides the setup-level entry setting for this region.
|
|
283
|
+
*
|
|
284
|
+
* @since 8.0.30
|
|
285
|
+
*/
|
|
286
|
+
notifyOnEntry?: boolean;
|
|
287
|
+
/**
|
|
288
|
+
* Overrides the setup-level exit setting for this region.
|
|
289
|
+
*
|
|
290
|
+
* @since 8.0.30
|
|
291
|
+
*/
|
|
292
|
+
notifyOnExit?: boolean;
|
|
293
|
+
/**
|
|
294
|
+
* Region-specific payload merged over the setup payload.
|
|
295
|
+
*
|
|
296
|
+
* @since 8.0.30
|
|
297
|
+
* @example { "storeId": "nyc-1" }
|
|
298
|
+
*/
|
|
299
|
+
payload?: Record<string, unknown>;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Options for removing a monitored geofence.
|
|
303
|
+
*
|
|
304
|
+
* @since 8.0.30
|
|
305
|
+
*/
|
|
306
|
+
export interface RemoveGeofenceOptions {
|
|
307
|
+
/**
|
|
308
|
+
* Identifier passed to `addGeofence`.
|
|
309
|
+
*
|
|
310
|
+
* @since 8.0.30
|
|
311
|
+
* @example "office"
|
|
312
|
+
*/
|
|
313
|
+
identifier: string;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Result returned when listing monitored geofences.
|
|
317
|
+
*
|
|
318
|
+
* @since 8.0.30
|
|
319
|
+
*/
|
|
320
|
+
export interface MonitoredGeofencesResult {
|
|
321
|
+
/**
|
|
322
|
+
* Identifiers for all geofences currently monitored by this plugin.
|
|
323
|
+
*
|
|
324
|
+
* @since 8.0.30
|
|
325
|
+
* @example ["office", "warehouse"]
|
|
326
|
+
*/
|
|
327
|
+
regions: string[];
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Event emitted when a monitored geofence is entered or exited.
|
|
331
|
+
*
|
|
332
|
+
* The same data is also sent to the configured `url`, when one is set.
|
|
333
|
+
*
|
|
334
|
+
* @since 8.0.30
|
|
335
|
+
*/
|
|
336
|
+
export interface GeofenceTransitionEvent {
|
|
337
|
+
/**
|
|
338
|
+
* Identifier of the geofence that changed state.
|
|
339
|
+
*
|
|
340
|
+
* @since 8.0.30
|
|
341
|
+
* @example "office"
|
|
342
|
+
*/
|
|
343
|
+
identifier: string;
|
|
344
|
+
/**
|
|
345
|
+
* Transition name.
|
|
346
|
+
*
|
|
347
|
+
* @since 8.0.30
|
|
348
|
+
* @example "enter"
|
|
349
|
+
*/
|
|
350
|
+
transition: 'enter' | 'exit';
|
|
351
|
+
/**
|
|
352
|
+
* `true` for entry transitions, `false` for exit transitions.
|
|
353
|
+
*
|
|
354
|
+
* @since 8.0.30
|
|
355
|
+
* @example true
|
|
356
|
+
*/
|
|
357
|
+
enter: boolean;
|
|
358
|
+
/**
|
|
359
|
+
* Latitude in degrees for the monitored region center, when available.
|
|
360
|
+
*
|
|
361
|
+
* @since 8.0.30
|
|
362
|
+
* @example 40.7128
|
|
363
|
+
*/
|
|
364
|
+
latitude?: number;
|
|
365
|
+
/**
|
|
366
|
+
* Longitude in degrees for the monitored region center, when available.
|
|
367
|
+
*
|
|
368
|
+
* @since 8.0.30
|
|
369
|
+
* @example -74.006
|
|
370
|
+
*/
|
|
371
|
+
longitude?: number;
|
|
372
|
+
/**
|
|
373
|
+
* Region radius in meters, when available.
|
|
374
|
+
*
|
|
375
|
+
* @since 8.0.30
|
|
376
|
+
* @example 150
|
|
377
|
+
*/
|
|
378
|
+
radius?: number;
|
|
379
|
+
/**
|
|
380
|
+
* Merged setup and region payload.
|
|
381
|
+
*
|
|
382
|
+
* @since 8.0.30
|
|
383
|
+
*/
|
|
384
|
+
payload?: Record<string, unknown>;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Event emitted when native geofence monitoring fails.
|
|
388
|
+
*
|
|
389
|
+
* @since 8.0.30
|
|
390
|
+
*/
|
|
391
|
+
export interface GeofenceErrorEvent {
|
|
392
|
+
/**
|
|
393
|
+
* Identifier of the geofence that failed, when native APIs provide it.
|
|
394
|
+
*
|
|
395
|
+
* @since 8.0.30
|
|
396
|
+
* @example "office"
|
|
397
|
+
*/
|
|
398
|
+
identifier?: string;
|
|
399
|
+
/**
|
|
400
|
+
* Native platform error code.
|
|
401
|
+
*
|
|
402
|
+
* @since 8.0.30
|
|
403
|
+
* @example 5
|
|
404
|
+
*/
|
|
405
|
+
code?: number;
|
|
406
|
+
/**
|
|
407
|
+
* Native platform error message.
|
|
408
|
+
*
|
|
409
|
+
* @since 8.0.30
|
|
410
|
+
*/
|
|
411
|
+
message: string;
|
|
412
|
+
/**
|
|
413
|
+
* Native error domain, when available.
|
|
414
|
+
*
|
|
415
|
+
* @since 8.0.30
|
|
416
|
+
*/
|
|
417
|
+
domain?: string;
|
|
418
|
+
}
|
|
193
419
|
/**
|
|
194
420
|
* Main plugin interface for background geolocation functionality.
|
|
195
421
|
* Provides methods to manage location updates and access device settings.
|
|
@@ -265,6 +491,97 @@ export interface BackgroundGeolocationPlugin {
|
|
|
265
491
|
* });
|
|
266
492
|
*/
|
|
267
493
|
setPlannedRoute(options: SetPlannedRouteOptions): Promise<void>;
|
|
494
|
+
/**
|
|
495
|
+
* Configures native geofence transition handling.
|
|
496
|
+
*
|
|
497
|
+
* Call this before adding geofences when you need native background POSTs
|
|
498
|
+
* or default entry/exit settings.
|
|
499
|
+
*
|
|
500
|
+
* @param options The geofence configuration options
|
|
501
|
+
* @returns A promise that resolves once geofencing is configured
|
|
502
|
+
*
|
|
503
|
+
* @since 8.0.30
|
|
504
|
+
* @example
|
|
505
|
+
* await BackgroundGeolocation.setupGeofencing({
|
|
506
|
+
* url: "https://api.example.com/geofences",
|
|
507
|
+
* notifyOnEntry: true,
|
|
508
|
+
* notifyOnExit: true,
|
|
509
|
+
* payload: { userId: "123" }
|
|
510
|
+
* });
|
|
511
|
+
*/
|
|
512
|
+
setupGeofencing(options: GeofenceSetupOptions): Promise<void>;
|
|
513
|
+
/**
|
|
514
|
+
* Starts monitoring a circular native geofence.
|
|
515
|
+
*
|
|
516
|
+
* @param options The geofence region options
|
|
517
|
+
* @returns A promise that resolves when native monitoring starts
|
|
518
|
+
*
|
|
519
|
+
* @since 8.0.30
|
|
520
|
+
* @example
|
|
521
|
+
* await BackgroundGeolocation.addGeofence({
|
|
522
|
+
* identifier: "office",
|
|
523
|
+
* latitude: 40.7128,
|
|
524
|
+
* longitude: -74.006,
|
|
525
|
+
* radius: 150
|
|
526
|
+
* });
|
|
527
|
+
*/
|
|
528
|
+
addGeofence(options: AddGeofenceOptions): Promise<void>;
|
|
529
|
+
/**
|
|
530
|
+
* Stops monitoring one geofence.
|
|
531
|
+
*
|
|
532
|
+
* @param options The geofence identifier
|
|
533
|
+
* @returns A promise that resolves when native monitoring stops
|
|
534
|
+
*
|
|
535
|
+
* @since 8.0.30
|
|
536
|
+
* @example
|
|
537
|
+
* await BackgroundGeolocation.removeGeofence({ identifier: "office" });
|
|
538
|
+
*/
|
|
539
|
+
removeGeofence(options: RemoveGeofenceOptions): Promise<void>;
|
|
540
|
+
/**
|
|
541
|
+
* Stops monitoring every geofence registered by this plugin.
|
|
542
|
+
*
|
|
543
|
+
* @returns A promise that resolves when all native geofences are removed
|
|
544
|
+
*
|
|
545
|
+
* @since 8.0.30
|
|
546
|
+
* @example
|
|
547
|
+
* await BackgroundGeolocation.removeAllGeofences();
|
|
548
|
+
*/
|
|
549
|
+
removeAllGeofences(): Promise<void>;
|
|
550
|
+
/**
|
|
551
|
+
* Lists the geofence identifiers currently monitored by this plugin.
|
|
552
|
+
*
|
|
553
|
+
* @returns A promise with monitored geofence identifiers
|
|
554
|
+
*
|
|
555
|
+
* @since 8.0.30
|
|
556
|
+
* @example
|
|
557
|
+
* const { regions } = await BackgroundGeolocation.getMonitoredGeofences();
|
|
558
|
+
*/
|
|
559
|
+
getMonitoredGeofences(): Promise<MonitoredGeofencesResult>;
|
|
560
|
+
/**
|
|
561
|
+
* Listens for geofence enter/exit transitions while the WebView is alive.
|
|
562
|
+
*
|
|
563
|
+
* Native `url` delivery configured through `setupGeofencing` is used for
|
|
564
|
+
* background-safe delivery.
|
|
565
|
+
*
|
|
566
|
+
* @since 8.0.30
|
|
567
|
+
* @example
|
|
568
|
+
* const handle = await BackgroundGeolocation.addListener(
|
|
569
|
+
* "geofenceTransition",
|
|
570
|
+
* (event) => console.log(event.identifier, event.transition)
|
|
571
|
+
* );
|
|
572
|
+
*/
|
|
573
|
+
addListener(eventName: 'geofenceTransition', listenerFunc: (event: GeofenceTransitionEvent) => void): Promise<PluginListenerHandle>;
|
|
574
|
+
/**
|
|
575
|
+
* Listens for native geofence monitoring errors while the WebView is alive.
|
|
576
|
+
*
|
|
577
|
+
* @since 8.0.30
|
|
578
|
+
* @example
|
|
579
|
+
* const handle = await BackgroundGeolocation.addListener(
|
|
580
|
+
* "geofenceError",
|
|
581
|
+
* (event) => console.error(event.identifier, event.message)
|
|
582
|
+
* );
|
|
583
|
+
*/
|
|
584
|
+
addListener(eventName: 'geofenceError', listenerFunc: (event: GeofenceErrorEvent) => void): Promise<PluginListenerHandle>;
|
|
268
585
|
/**
|
|
269
586
|
* Get the native Capacitor plugin version
|
|
270
587
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * The options for configuring for location updates.\n *\n * @since 7.0.9\n */\nexport interface StartOptions {\n /**\n * If the \"backgroundMessage\" option is defined, the plugin will\n * provide location updates whether the app is in the background or the\n * foreground. If it is not defined, location updates are only\n * guaranteed in the foreground. This is true on both platforms.\n *\n * On Android, a notification must be shown to continue receiving\n * location updates in the background. This option specifies the text of\n * that notification.\n *\n * @since 7.0.9\n * @example \"Getting your location to provide better service\"\n */\n backgroundMessage?: string;\n /**\n * The title of the notification mentioned above.\n *\n * @since 7.0.9\n * @default \"Using your location\"\n * @example \"Location Service\"\n */\n backgroundTitle?: string;\n /**\n * Whether permissions should be requested from the user automatically,\n * if they are not already granted.\n *\n * @since 7.0.9\n * @default true\n * @example\n * // Auto-request permissions\n * requestPermissions: true\n *\n * // Don't auto-request, handle manually\n * requestPermissions: false\n */\n requestPermissions?: boolean;\n /**\n * If \"true\", stale locations may be delivered while the device\n * obtains a GPS fix. You are responsible for checking the \"time\"\n * property. If \"false\", locations are guaranteed to be up to date.\n *\n * @since 7.0.9\n * @default false\n * @example\n * // Allow stale locations for faster initial response\n * stale: true\n *\n * // Only fresh locations\n * stale: false\n */\n stale?: boolean;\n /**\n * The distance in meters that the device must move before a new location update is triggered.\n * This is used to filter out small movements and reduce the number of updates.\n *\n * @since 7.0.9\n * @default 0\n * @example\n * // Update every 10 meters\n * distanceFilter: 10\n *\n * // Update on any movement\n * distanceFilter: 0\n */\n distanceFilter?: number;\n}\n\n/**\n * Represents a geographical location with various attributes.\n * Contains all the standard location properties returned by GPS/network providers.\n *\n * @since 7.0.0\n */\nexport interface Location {\n /**\n * Latitude in degrees.\n * Range: -90.0 to +90.0\n *\n * @since 7.0.0\n * @example 40.7128\n */\n latitude: number;\n /**\n * Longitude in degrees.\n * Range: -180.0 to +180.0\n *\n * @since 7.0.0\n * @example -74.0060\n */\n longitude: number;\n /**\n * Radius of horizontal uncertainty in metres, with 68% confidence.\n * Lower values indicate more accurate location.\n *\n * @since 7.0.0\n * @example 5.0\n */\n accuracy: number;\n /**\n * Metres above sea level (or null if not available).\n *\n * @since 7.0.0\n * @example 10.5\n */\n altitude: number | null;\n /**\n * Vertical uncertainty in metres, with 68% confidence (or null if not available).\n *\n * @since 7.0.0\n * @example 3.0\n */\n altitudeAccuracy: number | null;\n /**\n * `true` if the location was simulated by software, rather than GPS.\n * Useful for detecting mock locations in development or testing.\n *\n * @since 7.0.0\n * @example false\n */\n simulated: boolean;\n /**\n * Deviation from true north in degrees (or null if not available).\n * Range: 0.0 to 360.0\n *\n * @since 7.0.0\n * @example 45.5\n */\n bearing: number | null;\n /**\n * Speed in metres per second (or null if not available).\n *\n * @since 7.0.0\n * @example 2.5\n */\n speed: number | null;\n /**\n * Time the location was produced, in milliseconds since the unix epoch.\n * Use this to check if a location is stale when using stale: true.\n *\n * @since 7.0.0\n * @example 1640995200000\n */\n time: number | null;\n}\n\n/**\n * Error object that may be passed to the location start callback.\n * Extends the standard Error with optional error codes.\n *\n * @since 7.0.0\n */\nexport interface CallbackError extends Error {\n /**\n * Optional error code for more specific error handling.\n *\n * @since 7.0.0\n * @example \"PERMISSION_DENIED\"\n */\n code?: string;\n}\n\nexport interface SetPlannedRouteOptions {\n /**\n * The name of the sound file to play.\n * Must be a valid sound relative path in the app's public folder to work for both web and native platforms.\n * There's no need to include the public folder in the path.\n * @since 7.0.10\n * @example \"notification.mp3\"\n * */\n soundFile: string;\n /**\n * The planned route as an array of longitude and latitude pairs.\n * Each pair represents a point on the route.\n * This is used to define a route that the user can follow.\n * The route is used to play a sound when the user deviates from it.\n * @since 7.0.11\n * @example [[-74.0060, 40.7128], [-118.2437, 34.0522]]\n */\n route: [number, number][];\n\n /**\n * The distance in meters that the user must deviate from the planned route to trigger the sound.\n * This is used to determine how far off the route the user can be before the sound is played.\n * If not specified, a default value of 50 meters is used.\n * @since 7.0.11\n * @default 50\n * @example 50\n */\n distance: number;\n}\n\n/**\n * Main plugin interface for background geolocation functionality.\n * Provides methods to manage location updates and access device settings.\n *\n * @since 7.0.0\n */\nexport interface BackgroundGeolocationPlugin {\n /**\n * To start listening for changes in the device's location, call this method.\n * A Promise is returned to indicate that it finished the call. The callback will be called every time a new location\n * is available, or if there was an error when calling this method. Don't rely on promise rejection for this.\n *\n * @param options The configuration options\n * @param callback The callback function invoked when a new location is available or an error occurs\n * @returns A promise that resolves when the method is successfully called\n *\n * @since 7.0.9\n * @example\n * await BackgroundGeolocation.start(\n * {\n * backgroundMessage: \"App is using your location in the background\",\n * backgroundTitle: \"Location Service\",\n * requestPermissions: true,\n * stale: false,\n * distanceFilter: 10\n * },\n * (location, error) => {\n * if (error) {\n * console.error('Location error:', error);\n * return;\n * }\n * if (location) {\n * console.log('New location:', location.latitude, location.longitude);\n * }\n * }\n * );\n */\n start(options: StartOptions, callback: (position?: Location, error?: CallbackError) => void): Promise<void>;\n\n /**\n * Stops location updates.\n *\n * @returns A promise that resolves when the plugin stops successfully removed\n *\n * @since 7.0.9\n * @example\n * await BackgroundGeolocation.stop();\n */\n stop(): Promise<void>;\n\n /**\n * Opens the device's location settings page.\n * Useful for directing users to enable location services or adjust permissions.\n *\n * @returns A promise that resolves when the settings page is opened\n *\n * @since 7.0.0\n * @example\n * // Direct user to location settings\n * await BackgroundGeolocation.openSettings();\n */\n openSettings(): Promise<void>;\n\n /**\n * Plays a sound file when the user deviates from the planned route.\n * This should be used to play a sound (in the background too, only for native).\n *\n * @param options The options for setting the planned route and sound file\n * @returns A promise that resolves when the route is set successfully\n *\n * @since 7.0.11\n * @example\n * await BackgroundGeolocation.setPlannedRoute({\n * soundFile: \"notification.mp3\",\n * route: [[-74.0060, 40.7128], [-118.2437, 34.0522]]\n * });\n */\n setPlannedRoute(options: SetPlannedRouteOptions): Promise<void>;\n\n /**\n * Get the native Capacitor plugin version\n *\n * @returns {Promise<{ id: string }>} an Promise with version for this device\n * @throws An error if the something went wrong\n */\n getPluginVersion(): Promise<{ version: string }>;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PluginListenerHandle } from '@capacitor/core';\n\n/**\n * The options for configuring for location updates.\n *\n * @since 7.0.9\n */\nexport interface StartOptions {\n /**\n * If the \"backgroundMessage\" option is defined, the plugin will\n * provide location updates whether the app is in the background or the\n * foreground. If it is not defined, location updates are only\n * guaranteed in the foreground. This is true on both platforms.\n *\n * On Android, a notification must be shown to continue receiving\n * location updates in the background. This option specifies the text of\n * that notification.\n *\n * @since 7.0.9\n * @example \"Getting your location to provide better service\"\n */\n backgroundMessage?: string;\n /**\n * The title of the notification mentioned above.\n *\n * @since 7.0.9\n * @default \"Using your location\"\n * @example \"Location Service\"\n */\n backgroundTitle?: string;\n /**\n * Whether permissions should be requested from the user automatically,\n * if they are not already granted.\n *\n * @since 7.0.9\n * @default true\n * @example\n * // Auto-request permissions\n * requestPermissions: true\n *\n * // Don't auto-request, handle manually\n * requestPermissions: false\n */\n requestPermissions?: boolean;\n /**\n * If \"true\", stale locations may be delivered while the device\n * obtains a GPS fix. You are responsible for checking the \"time\"\n * property. If \"false\", locations are guaranteed to be up to date.\n *\n * @since 7.0.9\n * @default false\n * @example\n * // Allow stale locations for faster initial response\n * stale: true\n *\n * // Only fresh locations\n * stale: false\n */\n stale?: boolean;\n /**\n * The distance in meters that the device must move before a new location update is triggered.\n * This is used to filter out small movements and reduce the number of updates.\n *\n * @since 7.0.9\n * @default 0\n * @example\n * // Update every 10 meters\n * distanceFilter: 10\n *\n * // Update on any movement\n * distanceFilter: 0\n */\n distanceFilter?: number;\n}\n\n/**\n * Represents a geographical location with various attributes.\n * Contains all the standard location properties returned by GPS/network providers.\n *\n * @since 7.0.0\n */\nexport interface Location {\n /**\n * Latitude in degrees.\n * Range: -90.0 to +90.0\n *\n * @since 7.0.0\n * @example 40.7128\n */\n latitude: number;\n /**\n * Longitude in degrees.\n * Range: -180.0 to +180.0\n *\n * @since 7.0.0\n * @example -74.0060\n */\n longitude: number;\n /**\n * Radius of horizontal uncertainty in metres, with 68% confidence.\n * Lower values indicate more accurate location.\n *\n * @since 7.0.0\n * @example 5.0\n */\n accuracy: number;\n /**\n * Metres above sea level (or null if not available).\n *\n * @since 7.0.0\n * @example 10.5\n */\n altitude: number | null;\n /**\n * Vertical uncertainty in metres, with 68% confidence (or null if not available).\n *\n * @since 7.0.0\n * @example 3.0\n */\n altitudeAccuracy: number | null;\n /**\n * `true` if the location was simulated by software, rather than GPS.\n * Useful for detecting mock locations in development or testing.\n *\n * @since 7.0.0\n * @example false\n */\n simulated: boolean;\n /**\n * Deviation from true north in degrees (or null if not available).\n * Range: 0.0 to 360.0\n *\n * @since 7.0.0\n * @example 45.5\n */\n bearing: number | null;\n /**\n * Speed in metres per second (or null if not available).\n *\n * @since 7.0.0\n * @example 2.5\n */\n speed: number | null;\n /**\n * Time the location was produced, in milliseconds since the unix epoch.\n * Use this to check if a location is stale when using stale: true.\n *\n * @since 7.0.0\n * @example 1640995200000\n */\n time: number | null;\n}\n\n/**\n * Error object that may be passed to the location start callback.\n * Extends the standard Error with optional error codes.\n *\n * @since 7.0.0\n */\nexport interface CallbackError extends Error {\n /**\n * Optional error code for more specific error handling.\n *\n * @since 7.0.0\n * @example \"PERMISSION_DENIED\"\n */\n code?: string;\n}\n\nexport interface SetPlannedRouteOptions {\n /**\n * The name of the sound file to play.\n * Must be a valid sound relative path in the app's public folder to work for both web and native platforms.\n * There's no need to include the public folder in the path.\n * @since 7.0.10\n * @example \"notification.mp3\"\n * */\n soundFile: string;\n /**\n * The planned route as an array of longitude and latitude pairs.\n * Each pair represents a point on the route.\n * This is used to define a route that the user can follow.\n * The route is used to play a sound when the user deviates from it.\n * @since 7.0.11\n * @example [[-74.0060, 40.7128], [-118.2437, 34.0522]]\n */\n route: [number, number][];\n\n /**\n * The distance in meters that the user must deviate from the planned route to trigger the sound.\n * This is used to determine how far off the route the user can be before the sound is played.\n * If not specified, a default value of 50 meters is used.\n * @since 7.0.11\n * @default 50\n * @example 50\n */\n distance: number;\n}\n\n/**\n * Options for configuring native geofence transition handling.\n *\n * When `url` is provided, native iOS and Android code sends a JSON `POST`\n * whenever a monitored region is entered or exited. This keeps geofence\n * handling useful when the WebView is suspended.\n *\n * @since 8.0.30\n */\nexport interface GeofenceSetupOptions {\n /**\n * Endpoint that receives geofence transition payloads.\n *\n * @since 8.0.30\n * @example \"https://api.example.com/geofences\"\n */\n url?: string;\n\n /**\n * Whether entry transitions should be monitored.\n *\n * @since 8.0.30\n * @default true\n * @example true\n */\n notifyOnEntry?: boolean;\n\n /**\n * Whether exit transitions should be monitored.\n *\n * @since 8.0.30\n * @default true\n * @example true\n */\n notifyOnExit?: boolean;\n\n /**\n * Base JSON payload merged into every native transition POST and listener event.\n *\n * @since 8.0.30\n * @example { \"userId\": \"123\" }\n */\n payload?: Record<string, unknown>;\n\n /**\n * Whether the plugin should request the native location permission needed for geofencing.\n *\n * iOS geofencing needs Always location authorization. Android background geofencing\n * needs foreground location and, on Android 10+, background location permission.\n *\n * @since 8.0.30\n * @default true\n * @example true\n */\n requestPermissions?: boolean;\n}\n\n/**\n * A circular geofence region.\n *\n * @since 8.0.30\n */\nexport interface AddGeofenceOptions {\n /**\n * Latitude in degrees for the region center.\n *\n * @since 8.0.30\n * @example 40.7128\n */\n latitude: number;\n\n /**\n * Longitude in degrees for the region center.\n *\n * @since 8.0.30\n * @example -74.006\n */\n longitude: number;\n\n /**\n * Region radius in meters.\n *\n * @since 8.0.30\n * @default 50\n * @example 150\n */\n radius?: number;\n\n /**\n * Stable identifier for the geofence.\n *\n * @since 8.0.30\n * @example \"office\"\n */\n identifier: string;\n\n /**\n * Overrides the setup-level entry setting for this region.\n *\n * @since 8.0.30\n */\n notifyOnEntry?: boolean;\n\n /**\n * Overrides the setup-level exit setting for this region.\n *\n * @since 8.0.30\n */\n notifyOnExit?: boolean;\n\n /**\n * Region-specific payload merged over the setup payload.\n *\n * @since 8.0.30\n * @example { \"storeId\": \"nyc-1\" }\n */\n payload?: Record<string, unknown>;\n}\n\n/**\n * Options for removing a monitored geofence.\n *\n * @since 8.0.30\n */\nexport interface RemoveGeofenceOptions {\n /**\n * Identifier passed to `addGeofence`.\n *\n * @since 8.0.30\n * @example \"office\"\n */\n identifier: string;\n}\n\n/**\n * Result returned when listing monitored geofences.\n *\n * @since 8.0.30\n */\nexport interface MonitoredGeofencesResult {\n /**\n * Identifiers for all geofences currently monitored by this plugin.\n *\n * @since 8.0.30\n * @example [\"office\", \"warehouse\"]\n */\n regions: string[];\n}\n\n/**\n * Event emitted when a monitored geofence is entered or exited.\n *\n * The same data is also sent to the configured `url`, when one is set.\n *\n * @since 8.0.30\n */\nexport interface GeofenceTransitionEvent {\n /**\n * Identifier of the geofence that changed state.\n *\n * @since 8.0.30\n * @example \"office\"\n */\n identifier: string;\n\n /**\n * Transition name.\n *\n * @since 8.0.30\n * @example \"enter\"\n */\n transition: 'enter' | 'exit';\n\n /**\n * `true` for entry transitions, `false` for exit transitions.\n *\n * @since 8.0.30\n * @example true\n */\n enter: boolean;\n\n /**\n * Latitude in degrees for the monitored region center, when available.\n *\n * @since 8.0.30\n * @example 40.7128\n */\n latitude?: number;\n\n /**\n * Longitude in degrees for the monitored region center, when available.\n *\n * @since 8.0.30\n * @example -74.006\n */\n longitude?: number;\n\n /**\n * Region radius in meters, when available.\n *\n * @since 8.0.30\n * @example 150\n */\n radius?: number;\n\n /**\n * Merged setup and region payload.\n *\n * @since 8.0.30\n */\n payload?: Record<string, unknown>;\n}\n\n/**\n * Event emitted when native geofence monitoring fails.\n *\n * @since 8.0.30\n */\nexport interface GeofenceErrorEvent {\n /**\n * Identifier of the geofence that failed, when native APIs provide it.\n *\n * @since 8.0.30\n * @example \"office\"\n */\n identifier?: string;\n\n /**\n * Native platform error code.\n *\n * @since 8.0.30\n * @example 5\n */\n code?: number;\n\n /**\n * Native platform error message.\n *\n * @since 8.0.30\n */\n message: string;\n\n /**\n * Native error domain, when available.\n *\n * @since 8.0.30\n */\n domain?: string;\n}\n\n/**\n * Main plugin interface for background geolocation functionality.\n * Provides methods to manage location updates and access device settings.\n *\n * @since 7.0.0\n */\nexport interface BackgroundGeolocationPlugin {\n /**\n * To start listening for changes in the device's location, call this method.\n * A Promise is returned to indicate that it finished the call. The callback will be called every time a new location\n * is available, or if there was an error when calling this method. Don't rely on promise rejection for this.\n *\n * @param options The configuration options\n * @param callback The callback function invoked when a new location is available or an error occurs\n * @returns A promise that resolves when the method is successfully called\n *\n * @since 7.0.9\n * @example\n * await BackgroundGeolocation.start(\n * {\n * backgroundMessage: \"App is using your location in the background\",\n * backgroundTitle: \"Location Service\",\n * requestPermissions: true,\n * stale: false,\n * distanceFilter: 10\n * },\n * (location, error) => {\n * if (error) {\n * console.error('Location error:', error);\n * return;\n * }\n * if (location) {\n * console.log('New location:', location.latitude, location.longitude);\n * }\n * }\n * );\n */\n start(options: StartOptions, callback: (position?: Location, error?: CallbackError) => void): Promise<void>;\n\n /**\n * Stops location updates.\n *\n * @returns A promise that resolves when the plugin stops successfully removed\n *\n * @since 7.0.9\n * @example\n * await BackgroundGeolocation.stop();\n */\n stop(): Promise<void>;\n\n /**\n * Opens the device's location settings page.\n * Useful for directing users to enable location services or adjust permissions.\n *\n * @returns A promise that resolves when the settings page is opened\n *\n * @since 7.0.0\n * @example\n * // Direct user to location settings\n * await BackgroundGeolocation.openSettings();\n */\n openSettings(): Promise<void>;\n\n /**\n * Plays a sound file when the user deviates from the planned route.\n * This should be used to play a sound (in the background too, only for native).\n *\n * @param options The options for setting the planned route and sound file\n * @returns A promise that resolves when the route is set successfully\n *\n * @since 7.0.11\n * @example\n * await BackgroundGeolocation.setPlannedRoute({\n * soundFile: \"notification.mp3\",\n * route: [[-74.0060, 40.7128], [-118.2437, 34.0522]]\n * });\n */\n setPlannedRoute(options: SetPlannedRouteOptions): Promise<void>;\n\n /**\n * Configures native geofence transition handling.\n *\n * Call this before adding geofences when you need native background POSTs\n * or default entry/exit settings.\n *\n * @param options The geofence configuration options\n * @returns A promise that resolves once geofencing is configured\n *\n * @since 8.0.30\n * @example\n * await BackgroundGeolocation.setupGeofencing({\n * url: \"https://api.example.com/geofences\",\n * notifyOnEntry: true,\n * notifyOnExit: true,\n * payload: { userId: \"123\" }\n * });\n */\n setupGeofencing(options: GeofenceSetupOptions): Promise<void>;\n\n /**\n * Starts monitoring a circular native geofence.\n *\n * @param options The geofence region options\n * @returns A promise that resolves when native monitoring starts\n *\n * @since 8.0.30\n * @example\n * await BackgroundGeolocation.addGeofence({\n * identifier: \"office\",\n * latitude: 40.7128,\n * longitude: -74.006,\n * radius: 150\n * });\n */\n addGeofence(options: AddGeofenceOptions): Promise<void>;\n\n /**\n * Stops monitoring one geofence.\n *\n * @param options The geofence identifier\n * @returns A promise that resolves when native monitoring stops\n *\n * @since 8.0.30\n * @example\n * await BackgroundGeolocation.removeGeofence({ identifier: \"office\" });\n */\n removeGeofence(options: RemoveGeofenceOptions): Promise<void>;\n\n /**\n * Stops monitoring every geofence registered by this plugin.\n *\n * @returns A promise that resolves when all native geofences are removed\n *\n * @since 8.0.30\n * @example\n * await BackgroundGeolocation.removeAllGeofences();\n */\n removeAllGeofences(): Promise<void>;\n\n /**\n * Lists the geofence identifiers currently monitored by this plugin.\n *\n * @returns A promise with monitored geofence identifiers\n *\n * @since 8.0.30\n * @example\n * const { regions } = await BackgroundGeolocation.getMonitoredGeofences();\n */\n getMonitoredGeofences(): Promise<MonitoredGeofencesResult>;\n\n /**\n * Listens for geofence enter/exit transitions while the WebView is alive.\n *\n * Native `url` delivery configured through `setupGeofencing` is used for\n * background-safe delivery.\n *\n * @since 8.0.30\n * @example\n * const handle = await BackgroundGeolocation.addListener(\n * \"geofenceTransition\",\n * (event) => console.log(event.identifier, event.transition)\n * );\n */\n addListener(\n eventName: 'geofenceTransition',\n listenerFunc: (event: GeofenceTransitionEvent) => void,\n ): Promise<PluginListenerHandle>;\n\n /**\n * Listens for native geofence monitoring errors while the WebView is alive.\n *\n * @since 8.0.30\n * @example\n * const handle = await BackgroundGeolocation.addListener(\n * \"geofenceError\",\n * (event) => console.error(event.identifier, event.message)\n * );\n */\n addListener(\n eventName: 'geofenceError',\n listenerFunc: (event: GeofenceErrorEvent) => void,\n ): Promise<PluginListenerHandle>;\n\n /**\n * Get the native Capacitor plugin version\n *\n * @returns {Promise<{ id: string }>} an Promise with version for this device\n * @throws An error if the something went wrong\n */\n getPluginVersion(): Promise<{ version: string }>;\n}\n"]}
|
package/dist/esm/web.d.ts
CHANGED
|
@@ -1,16 +1,32 @@
|
|
|
1
1
|
import { WebPlugin } from '@capacitor/core';
|
|
2
|
-
import type { BackgroundGeolocationPlugin, StartOptions, Location, CallbackError, SetPlannedRouteOptions } from './definitions';
|
|
2
|
+
import type { BackgroundGeolocationPlugin, StartOptions, Location, CallbackError, SetPlannedRouteOptions, GeofenceSetupOptions, AddGeofenceOptions, RemoveGeofenceOptions, MonitoredGeofencesResult } from './definitions';
|
|
3
3
|
export declare class BackgroundGeolocationWeb extends WebPlugin implements BackgroundGeolocationPlugin {
|
|
4
4
|
private static readonly EARTH_RADIUS_M;
|
|
5
5
|
private watchId;
|
|
6
|
+
private geofenceWatchId;
|
|
6
7
|
private plannedRoute;
|
|
7
8
|
private audio;
|
|
8
9
|
private isOffRoute;
|
|
9
10
|
private distanceThreshold;
|
|
11
|
+
private geofences;
|
|
12
|
+
private geofenceUrl;
|
|
13
|
+
private geofencePayload;
|
|
14
|
+
private notifyOnEntry;
|
|
15
|
+
private notifyOnExit;
|
|
10
16
|
start(options: StartOptions, callback: (position?: Location, error?: CallbackError) => void): Promise<void>;
|
|
11
17
|
stop(): Promise<void>;
|
|
12
18
|
openSettings(): Promise<void>;
|
|
13
19
|
setPlannedRoute(options: SetPlannedRouteOptions): Promise<void>;
|
|
20
|
+
setupGeofencing(options: GeofenceSetupOptions): Promise<void>;
|
|
21
|
+
addGeofence(options: AddGeofenceOptions): Promise<void>;
|
|
22
|
+
removeGeofence(options: RemoveGeofenceOptions): Promise<void>;
|
|
23
|
+
removeAllGeofences(): Promise<void>;
|
|
24
|
+
getMonitoredGeofences(): Promise<MonitoredGeofencesResult>;
|
|
25
|
+
private validateGeofence;
|
|
26
|
+
private startGeofenceWatch;
|
|
27
|
+
private stopGeofenceWatchIfIdle;
|
|
28
|
+
private checkGeofences;
|
|
29
|
+
private emitGeofenceTransition;
|
|
14
30
|
private toRadians;
|
|
15
31
|
private haversine;
|
|
16
32
|
private distancePointToLineSegment;
|
package/dist/esm/web.js
CHANGED
|
@@ -5,6 +5,10 @@ export class BackgroundGeolocationWeb extends WebPlugin {
|
|
|
5
5
|
this.plannedRoute = [];
|
|
6
6
|
this.isOffRoute = true;
|
|
7
7
|
this.distanceThreshold = 50;
|
|
8
|
+
this.geofences = new Map();
|
|
9
|
+
this.geofencePayload = {};
|
|
10
|
+
this.notifyOnEntry = true;
|
|
11
|
+
this.notifyOnExit = true;
|
|
8
12
|
}
|
|
9
13
|
async start(options, callback) {
|
|
10
14
|
if (!navigator.geolocation) {
|
|
@@ -43,6 +47,7 @@ export class BackgroundGeolocationWeb extends WebPlugin {
|
|
|
43
47
|
}
|
|
44
48
|
this.isOffRoute = offRoute;
|
|
45
49
|
}
|
|
50
|
+
this.checkGeofences(position.coords.latitude, position.coords.longitude);
|
|
46
51
|
callback(location);
|
|
47
52
|
}, (error) => {
|
|
48
53
|
const callbackError = {
|
|
@@ -80,6 +85,109 @@ export class BackgroundGeolocationWeb extends WebPlugin {
|
|
|
80
85
|
this.plannedRoute = options.route || [];
|
|
81
86
|
this.distanceThreshold = options.distance || 50;
|
|
82
87
|
}
|
|
88
|
+
async setupGeofencing(options) {
|
|
89
|
+
var _a, _b, _c;
|
|
90
|
+
if (options.url) {
|
|
91
|
+
new URL(options.url);
|
|
92
|
+
}
|
|
93
|
+
this.geofenceUrl = options.url;
|
|
94
|
+
this.notifyOnEntry = (_a = options.notifyOnEntry) !== null && _a !== void 0 ? _a : true;
|
|
95
|
+
this.notifyOnExit = (_b = options.notifyOnExit) !== null && _b !== void 0 ? _b : true;
|
|
96
|
+
this.geofencePayload = (_c = options.payload) !== null && _c !== void 0 ? _c : {};
|
|
97
|
+
}
|
|
98
|
+
async addGeofence(options) {
|
|
99
|
+
var _a, _b, _c, _d;
|
|
100
|
+
if (!navigator.geolocation) {
|
|
101
|
+
throw new Error('Geolocation is not supported by this browser');
|
|
102
|
+
}
|
|
103
|
+
this.validateGeofence(options.latitude, options.longitude, (_a = options.radius) !== null && _a !== void 0 ? _a : 50, options.identifier);
|
|
104
|
+
this.geofences.set(options.identifier, {
|
|
105
|
+
latitude: options.latitude,
|
|
106
|
+
longitude: options.longitude,
|
|
107
|
+
radius: (_b = options.radius) !== null && _b !== void 0 ? _b : 50,
|
|
108
|
+
identifier: options.identifier,
|
|
109
|
+
notifyOnEntry: (_c = options.notifyOnEntry) !== null && _c !== void 0 ? _c : this.notifyOnEntry,
|
|
110
|
+
notifyOnExit: (_d = options.notifyOnExit) !== null && _d !== void 0 ? _d : this.notifyOnExit,
|
|
111
|
+
payload: options.payload,
|
|
112
|
+
});
|
|
113
|
+
this.startGeofenceWatch();
|
|
114
|
+
}
|
|
115
|
+
async removeGeofence(options) {
|
|
116
|
+
if (!options.identifier) {
|
|
117
|
+
throw new Error('Identifier is required');
|
|
118
|
+
}
|
|
119
|
+
this.geofences.delete(options.identifier);
|
|
120
|
+
this.stopGeofenceWatchIfIdle();
|
|
121
|
+
}
|
|
122
|
+
async removeAllGeofences() {
|
|
123
|
+
this.geofences.clear();
|
|
124
|
+
this.stopGeofenceWatchIfIdle();
|
|
125
|
+
}
|
|
126
|
+
async getMonitoredGeofences() {
|
|
127
|
+
return { regions: Array.from(this.geofences.keys()) };
|
|
128
|
+
}
|
|
129
|
+
validateGeofence(latitude, longitude, radius, identifier) {
|
|
130
|
+
if (!identifier) {
|
|
131
|
+
throw new Error('Identifier is required');
|
|
132
|
+
}
|
|
133
|
+
if (!Number.isFinite(latitude) || latitude < -90 || latitude > 90) {
|
|
134
|
+
throw new Error('Latitude must be between -90 and 90');
|
|
135
|
+
}
|
|
136
|
+
if (!Number.isFinite(longitude) || longitude < -180 || longitude > 180) {
|
|
137
|
+
throw new Error('Longitude must be between -180 and 180');
|
|
138
|
+
}
|
|
139
|
+
if (!Number.isFinite(radius) || radius <= 0) {
|
|
140
|
+
throw new Error('Radius must be greater than 0');
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
startGeofenceWatch() {
|
|
144
|
+
if (this.geofenceWatchId !== undefined || this.geofences.size === 0 || !navigator.geolocation) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this.geofenceWatchId = navigator.geolocation.watchPosition((position) => this.checkGeofences(position.coords.latitude, position.coords.longitude), () => undefined, {
|
|
148
|
+
enableHighAccuracy: false,
|
|
149
|
+
timeout: 30000,
|
|
150
|
+
maximumAge: 60000,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
stopGeofenceWatchIfIdle() {
|
|
154
|
+
if (this.geofences.size > 0 || this.geofenceWatchId === undefined) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
navigator.geolocation.clearWatch(this.geofenceWatchId);
|
|
158
|
+
this.geofenceWatchId = undefined;
|
|
159
|
+
}
|
|
160
|
+
checkGeofences(latitude, longitude) {
|
|
161
|
+
const point = [longitude, latitude];
|
|
162
|
+
this.geofences.forEach((geofence) => {
|
|
163
|
+
const distance = this.haversine(point, [geofence.longitude, geofence.latitude]);
|
|
164
|
+
const inside = distance <= geofence.radius;
|
|
165
|
+
const previousInside = geofence.inside;
|
|
166
|
+
geofence.inside = inside;
|
|
167
|
+
if (inside && previousInside !== true && geofence.notifyOnEntry) {
|
|
168
|
+
this.emitGeofenceTransition(geofence, true);
|
|
169
|
+
}
|
|
170
|
+
else if (!inside && previousInside === true && geofence.notifyOnExit) {
|
|
171
|
+
this.emitGeofenceTransition(geofence, false);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
emitGeofenceTransition(geofence, enter) {
|
|
176
|
+
var _a;
|
|
177
|
+
const payload = Object.assign(Object.assign({}, this.geofencePayload), ((_a = geofence.payload) !== null && _a !== void 0 ? _a : {}));
|
|
178
|
+
const event = Object.assign(Object.assign({}, payload), { identifier: geofence.identifier, transition: enter ? 'enter' : 'exit', enter, latitude: geofence.latitude, longitude: geofence.longitude, radius: geofence.radius, payload });
|
|
179
|
+
void this.notifyListeners('geofenceTransition', event);
|
|
180
|
+
if (this.geofenceUrl) {
|
|
181
|
+
void fetch(this.geofenceUrl, {
|
|
182
|
+
method: 'POST',
|
|
183
|
+
headers: {
|
|
184
|
+
Accept: 'application/json',
|
|
185
|
+
'Content-Type': 'application/json',
|
|
186
|
+
},
|
|
187
|
+
body: JSON.stringify(event),
|
|
188
|
+
}).catch(() => undefined);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
83
191
|
toRadians(degrees) {
|
|
84
192
|
return (degrees * Math.PI) / 180;
|
|
85
193
|
}
|
package/dist/esm/web.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAU5C,MAAM,OAAO,wBAAyB,SAAQ,SAAS;IAAvD;;QAIU,iBAAY,GAAuB,EAAE,CAAC;QAEtC,eAAU,GAAG,IAAI,CAAC;QAClB,sBAAiB,GAAG,EAAE,CAAC;IAiLjC,CAAC;IA/KC,KAAK,CAAC,KAAK,CAAC,OAAqB,EAAE,QAA8D;QAC/F,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;YAC3B,QAAQ,CAAC,SAAS,EAAE;gBAClB,IAAI,EAAE,kBAAkB;gBACxB,OAAO,EAAE,8CAA8C;gBACvD,IAAI,EAAE,eAAe;aACtB,CAAC,CAAC;YACH,OAAO;QACT,CAAC;QAED,I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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 async getPluginVersion(): Promise<{ version: string }> {\n return { version: 'web' };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AA0B5C,MAAM,OAAO,wBAAyB,SAAQ,SAAS;IAAvD;;QAKU,iBAAY,GAAuB,EAAE,CAAC;QAEtC,eAAU,GAAG,IAAI,CAAC;QAClB,sBAAiB,GAAG,EAAE,CAAC;QACvB,cAAS,GAAG,IAAI,GAAG,EAAuB,CAAC;QAE3C,oBAAe,GAA4B,EAAE,CAAC;QAC9C,kBAAa,GAAG,IAAI,CAAC;QACrB,iBAAY,GAAG,IAAI,CAAC;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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;IAED,KAAK,CAAC,gBAAgB;QACpB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;;AA3TuB,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 GeofenceSetupOptions,\n AddGeofenceOptions,\n RemoveGeofenceOptions,\n MonitoredGeofencesResult,\n GeofenceTransitionEvent,\n} from './definitions';\n\ninterface WebGeofence {\n latitude: number;\n longitude: number;\n radius: number;\n identifier: string;\n notifyOnEntry: boolean;\n notifyOnExit: boolean;\n payload?: Record<string, unknown>;\n inside?: boolean;\n}\n\nexport class BackgroundGeolocationWeb extends WebPlugin implements BackgroundGeolocationPlugin {\n private static readonly EARTH_RADIUS_M = 6371000;\n\n private watchId: number | undefined;\n private geofenceWatchId: number | undefined;\n private plannedRoute: [number, number][] = [];\n private audio: HTMLAudioElement | undefined;\n private isOffRoute = true;\n private distanceThreshold = 50;\n private geofences = new Map<string, WebGeofence>();\n private geofenceUrl: string | undefined;\n private geofencePayload: Record<string, unknown> = {};\n private notifyOnEntry = true;\n private notifyOnExit = true;\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 this.checkGeofences(position.coords.latitude, position.coords.longitude);\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 async setupGeofencing(options: GeofenceSetupOptions): Promise<void> {\n if (options.url) {\n new URL(options.url);\n }\n this.geofenceUrl = options.url;\n this.notifyOnEntry = options.notifyOnEntry ?? true;\n this.notifyOnExit = options.notifyOnExit ?? true;\n this.geofencePayload = options.payload ?? {};\n }\n\n async addGeofence(options: AddGeofenceOptions): Promise<void> {\n if (!navigator.geolocation) {\n throw new Error('Geolocation is not supported by this browser');\n }\n this.validateGeofence(options.latitude, options.longitude, options.radius ?? 50, options.identifier);\n this.geofences.set(options.identifier, {\n latitude: options.latitude,\n longitude: options.longitude,\n radius: options.radius ?? 50,\n identifier: options.identifier,\n notifyOnEntry: options.notifyOnEntry ?? this.notifyOnEntry,\n notifyOnExit: options.notifyOnExit ?? this.notifyOnExit,\n payload: options.payload,\n });\n this.startGeofenceWatch();\n }\n\n async removeGeofence(options: RemoveGeofenceOptions): Promise<void> {\n if (!options.identifier) {\n throw new Error('Identifier is required');\n }\n this.geofences.delete(options.identifier);\n this.stopGeofenceWatchIfIdle();\n }\n\n async removeAllGeofences(): Promise<void> {\n this.geofences.clear();\n this.stopGeofenceWatchIfIdle();\n }\n\n async getMonitoredGeofences(): Promise<MonitoredGeofencesResult> {\n return { regions: Array.from(this.geofences.keys()) };\n }\n\n private validateGeofence(latitude: number, longitude: number, radius: number, identifier: string): void {\n if (!identifier) {\n throw new Error('Identifier is required');\n }\n if (!Number.isFinite(latitude) || latitude < -90 || latitude > 90) {\n throw new Error('Latitude must be between -90 and 90');\n }\n if (!Number.isFinite(longitude) || longitude < -180 || longitude > 180) {\n throw new Error('Longitude must be between -180 and 180');\n }\n if (!Number.isFinite(radius) || radius <= 0) {\n throw new Error('Radius must be greater than 0');\n }\n }\n\n private startGeofenceWatch(): void {\n if (this.geofenceWatchId !== undefined || this.geofences.size === 0 || !navigator.geolocation) {\n return;\n }\n this.geofenceWatchId = navigator.geolocation.watchPosition(\n (position) => this.checkGeofences(position.coords.latitude, position.coords.longitude),\n () => undefined,\n {\n enableHighAccuracy: false,\n timeout: 30000,\n maximumAge: 60000,\n },\n );\n }\n\n private stopGeofenceWatchIfIdle(): void {\n if (this.geofences.size > 0 || this.geofenceWatchId === undefined) {\n return;\n }\n navigator.geolocation.clearWatch(this.geofenceWatchId);\n this.geofenceWatchId = undefined;\n }\n\n private checkGeofences(latitude: number, longitude: number): void {\n const point: [number, number] = [longitude, latitude];\n this.geofences.forEach((geofence) => {\n const distance = this.haversine(point, [geofence.longitude, geofence.latitude]);\n const inside = distance <= geofence.radius;\n const previousInside = geofence.inside;\n geofence.inside = inside;\n\n if (inside && previousInside !== true && geofence.notifyOnEntry) {\n this.emitGeofenceTransition(geofence, true);\n } else if (!inside && previousInside === true && geofence.notifyOnExit) {\n this.emitGeofenceTransition(geofence, false);\n }\n });\n }\n\n private emitGeofenceTransition(geofence: WebGeofence, enter: boolean): void {\n const payload = {\n ...this.geofencePayload,\n ...(geofence.payload ?? {}),\n };\n const event: GeofenceTransitionEvent = {\n ...payload,\n identifier: geofence.identifier,\n transition: enter ? 'enter' : 'exit',\n enter,\n latitude: geofence.latitude,\n longitude: geofence.longitude,\n radius: geofence.radius,\n payload,\n };\n void this.notifyListeners('geofenceTransition', event);\n if (this.geofenceUrl) {\n void fetch(this.geofenceUrl, {\n method: 'POST',\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(event),\n }).catch(() => undefined);\n }\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 async getPluginVersion(): Promise<{ version: string }> {\n return { version: 'web' };\n }\n}\n"]}
|