summaryrefslogtreecommitdiff
path: root/public/static/geolocation.js
blob: ccf03252df4b67943e4bf1025cb14bcaf23ccb2e (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
$(document).ready(function() {
	var removeStatus = function() {
		$('div.candidatestatus').remove();
	};

	var processResult = function(data) {
		removeStatus();
		if (data.error) {
			$('div.candidatelist').text(data.error);
		} else if (data.candidates.length == 0) {
			$('div.candidatelist').text("Keine Bahnhöfe in 70km Umkreis gefunden");
		} else {
			$.each(data.candidates, function(i, candidate) {

				var ds100 = candidate[0][0],
					name = candidate[0][1],
					distance = candidate[1];
				distance = distance.toFixed(1);

				var stationlink = $(document.createElement('a'));
				stationlink.attr('href', ds100);
				stationlink.text(name);
				$('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) {
			$('div.candidatelist').text('Geolocation request denied');
		} else if (error.code == error.POSITION_UNAVAILABLE) {
			$('div.candidatelist').text('Geolocation not available');
		} else if (error.code == error.TIMEOUT) {
			$('div.candidatelist').text('Geolocation timeout');
		} else {
			$('div.candidatelist').text('Unknown error');
		}
	};

	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(processLocation, processError);
		$('div.candidatestatus').text('Position wird bestimmt…');
	} else {
		removeStatus();
		$('div.candidatelist').text('Geolocation is not supported by your browser');
	}
});