diff options
author | Daniel Friesel <derf@finalrewind.org> | 2021-06-26 19:55:26 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2021-06-26 19:55:26 +0200 |
commit | d21d35ef767007257e80a795a41b6f4cc8a9fcbc (patch) | |
tree | a5dfd5a1ed6bb823c3ef2054588d057d7cabf55d /static/v0/js | |
parent | 1cac79ba31858b496c03762a66eb86680ff3a3da (diff) |
add a landing page with geolocation search
Diffstat (limited to 'static/v0/js')
-rw-r--r-- | static/v0/js/geolocation.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/static/v0/js/geolocation.js b/static/v0/js/geolocation.js new file mode 100644 index 0000000..7efffe0 --- /dev/null +++ b/static/v0/js/geolocation.js @@ -0,0 +1,67 @@ +document.addEventListener("DOMContentLoaded", function() { + const geoLocationButton = document.getElementById('geolocationsearch'); + + const processResult = function(results) { + console.log(results); + const list = document.createElement("ul"); + for (var result in results) { + result = results[result]; + var listentry = document.createElement("li"); + var link = document.createElement("a"); + link.text = result.name + link.href = "/board/" + result.id; + listentry.appendChild(link) + list.appendChild(listentry) + } + geoLocationButton.replaceWith(list); + }; + + const processLocation = function(loc) { + fetch('/geolocation', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + lon: loc.coords.longitude, + lat: loc.coords.latitude + }) + }).then(response => response.json()).then(processResult); + }; + + const processError = function(error) { + if (error.code == error.PERMISSION_DENIED) { + showError('Standortanfrage nicht möglich.', 'Vermutlich fehlen die Rechte im Browser oder der Android Location Service ist deaktiviert.', 'geolocation.error.PERMISSION_DENIED'); + } else if (error.code == error.POSITION_UNAVAILABLE) { + showError('Standort konnte nicht ermittelt werden', 'Service nicht verfügbar', 'geolocation.error.POSITION_UNAVAILABLE'); + } else if (error.code == error.TIMEOUT) { + showError('Standort konnte nicht ermittelt werden', 'Timeout', 'geolocation.error.TIMEOUT'); + } else { + showError('Standort konnte nicht ermittelt werden', 'unbekannter Fehler', 'unknown geolocation.error code'); + } + }; + + const getGeoLocation = function() { + geoLocationButton.textContent = "Suche Haltestellen ..."; + geoLocationButton.disabled = true; + navigator.geolocation.getCurrentPosition(processLocation, processError); + } + + if (geoLocationButton) { + if (navigator.geolocation) { + if (navigator.permissions) { + navigator.permissions.query({ name:'geolocation' }).then(function(value) { + if (value.state === 'prompt') { + geoLocationButton.addEventListener('click', getGeoLocation); + } else { + getGeoLocation(); + } + }); + } else { + geoLocationButton.addEventListener('click', getGeoLocation); + } + } else { + geoLocationButton.css('visibility', 'hidden'); + } + } +}); |