v7: working region-district-village search via Nominatim

This commit is contained in:
Dauren777 2026-06-19 06:25:30 +00:00
parent 08a02c6fda
commit 0606f947b1

View File

@ -63,14 +63,15 @@ body{font:14px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Inter,sans-serif;
</div>
</div>
<div class="loc-panel">
<label>📍 Регион:</label>
<label>📍 Область:</label>
<select id="regionSelect" onchange="onRegionChange()"><option value="">— выберите —</option></select>
<label>Район:</label>
<select id="districtSelect" onchange="onDistrictChange()"><option value="">— выберите</option></select>
<label>Нас. пункт:</label>
<input type="text" id="villageInput" placeholder="Название села...">
<select id="districtSelect" onchange="onDistrictSelect()"><option value="">— все</option></select>
<label>Село:</label>
<input type="text" id="villageInput" placeholder="Название села..." style="width:100px">
<button onclick="searchLocation()">🔍 Найти</button>
<button onclick="autoDetect()" style="background:#FF9800;color:#fff">🔄 Авто</button>
<span id="locStatus" style="color:#888;font-size:9px"></span>
</div>
<div class="settings">
<label>📐 Шаг: <input type="number" id="poleStep" value="50" min="10" max="200"> м</label>
@ -156,18 +157,57 @@ function init(){
function onRegionChange(){
const name=document.getElementById('regionSelect').value;
const r=REGIONS.find(x=>x.name===name);
if(r)map.setView(r.center,9);
if(!r){document.getElementById('districtSelect').innerHTML='<option value="">— все —</option>';return;}
map.setView(r.center,9);
document.getElementById('locStatus').textContent='⏳ загрузка районов...';
// Загружаем районы области через Nominatim
fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent('районы '+name+' области')+'&countrycodes=kz&limit=30')
.then(r=>r.json()).then(data=>{
const sel=document.getElementById('districtSelect');
const names=new Set();
sel.innerHTML='<option value="">— все —</option>';
data.forEach(item=>{
const match=item.display_name.match(/^([^,]+),\s*([^,]+)/);
if(match){
let dname=match[1].trim();
if(!dname.endsWith('район')&&!dname.endsWith('ауданы'))return;
dname=dname.replace(/\s*ауданы$/,'').replace(/\s*район$/,'');
if(!names.has(dname)){
names.add(dname);
const o=document.createElement('option');
o.value=dname;
o.textContent=dname+(dname.endsWith('ский')||dname.endsWith('ской')?' район':'');
sel.appendChild(o);
}
function onDistrictChange(){
if(document.getElementById('districtSelect').value)searchLocation();
}
});
document.getElementById('locStatus').textContent='';
if(names.size===0)document.getElementById('locStatus').textContent='районы не найдены';
});
}
function onDistrictSelect(){
searchLocation();
}
function searchLocation(){
const q=[document.getElementById('regionSelect').value,document.getElementById('districtSelect').value,document.getElementById('villageInput').value].filter(Boolean).join(', ');
if(!q){showToast('Выберите регион или введите название');return;}
fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(q)+'&countrycodes=kz&limit=5')
const parts=[document.getElementById('regionSelect').value];
const d=document.getElementById('districtSelect').value;
const v=document.getElementById('villageInput').value.trim();
if(d)parts.push(d+(d.endsWith('ский')||d.endsWith('ской')?' район':''));
if(v)parts.push(v);
const q=parts.join(', ');
if(!q){showToast('Выберите область или введите село');return;}
document.getElementById('locStatus').textContent='⏳ поиск...';
fetch('https://nominatim.openstreetmap.org/search?format=json&q='+encodeURIComponent(q)+'&countrycodes=kz&limit=8')
.then(r=>r.json()).then(data=>{
if(!data.length){showToast('Не найдено');return;}
map.setView([data[0].lat,data[0].lon],16);showToast('📍 '+data[0].display_name.split(',')[0]);
if(!data.length){showToast('❌ Не найдено: '+q);document.getElementById('locStatus').textContent='не найдено';return;}
const loc=data[0];
map.setView([loc.lat,loc.lon],16);
const name=loc.display_name.split(',')[0];
showToast('📍 '+name);
document.getElementById('villageInput').value=name;
document.getElementById('locStatus').textContent=name;
// Автозагрузка OSM данных
setTimeout(autoDetect,500);
});
}