summaryrefslogtreecommitdiff
path: root/public/static/js/map-refresh.js
blob: 1d8453a2c30b5d959a67597bc47b5f6bdb0bd580 (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
/*
 * Copyright (C) 2020 Birte Kristina Friesel
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

var j_reqid;
//var j_stops = [];
var j_positions = [];
var j_frame = [];
var j_frame_i = [];

function dbf_map_parse() {
	$('#jdata').each(function() {
		j_reqid = $(this).data('req');
		/*var route_data = $(this).data('route');
		if (route_data) {
			route_data = route_data.split('|');
			j_stops = [];
			for (var stop_id in route_data) {
				var stopdata = route_data[stop_id].split(';');
				for (var i = 1; i < 5; i++) {
					stopdata[i] = parseInt(stopdata[i]);
				}
				j_stops.push(stopdata);
			}
		}*/
		var positions = $(this).data('poly');
		if (positions) {
			positions = positions.split('|');
			j_positions = [];
			for (var pos_id in positions) {
				var posdata = positions[pos_id].split(';');
				posdata[0] = parseFloat(posdata[0]);
				posdata[1] = parseFloat(posdata[1]);
				j_positions.push(posdata);
			}
		}
	});
}

function dbf_anim_coarse() {
	if (j_positions.length) {
		var pos1 = marker.getLatLng();
		var pos1lat = pos1.lat;
		var pos1lon = pos1.lng;
		var pos2 = j_positions.shift();
		var pos2lat = pos2[0];
		var pos2lon = pos2[1];

		j_frame_i = 200;
		j_frame = [];

		// approx 30 Hz -> 60 frames per 2 seconds
		for (var i = 1; i <= 60; i++) {
			var ratio = i / 60;
			j_frame.push([pos1lat + ((pos2lat - pos1lat) * ratio), pos1lon + ((pos2lon - pos1lon) * ratio)]);
		}

		j_frame_i = 0;
	}
}

function dbf_anim_fine() {
	if (j_frame[j_frame_i]) {
		marker.setLatLng(j_frame[j_frame_i++]);
	}
}

function dbf_map_reload() {
	const param = new URLSearchParams(window.location.search);
	$.get('/_ajax_mapinfo/' + j_reqid + '?efa=' + param.get('efa') + '&amp;hafas=' + param.get('hafas'), function(data) {
		$('#infobox').html(data);
		dbf_map_parse();
		setTimeout(dbf_map_reload, 61000);
	}).fail(function() {
		setTimeout(dbf_map_reload, 5000);
	});
}

$(document).ready(function() {
	if ($('#infobox').length) {
		dbf_map_parse();
		setInterval(dbf_anim_coarse, 2000);
		setInterval(dbf_anim_fine, 33);
		setTimeout(dbf_map_reload, 61000);
	}
});