20 lines
691 B
TypeScript
20 lines
691 B
TypeScript
|
|
// geocoding.ts
|
||
|
|
export const getCityFromCoordinates = async (lat: number, lon: number): Promise<string | null> => {
|
||
|
|
const url = `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await fetch(url);
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error('Errore nella richiesta di geocoding');
|
||
|
|
}
|
||
|
|
const data = await response.json();
|
||
|
|
|
||
|
|
// Controlla se la città è presente nei risultati
|
||
|
|
return data.address?.city || data.address?.town || data.address?.village || null;
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Errore durante il geocoding:', error);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|