57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { Coordinates } from '@app/domain/localisation/coordinates.model';
|
|
import { from, Observable } from 'rxjs';
|
|
import { Injectable } from '@angular/core';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class GetCurrentLocationUseCase {
|
|
execute(): Observable<Coordinates> {
|
|
return from(
|
|
new Promise<Coordinates>((resolve, reject) => {
|
|
if (!navigator.geolocation) {
|
|
reject({
|
|
code: 0,
|
|
message: "La géolocalisation n'est pas supportée par votre navigateur",
|
|
});
|
|
return;
|
|
}
|
|
|
|
navigator.geolocation.getCurrentPosition(
|
|
(position) => {
|
|
resolve({
|
|
latitude: position.coords.latitude,
|
|
longitude: position.coords.longitude,
|
|
});
|
|
},
|
|
(error) => {
|
|
let message = 'Erreur de géolocalisation';
|
|
|
|
switch (error.code) {
|
|
case error.PERMISSION_DENIED:
|
|
message = 'Permission de géolocalisation refusée';
|
|
break;
|
|
case error.POSITION_UNAVAILABLE:
|
|
message = 'Position indisponible';
|
|
break;
|
|
case error.TIMEOUT:
|
|
message = 'Délai de géolocalisation dépassé';
|
|
break;
|
|
}
|
|
|
|
reject({
|
|
code: error.code,
|
|
message,
|
|
});
|
|
},
|
|
{
|
|
enableHighAccuracy: true,
|
|
timeout: 10000,
|
|
maximumAge: 0,
|
|
}
|
|
);
|
|
})
|
|
);
|
|
}
|
|
}
|