2023-01-03 17:17:12 +00:00
|
|
|
import memoize from 'memoizee';
|
|
|
|
|
2023-01-03 17:43:40 +00:00
|
|
|
export const _findAddress = async (
|
|
|
|
lon: number,
|
|
|
|
lat: number,
|
|
|
|
locale: () => string
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const response = await fetch(
|
|
|
|
`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2&addressdetails=1&extratags=1&namedetails=1&accept-language=${locale()}`,
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
const data = await response.json();
|
|
|
|
console.log({ caller: 'findAddress', lon, lat, data });
|
|
|
|
return data;
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ caller: 'findAddress', lon, lat, error });
|
|
|
|
return {};
|
|
|
|
}
|
2023-01-03 17:17:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const findAddress = memoize(_findAddress, {
|
|
|
|
promise: true,
|
|
|
|
max: 1000,
|
2023-01-03 17:43:40 +00:00
|
|
|
maxAge: 36000000,
|
2023-01-03 17:17:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export const getVillageOrTown = (address: any) => {
|
2023-01-03 17:43:40 +00:00
|
|
|
if (address?.address?.village) {
|
|
|
|
return address.address.village;
|
2023-01-03 17:17:12 +00:00
|
|
|
}
|
2023-01-03 17:43:40 +00:00
|
|
|
if (address?.address?.city) {
|
|
|
|
return address.address.city;
|
2023-01-03 17:24:38 +00:00
|
|
|
}
|
2023-01-03 17:43:40 +00:00
|
|
|
return address?.address?.town;
|
2023-01-03 17:17:12 +00:00
|
|
|
};
|