Query Nominatim directly for geocoding and reverse geocoding in poverty-mapping

This commit is contained in:
ilham_gmail
2026-06-11 19:34:01 +07:00
parent ff5ff49597
commit 0593d25080
+12 -41
View File
@@ -1280,53 +1280,24 @@ function syncAssistanceSourceSearch(){
function debounce(fn, wait){ let t=null; return function(...args){ clearTimeout(t); t=setTimeout(()=>fn.apply(this,args), wait); }; }
async function geocodeSearch(q){
if(!q || q.length<2) return [];
const url = 'src/api/geocode.php?action=search&q=' + encodeURIComponent(q);
const maxAttempts = 3;
let attempt = 0;
while(attempt < maxAttempts){
attempt++;
try{
const r = await fetch(url, { headers: { 'Accept': 'application/json' } });
if(r.ok){ const j = await r.json().catch(()=>null); return j || []; }
// server returned 5xx/4xx
console.warn('geocodeSearch: nominatim returned', r.status, r.statusText);
if(r.status >= 500){
// server error: retry after delay
await new Promise(res => setTimeout(res, 250 * attempt));
continue;
}
// client error: don't retry
return [];
}catch(e){
console.error('geocodeSearch failed attempt', attempt, e);
// network or CORS error: retry with backoff
await new Promise(res => setTimeout(res, 300 * attempt));
continue;
}
const url = 'https://nominatim.openstreetmap.org/search?format=jsonv2&addressdetails=1&limit=6&q=' + encodeURIComponent(q);
try{
const r = await fetch(url, { headers: { 'Accept': 'application/json' } });
if(r.ok){ const j = await r.json().catch(()=>null); return j || []; }
}catch(e){
console.error('geocodeSearch failed', e);
}
try{ showToast && showToast('Layanan geocoding sedang tidak tersedia. Coba lagi nanti.', 'error', 5000); }catch(e){}
return [];
}
async function reverseGeocode(lat, lon){
const url = 'src/api/geocode.php?action=reverse&lat=' + encodeURIComponent(lat) + '&lon=' + encodeURIComponent(lon);
const maxAttempts = 3;
let attempt = 0;
while(attempt < maxAttempts){
attempt++;
try{
const r = await fetch(url, { headers: { 'Accept': 'application/json' } });
if(r.ok){ const j = await r.json().catch(()=>null); return j || null; }
console.warn('reverseGeocode: nominatim returned', r.status, r.statusText);
if(r.status >= 500){ await new Promise(res => setTimeout(res, 250 * attempt)); continue; }
return null;
}catch(e){
console.error('reverseGeocode failed attempt', attempt, e);
await new Promise(res => setTimeout(res, 300 * attempt));
continue;
}
const url = `https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${encodeURIComponent(lat)}&lon=${encodeURIComponent(lon)}`;
try{
const r = await fetch(url, { headers: { 'Accept': 'application/json' } });
if(r.ok){ const j = await r.json().catch(()=>null); return j || null; }
}catch(e){
console.error('reverseGeocode failed', e);
}
try{ showToast && showToast('Layanan reverse-geocoding tidak tersedia. Coba lagi nanti.', 'error', 5000); }catch(e){}
return null;
}
function detectWorshipType(r){ try{