Projekt

Obecné

Profil

Stáhnout (5.76 KB) Statistiky
| Větev: | Revize:
1
var mymap;
2
var heatmapLayer = null;
3

    
4
var startX = 49.7248;
5
var startY = 13.3521;
6
var startZoom = 17;
7

    
8
var dataSourceRoute;
9
var currentTime;
10
var name;
11
var date;
12

    
13
var timer;
14
var isAnimationRunning = false;
15
var data = [];
16

    
17

    
18

    
19
function initMap() {
20
  mymap = L.map('heatmap').setView([startX, startY], startZoom);
21

    
22
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
23
    attribution: '',
24
    maxZoom: 19
25
  }).addTo(mymap);
26

    
27
  mymap.on('click', showInfo);
28

    
29
}
30

    
31
var info = [];
32
var currenInfo = 0;
33
var infoLength = 0;
34

    
35
function showInfo(e) {
36

    
37
  info = []
38
  currenInfo = 0;
39

    
40
  // https://wiki.openstreetmap.org/wiki/Zoom_levels
41
  // Todo change to variable - it is used in heatmap init
42
  var stile =  40075016.686 * Math.cos(startX) / Math.pow(2, mymap.getZoom()) ;
43
  var radius = 25 * stile / 256;
44

    
45
  var i = 0;
46
  var lat = 0;
47
  var lng = 0;
48

    
49
  data[currentTime]['items'].forEach(element => {
50
    if (e.latlng.distanceTo(new L.LatLng(element.x, element.y)) < radius) {
51
      lat += element.x
52
      lng += element.y;
53
      info[i] = {'place' : element.place, 'number' : element.number};
54
      i++;
55
    }
56
  });
57

    
58
  
59
  if (info.length > 0) {
60
    var popup = L.popup({
61
      autoPan: false
62
                  })
63
                  .setLatLng([lat / i, lng / i])
64
                  .setContent('<strong>Zařízení a počet:</strong><div id=\'place-info\'>' + info[currenInfo].place + '</div>' +
65
                              '<div id=\'number-info\'>' + info[currenInfo].number + '</div>' +
66
                              '<div class=\'popup-controls\'>' +
67
                                '<button id=\'previous-info-btn\' class=\'circle-button\' onclick=\'previousInfo()\'></button>' +
68
                                '<div id=\'count-info\'>' + (currenInfo + 1) + '/' + info.length + '</div>' +
69
                                '<button id=\'next-info-btn\' onclick=\'nextInfo()\' class=\'circle-button next\'></button>' +
70
                              '</div>'
71
                              )
72
                  .openOn(mymap);
73
    
74
    if (info.length == 1) {
75
      $('#previous-info-btn').prop('disabled', true);
76
      $('#next-info-btn').prop('disabled', true);
77
      $('.popup-controls').hide();
78
    }
79
  }
80

    
81
}
82

    
83
function previousInfo() {
84
  currenInfo = (currenInfo + info.length - 1) % info.length;
85
  displayInfoText();
86
}
87

    
88
function nextInfo() {
89
  currenInfo = (currenInfo + 1) % info.length
90
  displayInfoText();
91
}
92

    
93
function displayInfoText() {
94
  $('#place-info').html(info[currenInfo].place)
95
  $('#number-info').html(info[currenInfo].number)
96
  $('#count-info').html(currenInfo + 1 + '/' + info.length);
97
}
98

    
99
function setMapView(latitude = startX, longitude = startY, zoom = startZoom) {
100
  mymap.setView([latitude, longitude], zoom);
101
}
102

    
103
function changeAnimationState() {
104

    
105
  isAnimationRunning = !isAnimationRunning
106
  if (isAnimationRunning) {
107
    $('#play-pause').attr('class', 'pause');
108
    timer = setInterval(
109
      function() {
110
        mymap.closePopup();
111
        next();
112
      },
113
      800
114
    );
115
  }
116
  else {
117
    clearTimeout(timer);
118
    $('#play-pause').attr('class', 'play');
119
  }
120

    
121

    
122
}
123

    
124
function previous() {
125
  currentTime = (currentTime + 23) % 24;
126
  drawHeatmap(data[currentTime]);
127
  setTimeline();
128
  mymap.closePopup();
129
  updateHeaderControls();
130
}
131

    
132
function next() {
133
  currentTime = (currentTime + 1) % 24;
134
  drawHeatmap(data[currentTime]);
135
  setTimeline();
136
  mymap.closePopup();
137
  updateHeaderControls();
138
}
139

    
140
function updateHeaderControls() {
141
  document.getElementById('time').value = currentTime;
142
}
143

    
144
function setTimeline() {
145
  $('#timeline').text(currentTime + ":00");
146
  $('#timeline').attr('class', 'time hour-' + currentTime);
147
}
148

    
149
function loadCurrentTimeHeatmap(route) {
150

    
151
  dataSourceRoute = route;
152
  data = []
153

    
154
  name = $('#type').children("option:selected").text();
155
  var parts = $('#date').val().split('-');
156
  date = parts[2] + parts[1] + parts[0];
157
  currentTime = parseInt($('#time').children("option:selected").val());
158
  setTimeline();
159

    
160
  $.ajax({
161
    type: "POST",
162
    url: dataSourceRoute + '/' +  name + '/' + date + '/' + currentTime,
163
    success: function(result) {
164
      data[currentTime] = result;
165
      drawHeatmap(data[currentTime]);
166
    }
167
  });
168

    
169
  preload(currentTime, 1);
170
  preload(currentTime, -1);
171

    
172
}
173

    
174
function preload(time, change) {
175

    
176
  var ntime = time + change; 
177
  if (0 <=  ntime && ntime <= 23) {
178
      $.ajax({
179
        type: "POST",
180
        url: dataSourceRoute + '/' +  name + '/' + date + '/' + ntime,
181
        success: function(result) {
182
          data[ntime] = result;
183
          preload(ntime, change);
184
        }
185
      });
186
  }
187

    
188
}
189

    
190

    
191
function drawHeatmap(data) {
192

    
193
  // Todo still switched
194
  if (data['items'] != null) {
195
    points = data['items'].map(function (p) { return [p['x'], p['y'], p['number']]; });
196
    if (heatmapLayer != null) {
197
      mymap.removeLayer(heatmapLayer);
198
    }
199
    heatmapLayer = L.heatLayer(points, {'max' : data['max'], 'minOpacity' : 0.5, 'radius' : 35, 'blur' : 30}).addTo(mymap);
200
  }
201
  else {
202
    if (heatmapLayer != null) {
203
      mymap.removeLayer(heatmapLayer);
204
    }
205
  }
206

    
207
  // var heat_01 = ...
208
  // on background map.addLayer(heat_01) -> map.removeLayer(heat_01);
209
  // $(.leaflet-heatmap-layer).css('opacity', 'value');
210
}
211

    
212
function checkDataSetsAvailability(route) {
213

    
214
  var parts = $('#date').val().split('-');
215
  
216
  $.ajax({
217
    type: "POST",
218
    // Todo it might be good idea to change db collections format
219
    url: route + '/' + parts[2] + parts[1] + parts[0],
220
    success: function(result) {
221
      updateAvailableDataSets(result);
222
    }
223
  });
224
}
225

    
226
function updateAvailableDataSets(available) {
227
  
228
  var options = '';
229

    
230
  Object.entries(available).forEach(([key, value]) => {
231
    options += '<option value="' + value + '">' + key + '</option>\n'; 
232
  });
233

    
234
  if (options === '') {
235
    $('#submit-btn').prop('disabled', true);
236
  }
237
  else {
238
    $('#submit-btn').prop('disabled', false);
239
  }
240

    
241
  $('#type').empty().append(options);
242
  
243
}
(6-6/6)