summaryrefslogtreecommitdiff
path: root/public/static/geolocation.js
blob: 63b42b8abab8b48a1e161d481fc40e84ae400edf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
$(document).ready(function() {
	var removeStatus = function() {
		$('div.candidatestatus').remove();
	};
	var showError = function(oneline, str) {
		var errnode = $(document.createElement('div'));
		errnode.attr('class', 'error');
		errnode.text(str);

		if (oneline) {
			var shortnode = $(document.createElement('div'));
			shortnode.attr('class', 'errshort');
			shortnode.text(oneline);
			errnode.append(shortnode);
		}

		$('div.candidatelist').append(errnode);
	};

	var processResult = function(data) {
		removeStatus();
		if (data.error) {
			showError('Backend-Fehler', data.error);
		} else if (data.candidates.length == 0) {
			showError(null, 'Keine Bahnhöfe in 70km Umkreis gefunden');
		} else {
			$.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);

				var distancenode = $(document.createElement('div'));
				distancenode.attr('class', 'distance');
				distancenode.text(distance);

				stationlink.append(distancenode);
				$('div.candidatelist').append(stationlink);
			});
		}
	};

	var processLocation = function(loc) {
		$.post('/_geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude}, processResult);
		$('div.candidatestatus').text('Suche Bahnhöfe…');
	};

	var processError = function(error) {
		removeStatus();
		if (error.code == error.PERMISSION_DENIED) {
			showError('geolocation.error.PERMISSION_DENIED', 'Standortanfrage nicht möglich. Vermutlich fehlen die Rechte im Browser oder der Android Location Service ist deaktiviert.');
		} else if (error.code == error.POSITION_UNAVAILABLE) {
			showError('geolocation.error.POSITION_UNAVAILABLE', 'Standort konnte nicht ermittelt werden (Service nicht verfügbar)');
		} else if (error.code == error.TIMEOUT) {
			showError('geolocation.error.TIMEOUT', 'Standort konnte nicht ermittelt werden (Timeout)');
		} else {
			showError('unknown geolocatior.error code', 'Standort konnte nicht ermittelt werden (unbekannter Fehler)');
		}
	};

	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(processLocation, processError);
		$('div.candidatestatus').text('Position wird bestimmt…');
	} else {
		removeStatus();
		showError(null, 'Standortanfragen werden von diesem Browser nicht unterstützt');
	}
});