forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocoding.js
More file actions
52 lines (49 loc) 路 2.12 KB
/
Copy pathgeocoding.js
File metadata and controls
52 lines (49 loc) 路 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const GEOCODING_TIMEOUT = 8000;
const NOMINATIM_BASE = 'https://nominatim.openstreetmap.org';
async function geocodeAddress(address) {
const url = NOMINATIM_BASE + '/search?q=' + encodeURIComponent(address) + '&format=json&limit=1&addressdetails=1';
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), GEOCODING_TIMEOUT);
try {
const response = await fetch(url, {
signal: controller.signal,
headers: { 'Accept-Language': 'it', 'User-Agent': 'MyZubster/1.0' },
});
if (!response.ok) throw new Error('Nominatim risponde ' + response.status);
const results = await response.json();
if (!results || results.length === 0) return null;
const first = results[0];
return {
lat: parseFloat(first.lat),
lng: parseFloat(first.lon),
displayName: first.display_name,
neighborhood: (first.address && (first.address.suburb || first.address.neighbourhood)) || '',
city: (first.address && (first.address.city || first.address.town || first.address.village || first.address.municipality)) || '',
};
} finally {
clearTimeout(timeout);
}
}
async function reverseGeocode(lat, lng) {
const url = NOMINATIM_BASE + '/reverse?lat=' + lat + '&lon=' + lng + '&format=json&addressdetails=1';
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), GEOCODING_TIMEOUT);
try {
const response = await fetch(url, {
signal: controller.signal,
headers: { 'Accept-Language': 'it', 'User-Agent': 'MyZubster/1.0' },
});
if (!response.ok) throw new Error('Nominatim risponde ' + response.status);
const data = await response.json();
if (!data || data.error) return null;
return {
displayName: data.display_name,
address: data.display_name,
neighborhood: (data.address && (data.address.suburb || data.address.neighbourhood)) || '',
city: (data.address && (data.address.city || data.address.town || data.address.village || data.address.municipality)) || '',
};
} finally {
clearTimeout(timeout);
}
}
module.exports = { geocodeAddress, reverseGeocode };