Projekt

Obecné

Profil

Stáhnout (5.38 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
                  .setLatLng([lat / i, lng / i])
62
                  .setContent('<div id=\'place-info\'>Umístění: ' + info[currenInfo].place + '</div>' +
63
                              '<div id=\'number-info\'>Počet: ' + info[currenInfo].number + '</div>' +
64
                              '<div class=\'popup-controls\'>' +
65
                                '<button id=\'previous-info-btn\' onclick=\'previousInfo()\'>&lt</button>' +
66
                                '<div id=\'count-info\'>' + (currenInfo + 1) + '/' + info.length + '</div>' +
67
                                '<button id=\'next-info-btn\' onclick=\'nextInfo()\'>&gt</button>' +
68
                              '</div>'
69
                              )
70
                  .openOn(mymap);
71
    
72
    if (info.length == 1) {
73
      $('#previous-info-btn').prop('disabled', true);
74
      $('#next-info-btn').prop('disabled', true);
75
      $('.popup-controls').hide();
76
    }
77
  }
78

    
79
}
80

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

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

    
91
function displayInfoText() {
92
  $('#place-info').html('Umístění: ' + info[currenInfo].place)
93
  $('#number-info').html('Počet: ' + info[currenInfo].number)
94
  $('#count-info').html(currenInfo + 1 + '/' + info.length);
95
}
96

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

    
101
function changeAnimationState() {
102

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

    
119

    
120
}
121

    
122
function previous() {
123
  currentTime = (currentTime + 23) % 24;
124
  drawHeatmap(data[currentTime]);
125
  setTimeline();
126
}
127

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

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

    
139
function loadCurrentTimeHeatmap(route) {
140

    
141
  dataSourceRoute = route;
142
  data = []
143

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

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

    
159
  preload(currentTime, 1);
160
  preload(currentTime, -1);
161

    
162
}
163

    
164
function preload(time, change) {
165

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

    
178
}
179

    
180

    
181
function drawHeatmap(data) {
182

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

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

    
202
function checkDataSetsAvailability(route) {
203

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

    
216
function updateAvailableDataSets(available) {
217
  
218
  var options = '';
219

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

    
224
  $('#type').empty().append(options);
225
  
226
}
(6-6/6)