Projekt

Obecné

Profil

Stáhnout (5.58 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
}
129

    
130
function next() {
131
  currentTime = (currentTime + 1) % 24;
132
  drawHeatmap(data[currentTime]);
133
  setTimeline();
134
}
135

    
136
function setTimeline() {
137
  $('#timeline').text(currentTime + ":00");
138
  $('#timeline').attr('class', 'time hour-' + currentTime);
139
}
140

    
141
function loadCurrentTimeHeatmap(route) {
142

    
143
  dataSourceRoute = route;
144
  data = []
145

    
146
  name = $('#type').children("option:selected").text();
147
  var parts = $('#date').val().split('-');
148
  date = parts[2] + parts[1] + parts[0];
149
  currentTime = parseInt($('#time').children("option:selected").val());
150
  setTimeline();
151

    
152
  $.ajax({
153
    type: "POST",
154
    url: dataSourceRoute + '/' +  name + '/' + date + '/' + currentTime,
155
    success: function(result) {
156
      data[currentTime] = result;
157
      drawHeatmap(data[currentTime]);
158
    }
159
  });
160

    
161
  preload(currentTime, 1);
162
  preload(currentTime, -1);
163

    
164
}
165

    
166
function preload(time, change) {
167

    
168
  var ntime = time + change; 
169
  if (0 <=  ntime && ntime <= 23) {
170
      $.ajax({
171
        type: "POST",
172
        url: dataSourceRoute + '/' +  name + '/' + date + '/' + ntime,
173
        success: function(result) {
174
          data[ntime] = result;
175
          preload(ntime, change);
176
        }
177
      });
178
  }
179

    
180
}
181

    
182

    
183
function drawHeatmap(data) {
184

    
185
  // Todo still switched
186
  if (data['items'] != null) {
187
    points = data['items'].map(function (p) { return [p['x'], p['y'], p['number']]; });
188
    if (heatmapLayer != null) {
189
      mymap.removeLayer(heatmapLayer);
190
    }
191
    heatmapLayer = L.heatLayer(points, {'max' : data['max'], 'minOpacity' : 0.5, 'radius' : 35, 'blur' : 30}).addTo(mymap);
192
  }
193
  else {
194
    if (heatmapLayer != null) {
195
      mymap.removeLayer(heatmapLayer);
196
    }
197
  }
198

    
199
  // var heat_01 = ...
200
  // on background map.addLayer(heat_01) -> map.removeLayer(heat_01);
201
  // $(.leaflet-heatmap-layer).css('opacity', 'value');
202
}
203

    
204
function checkDataSetsAvailability(route) {
205

    
206
  var parts = $('#date').val().split('-');
207
  
208
  $.ajax({
209
    type: "POST",
210
    // Todo it might be good idea to change db collections format
211
    url: route + '/' + parts[2] + parts[1] + parts[0],
212
    success: function(result) {
213
      updateAvailableDataSets(result);
214
    }
215
  });
216
}
217

    
218
function updateAvailableDataSets(available) {
219
  
220
  var options = '';
221

    
222
  Object.entries(available).forEach(([key, value]) => {
223
    options += '<option value="' + value + '">' + key + '</option>\n'; 
224
  });
225

    
226
  if (options === '') {
227
    $('#submit-btn').prop('disabled', true);
228
  }
229
  else {
230
    $('#submit-btn').prop('disabled', false);
231
  }
232

    
233
  $('#type').empty().append(options);
234
  
235
}
(6-6/6)