Fix geolocation: button-triggered, error messages in Russian

This commit is contained in:
root 2026-08-20 18:45:42 +00:00
parent 31c53cd8d5
commit 7182e6828b

View File

@ -21,6 +21,8 @@ const MapView = {
<span class="slider"></span> <span class="slider"></span>
</label> </label>
</div> </div>
<button class="btn" id="loc-share-btn" style="margin-top:8px;">📍 Поделиться геолокацией</button>
<div class="privacy-note" id="loc-status">Нажми чтобы получить координаты</div>
</div> </div>
<div id="map" style="height:400px;border-radius:12px;overflow:hidden;margin-bottom:12px;"></div> <div id="map" style="height:400px;border-radius:12px;overflow:hidden;margin-bottom:12px;"></div>
<div class="row"> <div class="row">
@ -63,44 +65,88 @@ const MapView = {
this.refreshNearby(); this.refreshNearby();
}); });
this.refreshLocation(); document.getElementById('loc-share-btn').onclick = (e) => {
e.preventDefault();
this.requestLocation();
};
// Если уже есть сохранённые координаты — покажем на карте
if (window._lastKnownPos) {
this.applyPosition(window._lastKnownPos);
}
}, },
async refreshLocation() { requestLocation() {
if (!navigator.geolocation) { if (!navigator.geolocation) {
document.getElementById('nearby-count').textContent = 'Геолокация недоступна'; const s = document.getElementById('loc-status');
if (s) s.textContent = 'Геолокация недоступна в браузере';
return; return;
} }
const btn = document.getElementById('loc-share-btn');
const status = document.getElementById('loc-status');
if (btn) { btn.disabled = true; btn.textContent = '⏳ Запрашиваю…'; }
if (status) status.textContent = 'Браузер спросит разрешение…';
navigator.geolocation.getCurrentPosition( navigator.geolocation.getCurrentPosition(
async (pos) => { async (pos) => {
window._lastKnownPos = pos;
if (btn) { btn.disabled = false; btn.textContent = '📍 Обновить геолокацию'; }
if (status) status.textContent = `${pos.coords.latitude.toFixed(4)}, ${pos.coords.longitude.toFixed(4)}`;
await this.applyPosition(pos);
},
(err) => {
if (btn) { btn.disabled = false; btn.textContent = '📍 Попробовать снова'; }
if (status) status.textContent = '⚠️ ' + this.geolocationErrorText(err);
console.warn('geolocation error:', err);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 60000 }
);
},
geolocationErrorText(err) {
switch (err.code) {
case err.PERMISSION_DENIED: return 'Доступ к геолокации запрещён. Разреши в настройках браузера.';
case err.POSITION_UNAVAILABLE: return 'Не удалось определить координаты (нет GPS/Wi-Fi).';
case err.TIMEOUT: return 'Таймаут запроса геолокации.';
default: return 'Неизвестная ошибка геолокации.';
}
},
async applyPosition(pos) {
const lat = pos.coords.latitude; const lat = pos.coords.latitude;
const lng = pos.coords.longitude; const lng = pos.coords.longitude;
try { try {
await API.Auth.updateLocation(lat, lng, this.visible); await API.Auth.updateLocation(lat, lng, this.visible);
} catch (e) { } catch (e) {
console.warn(e); console.warn('updateLocation:', e);
} }
if (this.map) {
this.map.setView([lat, lng], 13); this.map.setView([lat, lng], 13);
// удалим старый круг
this.map.eachLayer((layer) => {
if (layer instanceof L.Circle) this.map.removeLayer(layer);
});
L.circle([lat, lng], { L.circle([lat, lng], {
radius: this.radius * 1000, radius: this.radius * 1000,
color: '#FF6B35', fillColor: '#FF6B35', fillOpacity: 0.1, weight: 2, color: '#FF6B35', fillColor: '#FF6B35', fillOpacity: 0.1, weight: 2,
}).addTo(this.map); }).addTo(this.map);
this.refreshNearby(); this.refreshNearbyFromPos(lat, lng);
}, }
(err) => {
document.getElementById('nearby-count').textContent = 'Нет доступа к GPS: ' + err.message;
},
{ enableHighAccuracy: true, timeout: 15000 }
);
}, },
async refreshNearby() { async refreshNearby() {
if (!navigator.geolocation) return; if (!navigator.geolocation) return;
navigator.geolocation.getCurrentPosition(async (pos) => { navigator.geolocation.getCurrentPosition(async (pos) => {
this.refreshNearbyFromPos(pos.coords.latitude, pos.coords.longitude);
});
},
async refreshNearbyFromPos(lat, lng) {
try { try {
const r = await API.Auth.searchNearby(pos.coords.latitude, pos.coords.longitude, this.radius); const r = await API.Auth.searchNearby(lat, lng, this.radius);
this.nearby = r.results || []; this.nearby = r.results || [];
document.getElementById('nearby-count').textContent = this.nearby.length + ' чел. рядом'; const c = document.getElementById('nearby-count');
if (c) c.textContent = this.nearby.length + ' чел. рядом';
// удалить старые маркеры (кроме своего круга) // удалить старые маркеры (кроме своего круга)
this.markers.forEach(m => this.map.removeLayer(m)); this.markers.forEach(m => this.map.removeLayer(m));
@ -120,7 +166,6 @@ const MapView = {
} catch (e) { } catch (e) {
console.warn(e); console.warn(e);
} }
});
}, },
}; };