Projekt

Obecné

Profil

Stáhnout (6.01 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
        next();
111
      },
112
      800
113
    );
114
  }
115
  else {
116
    clearTimeout(timer);
117
    $('#play-pause').attr('class', 'play');
118
  }
119

    
120

    
121
}
122

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

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

    
141
function changeUrl() {
142
  window.history.pushState(
143
    "",
144
    document.title,
145
    window.location.origin + window.location.pathname + `?data_set[date]=${$('#date').val()}&data_set[time]=${currentTime}&data_set[type]=${$('#type').children("option:selected").val()}`
146
  );
147
}
148

    
149
function updateHeaderControls() {
150
  document.getElementById('time').value = currentTime;
151
}
152

    
153
function setTimeline() {
154
  $('#timeline').text(currentTime + ":00");
155
  $('#timeline').attr('class', 'time hour-' + currentTime);
156
}
157

    
158
function loadCurrentTimeHeatmap(route) {
159

    
160
  dataSourceRoute = route;
161
  data = []
162

    
163
  name = $('#type').children("option:selected").text();
164
  date = $('#date').val()
165
  currentTime = parseInt($('#time').children("option:selected").val());
166
  setTimeline();
167

    
168
  $.ajax({
169
    type: "POST",
170
    url: dataSourceRoute + '/' +  name + '/' + date + '/' + currentTime,
171
    success: function(result) {
172
      data[currentTime] = result;
173
      drawHeatmap(data[currentTime]);
174
    }
175
  });
176

    
177
  preload(currentTime, 1);
178
  preload(currentTime, -1);
179

    
180
}
181

    
182
function preload(time, change) {
183

    
184
  var ntime = time + change; 
185
  if (0 <=  ntime && ntime <= 23) {
186
      $.ajax({
187
        type: "POST",
188
        url: dataSourceRoute + '/' +  name + '/' + date + '/' + ntime,
189
        success: function(result) {
190
          data[ntime] = result;
191
          preload(ntime, change);
192
        }
193
      });
194
  }
195

    
196
}
197

    
198

    
199
function drawHeatmap(data) {
200

    
201
  // Todo still switched
202
  if (data['items'] != null) {
203
    points = data['items'].map(function (p) { return [p['x'], p['y'], p['number']]; });
204
    if (heatmapLayer != null) {
205
      mymap.removeLayer(heatmapLayer);
206
    }
207
    heatmapLayer = L.heatLayer(points, {'max' : data['max'], 'minOpacity' : 0.5, 'radius' : 35, 'blur' : 30}).addTo(mymap);
208
  }
209
  else {
210
    if (heatmapLayer != null) {
211
      mymap.removeLayer(heatmapLayer);
212
    }
213
  }
214

    
215
  // var heat_01 = ...
216
  // on background map.addLayer(heat_01) -> map.removeLayer(heat_01);
217
  // $(.leaflet-heatmap-layer).css('opacity', 'value');
218
}
219

    
220
function checkDataSetsAvailability(route) {
221

    
222
  $.ajax({
223
    type: "POST",
224
    // Todo it might be good idea to change db collections format
225
    url: route + '/' + $('#date').val(),
226
    success: function(result) {
227
      updateAvailableDataSets(result);
228
    }
229
  });
230
}
231

    
232
function updateAvailableDataSets(available) {
233
  
234
  var isOptionEnabled = true;
235
  $("#type > option").each(function() {
236
    if((this.value in available) == false) {
237
      $(this).prop('disabled', true)
238
      $(this).prop('selected', false)  
239
    }
240
    else {
241
      $(this).prop('disabled', false)
242
      if (isOptionEnabled) {
243
        $(this).prop('selected', true)
244
      } 
245
      isOptionEnabled = false;
246
    }
247
  });
248

    
249
  $('#submit-btn').prop('disabled', isOptionEnabled);
250

    
251
}
(6-6/6)