2017-10-23 12:01:48 +02:00
|
|
|
const MAXNUMCOORD = 100;
|
2017-10-23 14:38:47 +02:00
|
|
|
const PINGWAITTIME = 1000*1; //1s
|
2017-10-27 16:16:40 +02:00
|
|
|
const MAXIMGROTATION = parseInt(max_img_rotation);
|
2017-10-23 12:01:48 +02:00
|
|
|
|
|
|
|
const OSMURL='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
|
|
|
|
const OSMATTRIB='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
|
2017-10-23 16:34:14 +02:00
|
|
|
var ROTATIONWAITTIME = 1000*rotation_wait_time; //seconds
|
2017-10-23 16:56:25 +02:00
|
|
|
var ZOOMLEVEL = zoomlevel;
|
2017-10-24 17:07:47 +02:00
|
|
|
var regionhits = {};
|
|
|
|
var regionhitsMax = 10;
|
2017-10-23 12:01:48 +02:00
|
|
|
|
|
|
|
var myOpenStreetMap = L.map('feedDivMap1').setView([0, 0], 1);
|
2017-10-23 16:56:25 +02:00
|
|
|
var osm = new L.TileLayer(OSMURL, {minZoom: 0, maxZoom: 18}).addTo(myOpenStreetMap);
|
2017-10-23 12:01:48 +02:00
|
|
|
|
|
|
|
class MapEvent {
|
|
|
|
constructor(json, marker) {
|
|
|
|
this.coord = json.coord;
|
2017-10-24 17:07:47 +02:00
|
|
|
this.regionCode = json.regionCode;
|
2017-10-23 12:01:48 +02:00
|
|
|
this.marker = marker;
|
|
|
|
this.categ = json.categ;
|
|
|
|
this.value = json.value;
|
2017-10-23 14:38:47 +02:00
|
|
|
this.country = json.country;
|
|
|
|
this.specifName = json.specifName;
|
|
|
|
this.cityName = json.cityName;
|
2017-10-23 12:01:48 +02:00
|
|
|
this.text = this.categ + ": " + this.value;
|
2018-09-27 09:38:39 +02:00
|
|
|
let underText = "";
|
|
|
|
if (this.specifName !== null && this.cityName !== null) {
|
|
|
|
underText = this.specifName+", "+this.cityName;
|
|
|
|
} else if (this.specifName !== null) {
|
|
|
|
underText = this.specifName;
|
|
|
|
} else if (this.cityName !== null) {
|
|
|
|
underText = this.cityName;
|
|
|
|
} else {
|
|
|
|
underText = "";
|
|
|
|
}
|
|
|
|
this.textMarker = "<b>{1}</b><br>{2}".replace("{1}", this.country).replace("{2}", underText);
|
2017-10-23 12:01:48 +02:00
|
|
|
}
|
2017-10-20 15:09:53 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 12:01:48 +02:00
|
|
|
class MapEventManager {
|
|
|
|
constructor() {
|
|
|
|
this._mapEventArray = [];
|
2017-10-30 11:07:41 +01:00
|
|
|
this._currentMapEvent;
|
2017-10-23 12:01:48 +02:00
|
|
|
this._nextEventToShow = 0;
|
|
|
|
this._first_map = true;
|
|
|
|
this._coordSet = new Set();
|
|
|
|
//current lat and lon shown in worldMap
|
|
|
|
this._latToPing;
|
|
|
|
this._lonToPing;
|
|
|
|
//Markers on the worldMap
|
|
|
|
this._allMarkers = [];
|
|
|
|
this._curMarkerNum = 0;
|
2017-10-23 14:38:47 +02:00
|
|
|
//use for cancelTimeout
|
|
|
|
this._timeoutRotate;
|
2017-10-23 12:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addMapEvent(mapevent) {
|
2017-10-27 16:16:40 +02:00
|
|
|
if(this.getNumberOfEvent() > MAXIMGROTATION) {
|
2017-10-23 12:01:48 +02:00
|
|
|
var toDel = this._mapEventArray[0];
|
2017-10-24 12:17:10 +02:00
|
|
|
toDel.marker.remove(); // remove marker
|
2017-10-23 12:01:48 +02:00
|
|
|
this._coordSet.delete(toDel.text);
|
2017-10-27 16:16:40 +02:00
|
|
|
this._mapEventArray = this._mapEventArray.slice(1);
|
2017-10-23 12:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!this._coordSet.has(mapevent.text)) { // avoid duplicate map
|
|
|
|
this._mapEventArray.push(mapevent);
|
|
|
|
this._coordSet.add(mapevent.text);
|
2017-10-24 17:07:47 +02:00
|
|
|
this.popupCoord(mapevent.coord, mapevent.regionCode);
|
2017-10-23 12:01:48 +02:00
|
|
|
} else {
|
2017-10-23 14:38:47 +02:00
|
|
|
//console.log('Duplicate coordinates');
|
2017-10-23 12:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(this._first_map) { // remove no_map pic
|
|
|
|
this.rotateMap();
|
|
|
|
this.ping();
|
|
|
|
this._first_map = false;
|
2017-10-23 14:38:47 +02:00
|
|
|
} else {
|
2017-10-23 14:48:59 +02:00
|
|
|
this.rotateMap(mapevent);
|
2017-10-20 15:09:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 12:01:48 +02:00
|
|
|
getNumberOfEvent() {
|
|
|
|
return this._mapEventArray.length
|
2017-10-20 15:09:53 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 12:01:48 +02:00
|
|
|
getNextEventToShow() {
|
|
|
|
var toShow = this._mapEventArray[this._nextEventToShow];
|
|
|
|
this._nextEventToShow = this._nextEventToShow == this._mapEventArray.length-1 ? 0 : this._nextEventToShow+1;
|
2017-10-30 11:07:41 +01:00
|
|
|
this._currentMapEvent = toShow;
|
2017-10-23 12:01:48 +02:00
|
|
|
return toShow;
|
|
|
|
}
|
|
|
|
|
2017-10-30 11:07:41 +01:00
|
|
|
getCurrentMapEvent() {
|
|
|
|
return this._currentMapEvent;
|
|
|
|
}
|
|
|
|
|
2017-10-23 12:01:48 +02:00
|
|
|
// Perform the roration of the map in the openStreetMap pannel
|
2017-10-23 14:48:59 +02:00
|
|
|
rotateMap(mapEvent) {
|
2017-10-23 14:38:47 +02:00
|
|
|
clearTimeout(this._timeoutRotate); //cancel current map rotation
|
2017-10-23 14:48:59 +02:00
|
|
|
if (mapEvent == undefined) {
|
|
|
|
var mapEvent = this.getNextEventToShow();
|
|
|
|
}
|
2017-10-23 14:38:47 +02:00
|
|
|
this._latToPing = mapEvent.coord.lat;
|
|
|
|
this._lonToPing = mapEvent.coord.lon;
|
|
|
|
var marker = mapEvent.marker;
|
2017-10-23 16:56:25 +02:00
|
|
|
myOpenStreetMap.flyTo([mapEvent.coord.lat, mapEvent.coord.lon], ZOOMLEVEL);
|
2017-10-23 14:38:47 +02:00
|
|
|
mapEvent.marker.bindPopup(mapEvent.textMarker).openPopup();
|
2017-10-20 15:09:53 +02:00
|
|
|
|
2017-10-25 12:12:14 +02:00
|
|
|
$("#textMap1").text(mapEvent.text);
|
2017-10-23 16:34:14 +02:00
|
|
|
if(ROTATIONWAITTIME != 0) {
|
|
|
|
this._timeoutRotate = setTimeout(function(){ mapEventManager.rotateMap(); }, ROTATIONWAITTIME);
|
|
|
|
}
|
2017-10-23 12:01:48 +02:00
|
|
|
}
|
2017-10-20 15:09:53 +02:00
|
|
|
|
2017-10-30 11:07:41 +01:00
|
|
|
directZoom() {
|
|
|
|
var mapEvent = this.getCurrentMapEvent();
|
|
|
|
if (mapEvent != undefined)
|
|
|
|
myOpenStreetMap.flyTo([mapEvent.coord.lat, mapEvent.coord.lon], ZOOMLEVEL);
|
|
|
|
}
|
|
|
|
|
2017-10-23 12:01:48 +02:00
|
|
|
ping() {
|
|
|
|
var pnts = openStreetMapObj.latLngToPoint(this._latToPing, this._lonToPing);
|
|
|
|
if (pnts != false) { //sometimes latLngToPoint return false
|
|
|
|
$("#feedDiv2").append(
|
|
|
|
$('<div class="marker_animation"></div>')
|
|
|
|
.css({'left': pnts.x-15 + 'px'}) /* HACK to center the effect */
|
|
|
|
.css({'top': pnts.y-15 + 'px'})
|
|
|
|
.css({ 'background-color': 'orange' })
|
|
|
|
.animate({ opacity: 0, scale: 1, height: '80px', width:'80px', margin: '-25px' }, 400, 'linear', function(){$(this).remove(); })
|
|
|
|
);
|
|
|
|
}
|
2017-10-23 14:38:47 +02:00
|
|
|
setTimeout(function(){ mapEventManager.ping(); }, PINGWAITTIME);
|
2017-10-23 12:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add and Manage markers on the map + make Animation
|
2017-10-24 17:07:47 +02:00
|
|
|
popupCoord(coord, regionCode) {
|
2017-10-23 12:01:48 +02:00
|
|
|
var coord = [coord.lat, coord.lon];
|
2017-11-09 10:32:39 +01:00
|
|
|
var color = 0.5*180;
|
2017-10-23 12:01:48 +02:00
|
|
|
var pnts = openStreetMapObj.latLngToPoint(coord[0], coord[1])
|
|
|
|
if (pnts != false) { //sometimes latLngToPoint return false
|
2017-10-23 14:38:47 +02:00
|
|
|
var addedMarker = openStreetMapObj.addMarker(this._curMarkerNum, coord, [color]);
|
2017-10-23 12:01:48 +02:00
|
|
|
this._allMarkers.push(this._curMarkerNum)
|
|
|
|
marker_animation(pnts.x, pnts.y, this._curMarkerNum);
|
2017-10-24 17:07:47 +02:00
|
|
|
update_region(regionCode);
|
|
|
|
|
|
|
|
|
2017-10-23 12:01:48 +02:00
|
|
|
this._curMarkerNum = this._curMarkerNum >= MAXNUMCOORD ? 0 : this._curMarkerNum+1;
|
|
|
|
if (this._allMarkers.length >= MAXNUMCOORD) {
|
2017-10-27 16:16:40 +02:00
|
|
|
var to_remove = this._allMarkers[0];
|
2017-10-23 12:01:48 +02:00
|
|
|
openStreetMapObj.removeMarkers([to_remove]);
|
|
|
|
this._allMarkers = this._allMarkers.slice(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 15:09:53 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 12:01:48 +02:00
|
|
|
function marker_animation(x, y, markerNum) {
|
|
|
|
var markerColor = openStreetMapObj.markers[markerNum].element.config.style.current.fill;
|
|
|
|
$("#feedDiv2").append(
|
|
|
|
$('<div class="marker_animation"></div>')
|
|
|
|
.css({'left': x-15 + 'px'}) /* HACK to center the effect */
|
|
|
|
.css({'top': y-15 + 'px'})
|
|
|
|
.css({ 'background-color': markerColor })
|
|
|
|
.animate({ opacity: 0, scale: 1, height: '80px', width:'80px', margin: '-25px' }, 700, 'linear', function(){$(this).remove(); })
|
|
|
|
);
|
|
|
|
}
|
2017-10-20 15:09:53 +02:00
|
|
|
|
2017-10-24 17:07:47 +02:00
|
|
|
function update_region(regionCode) {
|
|
|
|
if (regionCode in regionhits) {
|
|
|
|
regionhits[regionCode] += 1;
|
|
|
|
} else {
|
|
|
|
regionhits[regionCode] = 1;
|
|
|
|
}
|
|
|
|
// Force recomputation of min and max for correct color scaling
|
|
|
|
regionhitsMax = regionhitsMax >= regionhits[regionCode] ? regionhitsMax : regionhits[regionCode];
|
|
|
|
openStreetMapObj.series.regions[0].params.max = regionhitsMax;
|
|
|
|
// Update data
|
|
|
|
openStreetMapObj.series.regions[0].setValues(regionhits);
|
|
|
|
}
|
|
|
|
|
2017-10-23 12:01:48 +02:00
|
|
|
var mapEventManager = new MapEventManager();
|
|
|
|
var openStreetMapObj;
|
2017-10-20 15:09:53 +02:00
|
|
|
|
|
|
|
$(function(){
|
|
|
|
$('#feedDiv2').vectorMap({
|
|
|
|
map: 'world_mill',
|
|
|
|
markers: [],
|
|
|
|
series: {
|
2017-10-24 17:07:47 +02:00
|
|
|
markers: [{
|
|
|
|
attribute: 'fill',
|
2017-11-09 10:32:39 +01:00
|
|
|
//scale: ['#1A0DAB', '#e50000', '#62ff41'],
|
|
|
|
scale: ['#ffff66'],
|
2017-10-24 17:07:47 +02:00
|
|
|
values: [],
|
|
|
|
min: 0,
|
|
|
|
max: 180
|
|
|
|
}],
|
|
|
|
regions: [{
|
|
|
|
values: [],
|
|
|
|
min: 0,
|
|
|
|
max: 10,
|
2017-10-27 16:36:27 +02:00
|
|
|
scale: ['#003FBF','#0063BF','#0087BF','#00ACBF','#00BFAD','#00BF89','#00BF64','#00BF40','#00BF1C','#08BF00','#2CBF00','#51BF00','#75BF00','#99BF00','#BEBF00','#BF9B00','#BF7700','#BF5200','#BF2E00','#BF0900'],
|
|
|
|
normalizeFunction: 'linear',
|
|
|
|
legend: {
|
|
|
|
horizontal: true
|
|
|
|
}
|
2017-10-24 17:07:47 +02:00
|
|
|
}]
|
2017-10-20 15:09:53 +02:00
|
|
|
},
|
|
|
|
});
|
2017-10-23 12:01:48 +02:00
|
|
|
openStreetMapObj = $("#feedDiv2").vectorMap('get','mapObject');
|
2017-10-20 15:09:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Subscribe to the flask eventStream
|
2017-10-23 17:50:56 +02:00
|
|
|
var source_map;
|
|
|
|
function connect_source_map() {
|
|
|
|
source_map = new EventSource(urlForMaps);
|
|
|
|
source_map.onmessage = function(event) {
|
|
|
|
var json = jQuery.parseJSON( event.data );
|
|
|
|
var marker = L.marker([json.coord.lat, json.coord.lon]).addTo(myOpenStreetMap);
|
|
|
|
var mapEvent = new MapEvent(json, marker);
|
|
|
|
mapEventManager.addMapEvent(mapEvent);
|
2017-11-09 10:32:39 +01:00
|
|
|
|
2017-10-23 17:50:56 +02:00
|
|
|
};
|
|
|
|
source_map.onopen = function(){
|
2017-11-09 10:32:39 +01:00
|
|
|
console.log('connection is opened. '+source_map.readyState);
|
2017-10-23 17:50:56 +02:00
|
|
|
};
|
|
|
|
source_map.onerror = function(){
|
|
|
|
console.log('error: '+source_map.readyState);
|
|
|
|
setTimeout(function() { connect_source_map(); }, 5000);
|
|
|
|
};
|
|
|
|
}
|
2017-10-30 11:07:41 +01:00
|
|
|
|
|
|
|
$(document).ready(function () {
|
|
|
|
$( "#rotation_wait_time_selector" ).change(function() {
|
|
|
|
var sel = parseInt($( this ).val());
|
|
|
|
if(isNaN(sel)) {
|
|
|
|
rotation_wait_time = 0;
|
|
|
|
} else {
|
|
|
|
rotation_wait_time = sel;
|
|
|
|
}
|
|
|
|
var old = ROTATIONWAITTIME;
|
|
|
|
ROTATIONWAITTIME = 1000*rotation_wait_time; //seconds
|
|
|
|
if(old == 0) {
|
|
|
|
mapEventManager._timeoutRotate = setTimeout(function(){ mapEventManager.rotateMap(); }, ROTATIONWAITTIME);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$( "#zoom_selector" ).change(function() {
|
|
|
|
var sel = parseInt($( this ).val());
|
|
|
|
ZOOMLEVEL = sel;
|
|
|
|
mapEventManager.directZoom();
|
|
|
|
});
|
2018-09-27 09:38:39 +02:00
|
|
|
|
|
|
|
if (!!window.EventSource) {
|
|
|
|
$.getJSON( urlForMaps, function( data ) {
|
|
|
|
data.forEach(function(item) {
|
|
|
|
var marker = L.marker([item.coord.lat, item.coord.lon]).addTo(myOpenStreetMap);
|
|
|
|
var mapEvent = new MapEvent(item, marker);
|
|
|
|
mapEventManager.addMapEvent(mapEvent);
|
|
|
|
});
|
|
|
|
connect_source_map()
|
|
|
|
});
|
|
|
|
}
|
2017-10-30 11:07:41 +01:00
|
|
|
});
|