Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/core/services/geolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ import { CoreUtils } from './utils/utils';
import { CorePlatform } from './platform';
import { CoreSilentError } from '@classes/errors/silenterror';
import { CoreSubscriptions } from '@singletons/subscriptions';
import { CoreLogger } from '@singletons/logger';

@Injectable({ providedIn: 'root' })
export class CoreGeolocationProvider {

protected logger: CoreLogger;

constructor() {
this.logger = CoreLogger.getInstance('CoreGeolocationProvider');
}

/**
* Get current user coordinates.
*
Expand All @@ -34,16 +41,21 @@ export class CoreGeolocationProvider {
*/
async getCoordinates(): Promise<Coordinates> {
try {
this.logger.log('Getting coordinates.');
await this.authorizeLocation();
await this.enableLocation();
this.logger.log('Getting coordinates: authorized and enabled.');

const result = await Geolocation.getCurrentPosition({
enableHighAccuracy: true,
timeout: 30000,
});
this.logger.log('Coordinates retrieved');

return result.coords;
} catch (error) {
this.logger.log('Error getting coordinates.', error);

if (this.isCordovaPermissionDeniedError(error)) {
throw new CoreGeolocationError(CoreGeolocationErrorReason.PERMISSION_DENIED);
}
Expand Down Expand Up @@ -94,14 +106,18 @@ export class CoreGeolocationProvider {
*/
protected async doAuthorizeLocation(failOnDeniedOnce: boolean = false): Promise<void> {
const authorizationStatus = await Diagnostic.getLocationAuthorizationStatus();
this.logger.log(`Authorize location: status ${authorizationStatus}`);

switch (authorizationStatus) {
case Diagnostic.permissionStatus.DENIED_ONCE:
if (failOnDeniedOnce) {
throw new CoreGeolocationError(CoreGeolocationErrorReason.PERMISSION_DENIED);
}
// Fall through.
case Diagnostic.permissionStatus.NOT_REQUESTED:
this.logger.log('Request location authorization.');
await this.requestLocationAuthorization();
this.logger.log('Location authorization granted.');
await CoreApp.waitForResume(500);
await this.doAuthorizeLocation(true);

Expand Down