travelynx/public/static/js/geolocation.js

89 lines
3 KiB
JavaScript
Raw Normal View History

2020-11-27 21:12:56 +00:00
/*
* Copyright (C) 2020 Daniel Friesel
*
* SPDX-License-Identifier: MIT
*/
2018-10-07 09:35:47 +00:00
$(document).ready(function() {
2019-05-11 10:43:04 +00:00
function getPlaceholder() {
return $('div.geolocation div.progress');
}
2018-10-07 09:35:47 +00:00
var showError = function(header, message, code) {
2019-05-11 10:43:04 +00:00
getPlaceholder().remove();
2019-03-08 18:40:57 +00:00
var errnode = $(document.createElement('div'));
errnode.attr('class', 'error');
errnode.text(message);
2019-03-08 18:40:57 +00:00
var headnode = $(document.createElement('strong'));
headnode.text(header);
errnode.prepend(headnode);
$('div.geolocation').append(errnode);
2018-10-07 09:35:47 +00:00
};
var processResult = function(data) {
if (data.error) {
showError('Backend-Fehler:', data.error, null);
} else if (data.candidates.length == 0) {
showError('Keine Bahnhöfe in 70km Umkreis gefunden', '', null);
} else {
resultTable = $('<table><tbody></tbody></table>')
resultBody = resultTable.children();
$.each(data.candidates, function(i, candidate) {
var ds100 = candidate.ds100,
name = candidate.name,
distance = candidate.distance;
distance = distance.toFixed(1);
var stationlink = $(document.createElement('a'));
stationlink.attr('href', ds100);
stationlink.text(name);
2019-03-07 17:36:11 +00:00
resultBody.append('<tr><td><a href="/s/' + ds100 + '">' + name + '</a></td></tr>');
2018-10-07 09:35:47 +00:00
});
2019-05-11 10:43:04 +00:00
getPlaceholder().replaceWith(resultTable);
2018-10-07 09:35:47 +00:00
}
};
var processLocation = function(loc) {
2019-03-07 17:36:11 +00:00
$.post('/geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude}, processResult);
2018-10-07 09:35:47 +00:00
};
var 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');
}
};
2019-05-11 10:43:04 +00:00
var geoLocationButton = $('div.geolocation > button');
var getGeoLocation = function() {
geoLocationButton.replaceWith($('<p class="geolocationhint">Stationen in der Umgebung:</p><div class="progress"><div class="indeterminate"></div></div>'));
navigator.geolocation.getCurrentPosition(processLocation, processError);
}
if (geoLocationButton.length) {
if (navigator.geolocation) {
2019-05-11 10:43:04 +00:00
if (navigator.permissions) {
navigator.permissions.query({ name:'geolocation' }).then(function(value) {
if (value.state === 'prompt') {
geoLocationButton.on('click', getGeoLocation);
} else {
// User either rejected or granted permission. User wont get prompted and we can show stations/error
getGeoLocation();
}
});
} else {
geoLocationButton.on('click', getGeoLocation);
}
} else {
showError('Standortanfragen werden von diesem Browser nicht unterstützt', '', null);
}
2018-10-07 09:35:47 +00:00
}
});