summaryrefslogtreecommitdiff
path: root/public/static/geolocation.js
blob: 1d37c96cebacb47489aee61c19e7c9f7d547efe6 (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
$(document).ready(function() {
	var processResult = function(data) {
		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);
	};

	var processError = function(error) {
		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);
	} else {
		$('div.candidatelist').text('Geolocation is not supported by your browser');
	}
});