Projekt

Obecné

Profil

Stáhnout (5.74 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
  date = $('#date').val()
156
  currentTime = parseInt($('#time').children("option:selected").val());
157
  setTimeline();
158

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

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

    
171
}
172

    
173
function preload(time, change) {
174

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

    
187
}
188

    
189

    
190
function drawHeatmap(data) {
191

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

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

    
211
function checkDataSetsAvailability(route) {
212

    
213
  $.ajax({
214
    type: "POST",
215
    // Todo it might be good idea to change db collections format
216
    url: route + '/' + $('#date').val(),
217
    success: function(result) {
218
      updateAvailableDataSets(result);
219
    }
220
  });
221
}
222

    
223
function updateAvailableDataSets(available) {
224
  
225
  var isOptionEnabled = true;
226
  $("#type > option").each(function() {
227
    if((this.value in available) == false) {
228
      $(this).prop('disabled', true)
229
      $(this).prop('selected', false)  
230
    }
231
    else {
232
      $(this).prop('disabled', false)
233
      if (isOptionEnabled) {
234
        $(this).prop('selected', true)
235
      } 
236
      isOptionEnabled = false;
237
    }
238
  });
239

    
240
  $('#submit-btn').prop('disabled', isOptionEnabled);
241

    
242
}
(6-6/6)