Implement geolocation features and enhance UI for location handling

This commit is contained in:
xavs
2026-05-18 11:38:20 +02:00
parent 355729ddcf
commit c2fe21d9e8
5 changed files with 133 additions and 10 deletions
+66 -9
View File
@@ -15,8 +15,8 @@ import { PrediccioHorariaComponent } from '../prediccio-horaria/prediccio-horari
styleUrl: './home.scss',
})
export class Home implements OnInit {
// Lliçà d'Amunt por defecto
readonly municipiCodi = '081075';
// Municipio dinámico según GPS
municipiCodi = signal<string>('081075'); // Lliçà d'Amunt por defecto
municipi = signal<Municipi | null>(null);
prediccioDiaria = signal<PrediccioDiaria[]>([]);
@@ -25,19 +25,69 @@ export class Home implements OnInit {
loading = signal(true);
error = signal<string | null>(null);
gettingLocation = signal(false);
locationDenied = signal(false);
constructor(private meteocatService: MeteocatService) {}
ngOnInit(): void {
this.loadWeatherData();
this.getCurrentLocation();
}
loadWeatherData(): void {
getCurrentLocation(): void {
if (!navigator.geolocation) {
console.warn('Geolocalización no disponible, usando municipio por defecto');
this.loadWeatherData(this.municipiCodi());
return;
}
this.gettingLocation.set(true);
this.loading.set(true);
navigator.geolocation.getCurrentPosition(
(position) => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
console.log(`Ubicación GPS obtenida: ${lat}, ${lon}`);
// Buscar municipio más cercano
this.meteocatService.getMunicipiCercano(lat, lon).subscribe({
next: (municipi) => {
console.log(`Municipio más cercano: ${municipi.nom} (${municipi.codi})`);
this.municipiCodi.set(municipi.codi);
this.gettingLocation.set(false);
this.loadWeatherData(municipi.codi);
},
error: (err) => {
console.error('Error al buscar municipio cercano:', err);
this.gettingLocation.set(false);
// Usar municipio por defecto si falla
this.loadWeatherData(this.municipiCodi());
}
});
},
(error) => {
console.warn('Error obteniendo ubicación:', error.message);
this.gettingLocation.set(false);
this.locationDenied.set(true);
// Usar municipio por defecto si se deniega el permiso
this.loadWeatherData(this.municipiCodi());
},
{
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 300000 // Cache de 5 minutos
}
);
}
loadWeatherData(codiMunicipi: string): void {
this.loading.set(true);
this.error.set(null);
// Cargar municipio
this.meteocatService.getMunicipiByCode(this.municipiCodi).subscribe({
this.meteocatService.getMunicipiByCode(codiMunicipi).subscribe({
next: (municipi) => {
this.municipi.set(municipi);
},
@@ -48,7 +98,7 @@ export class Home implements OnInit {
});
// Cargar predicción diaria
this.meteocatService.getPrediccioDiaria(this.municipiCodi).subscribe({
this.meteocatService.getPrediccioDiaria(codiMunicipi).subscribe({
next: (prediccions) => {
this.prediccioDiaria.set(prediccions);
},
@@ -59,7 +109,7 @@ export class Home implements OnInit {
});
// Cargar predicción horaria
this.meteocatService.getPrediccioHoraria(this.municipiCodi).subscribe({
this.meteocatService.getPrediccioHoraria(codiMunicipi).subscribe({
next: (prediccions) => {
// Filtrar solo predicciones desde la hora actual y limitamos a 48 horas
const now = new Date();
@@ -81,7 +131,7 @@ export class Home implements OnInit {
});
// Cargar estación XEMA más cercana
this.meteocatService.getEstacioCercana(this.municipiCodi).subscribe({
this.meteocatService.getEstacioCercana(codiMunicipi).subscribe({
next: (response) => {
this.estacioInfo.set(response);
},
@@ -111,6 +161,13 @@ export class Home implements OnInit {
}
refresh(): void {
this.loadWeatherData();
// Recargar datos con el municipio actual
this.loadWeatherData(this.municipiCodi());
}
requestLocation(): void {
// Solicitar ubicación nuevamente
this.locationDenied.set(false);
this.getCurrentLocation();
}
}