Skip to content

Commit

Permalink
Fixes #2085 - Can't approve location permission on Android 13
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Dec 18, 2024
1 parent 9af37ef commit 5e76e29
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions IsraelHiking.Web/src/application/services/geo-location.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class GeoLocationService {
this.loggingService.debug(`[GeoLocation] Now in ${this.isBackground ? "back" : "fore"}ground`);
if (state.isActive) {
if (!this.store.selectSnapshot((s: ApplicationState) => s.recordedRouteState).isRecording) {
this.getRoughPosition();
this.startBackgroundGeolocation();
}
this.ngZone.run(async () => {
Expand Down Expand Up @@ -105,18 +104,8 @@ export class GeoLocationService {
}
}

private getRoughPosition() {
if (window.navigator && window.navigator.geolocation) {
// Upon starting location watching get the current position as fast as we can, even if not accurate.
window.navigator.geolocation.getCurrentPosition((position: GeolocationPosition) => {
this.handlePositionChange(position);
}, () => {}, { timeout: GeoLocationService.SHORT_TIME_OUT });
}
}

private startWatching() {
this.store.dispatch(new SetTrackingStateAction("searching"));
this.getRoughPosition();
if (this.runningContextService.isCapacitor) {
this.startBackgroundGeolocation();
} else {
Expand Down Expand Up @@ -147,10 +136,9 @@ export class GeoLocationService {
});
}

private startBackgroundGeolocation() {
private async startBackgroundGeolocation() {
if (this.wasInitialized) {
this.loggingService.info("[GeoLocation] Starting background tracking");
BackgroundGeolocation.start();
this.startBackgroundGeolocationWithRoughPosition();
return;
}
this.loggingService.info("[GeoLocation] Initializing background tracking");
Expand Down Expand Up @@ -198,7 +186,27 @@ export class GeoLocationService {
this.toastService.warning(this.resources.unableToFindYourLocation);
this.disable();
});

this.startBackgroundGeolocationWithRoughPosition();
}

private async startBackgroundGeolocationWithRoughPosition() {
this.loggingService.info("[GeoLocation] Starting background tracking");
BackgroundGeolocation.start();

if (window.navigator?.geolocation == null) {
return;
}
const status = await BackgroundGeolocation.checkStatus();
if (status.authorization === BackgroundGeolocation.NOT_AUTHORIZED) {
return;
}
window.navigator.geolocation.getCurrentPosition((position: GeolocationPosition) => {
// Upon starting location watching get the current position as fast as we can, even if not accurate, only update if we didn't reveice a location already.
if (this.store.selectSnapshot((s: ApplicationState) => s.gpsState).tracking === "searching") {
this.handlePositionChange(position);
}
}, () => {}, { timeout: GeoLocationService.SHORT_TIME_OUT });
}

private async onLocationUpdate() {
Expand Down

0 comments on commit 5e76e29

Please sign in to comment.