@sumaris-net/ngx-components 2.4.64 → 2.4.65

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.
@@ -21630,13 +21630,44 @@ class JobUtils {
21630
21630
  class IPosition {
21631
21631
  }
21632
21632
  class GeolocationUtils {
21633
+ static isNotNilAndValid(position, opts) {
21634
+ if (!position || isNil(position.latitude) || isNil(position.longitude))
21635
+ return false;
21636
+ // Invalid lat/lon
21637
+ if (position.latitude < -90 || position.latitude > 90
21638
+ || position.longitude < -180 || position.longitude > 180) {
21639
+ // /!\ Log in console, because should never occur
21640
+ if (opts?.debug)
21641
+ console.warn('Invalid lat/long position: ', position);
21642
+ return false;
21643
+ }
21644
+ // OK: valid
21645
+ return true;
21646
+ }
21647
+ static isNilOrInvalid(position, opts) {
21648
+ return !GeolocationUtils.isNotNilAndValid(position, opts);
21649
+ }
21650
+ static computeDistanceInMiles(position1, position2) {
21651
+ // Invalid position(s): skip
21652
+ if (GeolocationUtils.isNilOrInvalid(position1, { debug: true })
21653
+ || GeolocationUtils.isNilOrInvalid(position2, { debug: true }))
21654
+ return;
21655
+ const latitude1Rad = Math.PI * position1.latitude / 180;
21656
+ const longitude1Rad = Math.PI * position1.longitude / 180;
21657
+ const latitude2Rad = Math.PI * position2.latitude / 180;
21658
+ const longitude2Rad = Math.PI * position2.longitude / 180;
21659
+ let distance = 2 * 6371 * Math.asin(Math.sqrt(Math.pow(Math.sin((latitude1Rad - latitude2Rad) / 2), 2)
21660
+ + Math.cos(latitude1Rad) * Math.cos(latitude2Rad) * Math.pow(Math.sin((longitude2Rad - longitude1Rad) / 2), 2)));
21661
+ distance = Math.round((((distance / 1.852) + Number.EPSILON) * 100)) / 100;
21662
+ return distance;
21663
+ }
21633
21664
  /**
21634
21665
  * Get the position by geo loc sensor
21635
21666
  */
21636
21667
  static async getCurrentPosition(platform, options) {
21637
21668
  // Use Capacitor plugin
21638
21669
  try {
21639
- console.info(`[position-utils] Get current geo position, using Capacitor...(timeout: ${options?.timeout})`);
21670
+ console.info(`[geolocation-utils] Get current geo location, using Capacitor...(timeout: ${options?.timeout})`);
21640
21671
  const pos = await Geolocation.getCurrentPosition(options);
21641
21672
  return {
21642
21673
  latitude: pos.coords.latitude,
@@ -21644,13 +21675,19 @@ class GeolocationUtils {
21644
21675
  };
21645
21676
  }
21646
21677
  catch (err) {
21647
- console.error('[position-utils] Cannot get current geo position, using Capacitor:', err);
21648
- // Stop if capacitor (cannot use browser geolocation, because of browser security limitation)
21649
- if (platform.is('capacitor'))
21678
+ // If capacitor : throw error (cannot fallback to browser geolocation, because of browser security limitation)
21679
+ if (platform.is('capacitor')) {
21680
+ console.error(`[geolocation-utils] Cannot get current geo location, using Capacitor: ${err?.message || ''} (code=${err?.code || 'null'})`);
21681
+ const permissions = await Geolocation.checkPermissions();
21682
+ const permissionStatus = permissions?.coarseLocation || permissions?.location;
21683
+ if (permissionStatus === 'denied') {
21684
+ throw { ...err, code: GeolocationPositionError.PERMISSION_DENIED };
21685
+ }
21650
21686
  throw err;
21687
+ }
21651
21688
  }
21652
21689
  // Or fallback to navigator
21653
- console.info('[position-utils] Get current geo position, using browser...');
21690
+ console.info('[geolocation-utils] Get current geo location, using browser...');
21654
21691
  return new Promise((resolve, reject) => {
21655
21692
  navigator.geolocation.getCurrentPosition((res) => {
21656
21693
  resolve({
@@ -21660,7 +21697,7 @@ class GeolocationUtils {
21660
21697
  }, (err) => {
21661
21698
  let message = err?.message || err;
21662
21699
  message = typeof message === 'string' ? message : JSON.stringify(message);
21663
- console.error('[position-utils] Cannot get current geo position, using browser:' + message);
21700
+ console.error('[geolocation-utils] Cannot get current geo location, using browser:' + message);
21664
21701
  reject(err);
21665
21702
  }, options);
21666
21703
  });