Add ability to detect user's city using geolocation

Implement geolocation functionality in the weather widget to automatically detect and set the user's city based on GPS coordinates, updating weather data accordingly.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: d43bd811-9372-45a7-8ac9-4a954c0538e1
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/d43bd811-9372-45a7-8ac9-4a954c0538e1/zoZabWF
This commit is contained in:
michaelschal committed 2025-08-15 11:20:09 +00:00
1 parent 8eb0458ce2
commit c65860cb85
4 files changed
+202 -6

No files matched your search

+54
View File
@@ -2934,6 +2934,60 @@ RÉSUMÉ DU SCAN
}
});
// Route de géolocalisation météo
app.post('/api/weather/geolocation', isAuthenticated, async (req: any, res) => {
try {
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const { latitude, longitude } = req.body;
if (!latitude || !longitude) {
return res.status(400).json({ message: "Latitude et longitude requises" });
}
// Récupérer les paramètres météo actuels pour obtenir la clé API
const settings = await storage.getWeatherSettings();
if (!settings || !settings.apiKey) {
return res.status(500).json({ message: "Configuration météo manquante" });
}
// Convertir les coordonnées en nom de ville
const locationData = await weatherService.getCityFromCoordinates(
parseFloat(latitude),
parseFloat(longitude),
settings.apiKey
);
if (!locationData) {
return res.status(500).json({ message: "Impossible de déterminer la ville à partir des coordonnées" });
}
// Mettre à jour automatiquement la configuration météo avec la nouvelle localisation
await storage.updateWeatherSettings(settings.id, {
location: locationData.fullLocation
});
console.log('🌍 Localisation mise à jour automatiquement:', {
from: settings.location,
to: locationData.fullLocation,
coordinates: { latitude, longitude }
});
res.json({
success: true,
location: locationData,
message: `Localisation mise à jour vers ${locationData.city}, ${locationData.country}`
});
} catch (error) {
console.error("Erreur géolocalisation météo:", error);
res.status(500).json({ message: "Erreur lors de la géolocalisation" });
}
});
// Create server instance
const httpServer = createServer(app);
+40
View File
@@ -242,6 +242,46 @@ export class WeatherService {
return { success: false, message: "Erreur de connexion à l'API météo" };
}
}
/**
* Convertit les coordonnées GPS en nom de ville via l'API Visual Crossing
*/
async getCityFromCoordinates(latitude: number, longitude: number, apiKey: string): Promise<{ city: string; country: string; fullLocation: string } | null> {
try {
const url = `${this.baseUrl}/${latitude},${longitude}?unitGroup=metric&include=current&key=${apiKey}&contentType=json`;
console.log('🌍 Géolocalisation:', { latitude, longitude });
const response = await fetch(url);
if (!response.ok) {
console.error(`Geolocation API error: ${response.status} ${response.statusText}`);
return null;
}
const data = await response.json();
// L'API Visual Crossing retourne les informations de localisation
if (data.resolvedAddress) {
const address = data.resolvedAddress;
const parts = address.split(',').map((part: string) => part.trim());
// Extraire la ville et le pays de l'adresse
const city = parts[0] || 'Ville inconnue';
const country = parts[parts.length - 1] || 'Pays inconnu';
return {
city,
country,
fullLocation: address
};
}
return null;
} catch (error) {
console.error("Erreur lors de la géolocalisation:", error);
return null;
}
}
}
export const weatherService = new WeatherService();