From 7789f8202f60f69ce015b1bc34eb0c076d7545b5 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 24 May 2020 18:38:08 +0200 Subject: animate train position in map --- public/static/js/map-refresh.js | 81 +++++++++++++++++++++++++++++++++++++ public/static/js/map-refresh.min.js | 1 + 2 files changed, 82 insertions(+) create mode 100644 public/static/js/map-refresh.js create mode 100644 public/static/js/map-refresh.min.js (limited to 'public/static/js') diff --git a/public/static/js/map-refresh.js b/public/static/js/map-refresh.js new file mode 100644 index 0000000..b1729c3 --- /dev/null +++ b/public/static/js/map-refresh.js @@ -0,0 +1,81 @@ +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() { + $.get('/_ajax_mapinfo/' + j_reqid, 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); + } +}); diff --git a/public/static/js/map-refresh.min.js b/public/static/js/map-refresh.min.js new file mode 100644 index 0000000..802d376 --- /dev/null +++ b/public/static/js/map-refresh.min.js @@ -0,0 +1 @@ +function dbf_map_parse(){$("#jdata").each(function(){j_reqid=$(this).data("req");var a=$(this).data("poly");if(a){a=a.split("|"),j_positions=[];for(var e in a){var i=a[e].split(";");i[0]=parseFloat(i[0]),i[1]=parseFloat(i[1]),j_positions.push(i)}}})}function dbf_anim_coarse(){if(j_positions.length){var a=marker.getLatLng(),e=a.lat,i=a.lng,_=j_positions.shift(),t=_[0],r=_[1];j_frame_i=200,j_frame=[];for(var f=1;f<=60;f++){var n=f/60;j_frame.push([e+(t-e)*n,i+(r-i)*n])}j_frame_i=0}}function dbf_anim_fine(){j_frame[j_frame_i]&&marker.setLatLng(j_frame[j_frame_i++])}function dbf_map_reload(){$.get("/_ajax_mapinfo/"+j_reqid,function(a){$("#infobox").html(a),dbf_map_parse(),setTimeout(dbf_map_reload,61e3)}).fail(function(){setTimeout(dbf_map_reload,5e3)})}var j_reqid,j_positions=[],j_frame=[],j_frame_i=[];$(document).ready(function(){$("#infobox").length&&(dbf_map_parse(),setInterval(dbf_anim_coarse,2e3),setInterval(dbf_anim_fine,33),setTimeout(dbf_map_reload,61e3))}); -- cgit v1.2.3