summaryrefslogtreecommitdiff
path: root/static/v0/js/geolocation.js
blob: 4e868b0226263c88f6c4ab62e710e31e7c4fb0f5 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
document.addEventListener("DOMContentLoaded", function() {
	const geoLocationButton = document.getElementById('geolocationsearch');

	const mkTextNode = function(className, textContent) {
		const node = document.createElement("span");
		node.className = className;
		node.textContent = textContent;
		return node
	}

	const processResult = function(results) {
		const list = document.createElement("ul");
		list.className = "stops";
		for (var result in results) {
			result = results[result];
			const listentry = document.createElement("li");
			const link = document.createElement("a");
			const note = document.createElement("span");
			const lines = document.createElement("span");

			link.className = "name";
			link.textContent = result.name;
			link.href = "/board/" + result.id;

			note.className = "note";
			if (result.distance >= 1000) {
				note.textContent = result.distance.toFixed(1) + " km"
			} else {
				note.textContent = result.distance + " m"
			}

			lines.className = "lines";

			if (result.products.nationalExpress) {
				lines.appendChild(mkTextNode("longdistance", "ICE"));
			}
			if (result.products.national) {
				lines.appendChild(mkTextNode("longdistance", "IC"));
			}
			if (result.products.regionalExp) {
				lines.appendChild(mkTextNode("longdistance", "RE"));
			}
			if (result.products.regional) {
				lines.appendChild(mkTextNode("longdistance", "R"));
			}
			if (result.products.suburban) {
				lines.appendChild(mkTextNode("suburban", "S"));
			}
			if (result.products.tram) {
				lines.appendChild(mkTextNode("tram", "T"));
			}
			if (result.products.bus) {
				lines.appendChild(mkTextNode("bus", "Bus"));
			}
			if (result.products.ferry) {
				lines.appendChild(mkTextNode("bus", "Fähre"));
			}
			if (result.products.taxi) {
				lines.appendChild(mkTextNode("taxi", "AST"));
			}

			listentry.appendChild(link);
			listentry.appendChild(note);
			listentry.appendChild(lines);
			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');
		}
	}
});