forked from SerkanYildiz/open-iRail-map
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
115 lines (99 loc) · 4.6 KB
/
index.html
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
<!DOCTYPE html>
<!-- author Serkan Yildiz: MIT License-->
<html>
<head>
<title>Open iRail Map</title>
<link rel="stylesheet" href="assets/css/leaflet.css" media="screen" title="no title" charset="utf-8">
<script src="assets/js/leaflet.js" charset="utf-8"></script>
<script src="assets/js/moment.js" charset="utf-8"></script>
<script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,800' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="assets/css/style.css" media="screen" title="no title" charset="utf-8">
</head>
<body style="height:100%;width:100%;">
<div id="connections"></div>
<div id="map" style="width: 100%; height: 100%"></div>
<script>
setInterval(function () { location.reload() }, 60000);
/**
* Initializing leaflet map
*/
var mbAttr = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
mbUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; //+(L.Browser.retina?'@2x':'')+'.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw';
var grayscale = L.tileLayer(mbUrl, {id: 'mapbox.light', attribution: mbAttr}),
streets = L.tileLayer(mbUrl, {id: 'mapbox.streets', attribution: mbAttr});
var map = L.map('map', {
center: [50.8503, 4.3517],
zoom: 8,
layers: [grayscale]
});
// query log URL
var query_log = 'https://api.irail.be/logs';
var api_response = [];
var connections_count = 0;
var datetime = null;
function loadConnections()
{
// getting Json response from iRail api log
$.getJSON(
{
url: query_log,
success: function(data, status, jqr)
{
var serverDate = moment(jqr.getResponseHeader("expires")).subtract(1,"seconds");
if (!datetime) {
//get last second minus
datetime = moment(data[data.length-1].querytime).subtract(1,"seconds");
} else {
datetime = datetime.add(1,"seconds");
}
for(var log in data )
{
var query = data[log];
if(query != null && query.querytype !== null && query.querytype == 'connections' && query.code == null
&& moment(query.querytime).format("X") === datetime.format("X"))
{
connections_count++;
// departure information
var dep_id = query.query.departureStop['@id'];
var dep_name = query.query.departureStop.name;
var latFrom = query.query.departureStop.latitude;
var longFrom = query.query.departureStop.longitude;
// arrival information
var arr_id = query.query.arrivalStop['@id'];
var arr_name = query.query.arrivalStop.name;
var latTo = query.query.arrivalStop.latitude;
var longTo = query.query.arrivalStop.longitude;
var dateTime = query.query.dateTime;
console.log("Query at " + datetime.format() + " from " + dep_name + " to " + arr_name);
// Lat and Long information of departure and arrival
var pointFrom = new L.LatLng(latFrom, longFrom);
var pointTo = new L.LatLng(latTo, longTo);
var pointList = [pointFrom, pointTo];
var polyline = new L.Polyline(pointList,
{
color: 'blue',
weight: 3,
opacity: 0.3,
smoothFactor: 3
});
// add 'from' marker to map
//var fromMarker = new L.marker([latFrom, longFrom]).bindPopup(dep_name + ' at ' + moment(dateTime).format('LLL') + ' to ' + arr_name);
//fromMarker.addTo(map);
// add 'to' marker to map
//var toMarker = new L.marker([latTo,longTo]).bindPopup(arr_name);
//toMarker.addTo(map);
// add polyline to map (line between from - to)
polyline.addTo(map);
$("#connections").html(connections_count + ' searches');
}
}
}
});
}
window.setInterval(loadConnections,1000);
</script>
</body>
</html>