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;}
|
||||
map.setView(r.center,9);
|
||||
document.getElementById('locStatus').textContent='⏳ районы...';
|
||||
// Overpass: ищем админ-границы 6-8 уровня внутри области
|
||||
const q='[out:json];(area["name"="'+name+' область"]["admin_level"~"[46]"];)->.a;(rel(area.a)["admin_level"~"[678]"];);out tags;';
|
||||
fetch('https://overpass-api.de/api/interpreter?data='+encodeURIComponent(q))
|
||||
.then(r=>r.json())
|
||||
.then(data=>{
|
||||
if(!data||!data.elements){document.getElementById('locStatus').textContent='нет данных';return;}
|
||||
// Ищем область в Nominatim, получаем bounding box, ищем районы внутри
|
||||
const searchName=name+' область';
|
||||
fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(searchName)+'&countrycodes=kz&limit=1')
|
||||
.then(r=>r.json()).then(reg=>{
|
||||
if(!reg.length)return fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(name)+'&countrycodes=kz&limit=1').then(r=>r.json());
|
||||
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();
|
||||
data.elements.forEach(el=>{
|
||||
if(!el.tags||!el.tags.name)return;
|
||||
let n=el.tags.name.replace(/\s*ауданы$/,'').replace(/\s*районы$/,'').replace(/\s*район$/,'').trim();
|
||||
data.forEach(item=>{
|
||||
if(item.type!='administrative')return;
|
||||
const n=item.display_name.split(',')[0].trim();
|
||||
// отсекаем лишнее
|
||||
if(n.toLowerCase().includes('город')||n===name)return;
|
||||
if(n&&!names.has(n))names.add(n);
|
||||
});
|
||||
const sorted=[...names].sort();
|
||||
sorted.forEach(n=>{
|
||||
const o=document.createElement('option');
|
||||
o.value=n;
|
||||
o.textContent=n;
|
||||
o.value=n;o.textContent=n;
|
||||
sel.appendChild(o);
|
||||
});
|
||||
document.getElementById('locStatus').textContent=sorted.length?'✅ районы: '+sorted.length: '⚠ районы не найдены';
|
||||
if(sorted.length===0){
|
||||
// fallback: поищем через Nominatim
|
||||
fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(name+' район')+'&countrycodes=kz&limit=20')
|
||||
.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;
|
||||
});
|
||||
}
|
||||
document.getElementById('locStatus').textContent=sorted.length?'✅ '+sorted.length+' районов':'⚠ районы не найдены (введите село вручную)';
|
||||
})
|
||||
.catch(()=>{
|
||||
document.getElementById('locStatus').textContent='⚠ ошибка загрузки (введите село вручную)';
|
||||
});
|
||||
}
|
||||
function onDistrictSelect(){
|
||||
|
||||
Loading…
Reference in New Issue
Block a user