fixed and consolidated location querys
This commit is contained in:
1 parent
1701eb5070
commit
739a1eee28
4 files changed
+158
-141
No files matched your search
@@ -59,6 +59,7 @@
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<script src="{{ asset('js/location.js') }}"></script>
|
||||
<script src="{{ asset('js/nav.js') }}"></script>
|
||||
{% block scripts %}{% endblock %}
|
||||
</body>
|
||||
|
||||
@@ -133,27 +133,10 @@
|
||||
var currentLon = {{ hasLocation ? lon : 'null' }};
|
||||
var rangeSelect = document.getElementById('rangeSelect');
|
||||
|
||||
// Shared with the home hero map and /map (see map.js) so a search/
|
||||
// geolocation made on any of the three sticks when moving between them.
|
||||
var LAST_LOCATION_KEY = 'vindn:lastLocation';
|
||||
|
||||
function loadLastLocation() {
|
||||
try {
|
||||
var raw = window.localStorage && localStorage.getItem(LAST_LOCATION_KEY);
|
||||
if (!raw) return null;
|
||||
var parsed = JSON.parse(raw);
|
||||
if (typeof parsed.lat === 'number' && typeof parsed.lon === 'number' && isFinite(parsed.lat) && isFinite(parsed.lon)) {
|
||||
return parsed;
|
||||
}
|
||||
} catch (e) { /* localStorage disabled/unavailable, or corrupt value — just skip it */ }
|
||||
return null;
|
||||
}
|
||||
|
||||
function saveLastLocation(lat, lon, name) {
|
||||
try {
|
||||
if (window.localStorage) localStorage.setItem(LAST_LOCATION_KEY, JSON.stringify({ lat: lat, lon: lon, name: name || null }));
|
||||
} catch (e) { /* localStorage disabled/unavailable — nothing to persist to */ }
|
||||
}
|
||||
// Storage/search/GPS lookups live in window.VindnLocation (location.js,
|
||||
// loaded globally in base.html.twig) — shared with the home hero map and
|
||||
// /map, so a search/geolocation made on any of the three sticks when
|
||||
// moving between them.
|
||||
|
||||
function currentRangeKm() {
|
||||
return rangeSelect ? parseInt(rangeSelect.value, 10) : {{ rangeKm }};
|
||||
@@ -170,7 +153,7 @@
|
||||
var locInput = document.getElementById('locationSearchInput');
|
||||
name = locInput ? locInput.value : null;
|
||||
}
|
||||
saveLastLocation(lat, lon, name);
|
||||
window.VindnLocation.save(lat, lon, name);
|
||||
var url = new URL(window.location.href);
|
||||
url.searchParams.set('lat', lat.toFixed(5));
|
||||
url.searchParams.set('lon', lon.toFixed(5));
|
||||
@@ -186,8 +169,6 @@
|
||||
el.classList.toggle('is-error', !!isError);
|
||||
}
|
||||
|
||||
// Nominatim: explicit submit only, never as-you-type — required by its
|
||||
// usage policy (max ~1 req/s, no autocomplete-style querying).
|
||||
var locationForm = document.getElementById('locationSearchForm');
|
||||
if (locationForm) {
|
||||
locationForm.addEventListener('submit', function (e) {
|
||||
@@ -195,35 +176,24 @@
|
||||
var input = document.getElementById('locationSearchInput');
|
||||
var query = (input.value || '').trim();
|
||||
if (!query) return;
|
||||
setStatus('Looking up "' + query + '"…', false);
|
||||
fetch('https://nominatim.openstreetmap.org/search?format=json&limit=1&q=' + encodeURIComponent(query))
|
||||
.then(function (res) { return res.json(); })
|
||||
.then(function (results) {
|
||||
if (!results || !results.length) {
|
||||
setStatus('Could not find "' + query + '"', true);
|
||||
return;
|
||||
}
|
||||
navigateWithLocation(parseFloat(results[0].lat), parseFloat(results[0].lon), currentRangeKm(), results[0].display_name);
|
||||
})
|
||||
.catch(function () {
|
||||
setStatus('Location search failed', true);
|
||||
});
|
||||
window.VindnLocation.searchPlace(query, {
|
||||
onStart: function () { setStatus('Looking up "' + query + '"…', false); },
|
||||
onNotFound: function () { setStatus('Could not find "' + query + '"', true); },
|
||||
onFound: function (lat, lon, name) { navigateWithLocation(lat, lon, currentRangeKm(), name); },
|
||||
onError: function () { setStatus('Location search failed', true); }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var useMyLocationBtn = document.getElementById('useMyLocationBtn');
|
||||
if (useMyLocationBtn) {
|
||||
useMyLocationBtn.addEventListener('click', function () {
|
||||
if (!navigator.geolocation) {
|
||||
setStatus('Location not supported by your browser', true);
|
||||
return;
|
||||
}
|
||||
setStatus('Finding you…', false);
|
||||
navigator.geolocation.getCurrentPosition(function (pos) {
|
||||
navigateWithLocation(pos.coords.latitude, pos.coords.longitude, currentRangeKm(), 'Your location');
|
||||
}, function () {
|
||||
setStatus("Couldn't get your location", true);
|
||||
}, { timeout: 8000 });
|
||||
window.VindnLocation.useMyLocation({
|
||||
onUnsupported: function () { setStatus('Location not supported by your browser', true); },
|
||||
onStart: function () { setStatus('Finding you…', false); },
|
||||
onFound: function (lat, lon, name) { navigateWithLocation(lat, lon, currentRangeKm(), name); },
|
||||
onError: function () { setStatus("Couldn't get your location", true); }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -240,9 +210,7 @@
|
||||
if (clearLocationLink) {
|
||||
// Otherwise the restore-on-load check below would immediately undo
|
||||
// the clear on the very next visit.
|
||||
clearLocationLink.addEventListener('click', function () {
|
||||
try { window.localStorage && localStorage.removeItem(LAST_LOCATION_KEY); } catch (e) {}
|
||||
});
|
||||
clearLocationLink.addEventListener('click', function () { window.VindnLocation.clear(); });
|
||||
}
|
||||
|
||||
var sortSelect = document.getElementById('sortSelect');
|
||||
@@ -253,7 +221,7 @@
|
||||
});
|
||||
}
|
||||
|
||||
var saved = loadLastLocation();
|
||||
var saved = window.VindnLocation.load();
|
||||
|
||||
// Every full reload here (search, geolocation, range change) already
|
||||
// lands with an explicit ?lat=, so this doesn't overwrite it — it only
|
||||
|
||||
Reference in new issue
Block a user