Query Nominatim directly for geocoding and reverse geocoding in poverty-mapping
This commit is contained in:
+12
-41
@@ -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); }; }
|
function debounce(fn, wait){ let t=null; return function(...args){ clearTimeout(t); t=setTimeout(()=>fn.apply(this,args), wait); }; }
|
||||||
async function geocodeSearch(q){
|
async function geocodeSearch(q){
|
||||||
if(!q || q.length<2) return [];
|
if(!q || q.length<2) return [];
|
||||||
const url = 'src/api/geocode.php?action=search&q=' + encodeURIComponent(q);
|
const url = 'https://nominatim.openstreetmap.org/search?format=jsonv2&addressdetails=1&limit=6&q=' + encodeURIComponent(q);
|
||||||
const maxAttempts = 3;
|
try{
|
||||||
let attempt = 0;
|
const r = await fetch(url, { headers: { 'Accept': 'application/json' } });
|
||||||
while(attempt < maxAttempts){
|
if(r.ok){ const j = await r.json().catch(()=>null); return j || []; }
|
||||||
attempt++;
|
}catch(e){
|
||||||
try{
|
console.error('geocodeSearch failed', e);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
try{ showToast && showToast('Layanan geocoding sedang tidak tersedia. Coba lagi nanti.', 'error', 5000); }catch(e){}
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function reverseGeocode(lat, lon){
|
async function reverseGeocode(lat, lon){
|
||||||
const url = 'src/api/geocode.php?action=reverse&lat=' + encodeURIComponent(lat) + '&lon=' + encodeURIComponent(lon);
|
const url = `https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${encodeURIComponent(lat)}&lon=${encodeURIComponent(lon)}`;
|
||||||
const maxAttempts = 3;
|
try{
|
||||||
let attempt = 0;
|
const r = await fetch(url, { headers: { 'Accept': 'application/json' } });
|
||||||
while(attempt < maxAttempts){
|
if(r.ok){ const j = await r.json().catch(()=>null); return j || null; }
|
||||||
attempt++;
|
}catch(e){
|
||||||
try{
|
console.error('reverseGeocode failed', e);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
try{ showToast && showToast('Layanan reverse-geocoding tidak tersedia. Coba lagi nanti.', 'error', 5000); }catch(e){}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
function detectWorshipType(r){ try{
|
function detectWorshipType(r){ try{
|
||||||
|
|||||||
Reference in New Issue
Block a user