@sumaris-net/ngx-components 2.4.64 → 2.4.66
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/esm2020/src/app/shared/geolocation/geolocation.utils.mjs +51 -7
- package/fesm2015/sumaris-net.ngx-components.mjs +49 -6
- package/fesm2015/sumaris-net.ngx-components.mjs.map +1 -1
- package/fesm2020/sumaris-net.ngx-components.mjs +49 -6
- package/fesm2020/sumaris-net.ngx-components.mjs.map +1 -1
- package/package.json +1 -1
- package/src/app/shared/geolocation/geolocation.utils.d.ts +9 -1
|
@@ -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(`[
|
|
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,25 @@ class GeolocationUtils {
|
|
|
21644
21675
|
};
|
|
21645
21676
|
}
|
|
21646
21677
|
catch (err) {
|
|
21647
|
-
|
|
21648
|
-
|
|
21649
|
-
|
|
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
|
+
try {
|
|
21682
|
+
const permissions = await Geolocation.checkPermissions();
|
|
21683
|
+
console.error(`[geolocation-utils] Permissions: ${JSON.stringify(permissions)})`);
|
|
21684
|
+
const permissionStatus = permissions?.coarseLocation || permissions?.location;
|
|
21685
|
+
if (permissionStatus === 'denied') {
|
|
21686
|
+
throw { ...err, code: GeolocationPositionError.PERMISSION_DENIED };
|
|
21687
|
+
}
|
|
21688
|
+
}
|
|
21689
|
+
catch (e2) {
|
|
21690
|
+
console.error(`[geolocation-utils] Error while calling checkPermissions(): ${err?.message || ''}`);
|
|
21691
|
+
}
|
|
21650
21692
|
throw err;
|
|
21693
|
+
}
|
|
21651
21694
|
}
|
|
21652
21695
|
// Or fallback to navigator
|
|
21653
|
-
console.info('[
|
|
21696
|
+
console.info('[geolocation-utils] Get current geo location, using browser...');
|
|
21654
21697
|
return new Promise((resolve, reject) => {
|
|
21655
21698
|
navigator.geolocation.getCurrentPosition((res) => {
|
|
21656
21699
|
resolve({
|
|
@@ -21660,7 +21703,7 @@ class GeolocationUtils {
|
|
|
21660
21703
|
}, (err) => {
|
|
21661
21704
|
let message = err?.message || err;
|
|
21662
21705
|
message = typeof message === 'string' ? message : JSON.stringify(message);
|
|
21663
|
-
console.error('[
|
|
21706
|
+
console.error('[geolocation-utils] Cannot get current geo location, using browser:' + message);
|
|
21664
21707
|
reject(err);
|
|
21665
21708
|
}, options);
|
|
21666
21709
|
});
|