v9: district list via Nominatim bounding box
This commit is contained in:
parent
6bd858b709
commit
69d9d1276c
48
index.html
48
index.html
@ -162,40 +162,36 @@ function onRegionChange(){
|
|||||||
if(!r){document.getElementById('locStatus').textContent='';return;}
|
if(!r){document.getElementById('locStatus').textContent='';return;}
|
||||||
map.setView(r.center,9);
|
map.setView(r.center,9);
|
||||||
document.getElementById('locStatus').textContent='⏳ районы...';
|
document.getElementById('locStatus').textContent='⏳ районы...';
|
||||||
// Overpass: ищем админ-границы 6-8 уровня внутри области
|
// Ищем область в Nominatim, получаем bounding box, ищем районы внутри
|
||||||
const q='[out:json];(area["name"="'+name+' область"]["admin_level"~"[46]"];)->.a;(rel(area.a)["admin_level"~"[678]"];);out tags;';
|
const searchName=name+' область';
|
||||||
fetch('https://overpass-api.de/api/interpreter?data='+encodeURIComponent(q))
|
fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(searchName)+'&countrycodes=kz&limit=1')
|
||||||
.then(r=>r.json())
|
.then(r=>r.json()).then(reg=>{
|
||||||
.then(data=>{
|
if(!reg.length)return fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(name)+'&countrycodes=kz&limit=1').then(r=>r.json());
|
||||||
if(!data||!data.elements){document.getElementById('locStatus').textContent='нет данных';return;}
|
return reg;
|
||||||
|
}).then(reg=>{
|
||||||
|
if(!reg.length)throw new Error('region not found');
|
||||||
|
const b=reg[0].boundingbox;
|
||||||
|
return fetch('https://nominatim.openstreetmap.org/search?format=json&q=район&countrycodes=kz&bounded=1&viewbox='+b[2]+','+b[0]+','+b[3]+','+b[1]+'&limit=40');
|
||||||
|
})
|
||||||
|
.then(r=>r.json()).then(data=>{
|
||||||
const names=new Set();
|
const names=new Set();
|
||||||
data.elements.forEach(el=>{
|
data.forEach(item=>{
|
||||||
if(!el.tags||!el.tags.name)return;
|
if(item.type!='administrative')return;
|
||||||
let n=el.tags.name.replace(/\s*ауданы$/,'').replace(/\s*районы$/,'').replace(/\s*район$/,'').trim();
|
const n=item.display_name.split(',')[0].trim();
|
||||||
|
// отсекаем лишнее
|
||||||
|
if(n.toLowerCase().includes('город')||n===name)return;
|
||||||
if(n&&!names.has(n))names.add(n);
|
if(n&&!names.has(n))names.add(n);
|
||||||
});
|
});
|
||||||
const sorted=[...names].sort();
|
const sorted=[...names].sort();
|
||||||
sorted.forEach(n=>{
|
sorted.forEach(n=>{
|
||||||
const o=document.createElement('option');
|
const o=document.createElement('option');
|
||||||
o.value=n;
|
o.value=n;o.textContent=n;
|
||||||
o.textContent=n;
|
|
||||||
sel.appendChild(o);
|
sel.appendChild(o);
|
||||||
});
|
});
|
||||||
document.getElementById('locStatus').textContent=sorted.length?'✅ районы: '+sorted.length: '⚠ районы не найдены';
|
document.getElementById('locStatus').textContent=sorted.length?'✅ '+sorted.length+' районов':'⚠ районы не найдены (введите село вручную)';
|
||||||
if(sorted.length===0){
|
})
|
||||||
// fallback: поищем через Nominatim
|
.catch(()=>{
|
||||||
fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(name+' район')+'&countrycodes=kz&limit=20')
|
document.getElementById('locStatus').textContent='⚠ ошибка загрузки (введите село вручную)';
|
||||||
.then(r=>r.json()).then(data2=>{
|
|
||||||
if(!data2.length)return;
|
|
||||||
data2.forEach(item=>{
|
|
||||||
const nm=item.display_name.split(',')[0].trim();
|
|
||||||
const o=document.createElement('option');
|
|
||||||
o.value=nm;o.textContent=nm;
|
|
||||||
sel.appendChild(o);
|
|
||||||
});
|
|
||||||
document.getElementById('locStatus').textContent='✅ через Nominatim: '+data2.length;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function onDistrictSelect(){
|
function onDistrictSelect(){
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user