-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
174 lines (145 loc) · 4.81 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var map = L.map('map').setView([55.7558, 37.6177], 11);
var hash = new L.Hash(map);
var tiles = L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
// Global vars
var dataPoints = [];
var mapClassList = document.getElementById('map').classList;
var heat = L.heatLayer(dataPoints).addTo(map),
action = 'idle';
var overlay = document.getElementById("md-overlay"),
info = document.getElementById("md-info");
window.onkeydown = function(e) {
// SpaceBar
if (e.code == 'Space' || e.keyCode === 32) {
e.preventDefault();
action = 'draw';
mapClassList.add('paint');
};
// LeftCtrl
if (e.ctrlKey || e.keyCode === 17) {
e.preventDefault();
action = 'erase';
mapClassList.add('erase');
};
};
window.onkeyup = function(e) {
// SpaceBar
if (e.code == 'Space' || e.keyCode === 32) {
e.preventDefault();
mapClassList.remove('paint');
action = 'idle';
};
// LeftCtrl
if (e.ctrlKey || e.keyCode === 17) {
e.preventDefault();
mapClassList.remove('erase');
action = 'idle';
};
};
map.on({
mousemove: function (e) {
if (action == 'draw') {
dataPoints.push(e.latlng);
heat.redraw();
};
if (action == 'erase' && dataPoints != []) {
var dataPointsfiltered = dataPoints.filter(function(p) {
// Tolerance for zoom level 18
var tolerance = 0.0001,
inverted_zoom = 18 - map.getZoom();
return calcLength(p, e.latlng) > 0.0001 * Math.pow(2, inverted_zoom);
});
dataPoints = dataPointsfiltered;
heat.setLatLngs(dataPoints);
heat.redraw();
}
}
});
var saveControl = L.Control.extend({
options: {
position: 'topright'
},
onAdd: function (map) {
// create the control container with a particular class name
var container = L.DomUtil.create('div', 'save-control');
// ... initialize other DOM elements, add listeners, etc.
container.innerHTML = '<button id="save">Save data</button>';
L.DomEvent
.on(container.firstChild, 'click', function(e) {
var saveLink = document.createElement('a');
blob = new Blob([JSON.stringify(dataPoints)], {type : 'application/json'});
blobUrl = URL.createObjectURL(blob);
saveLink.href = blobUrl;
saveLink.download = 'DataPoints.json';
document.body.appendChild(saveLink);
saveLink.click();
setTimeout(function() {
window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(saveLink);
}, 300);
});
return container;
}
});
map.addControl(new saveControl());
var infoControl = L.Control.extend({
options: {
position: 'bottomright'
},
onAdd: function (map) {
// create the control container with a particular class name
var container = L.DomUtil.create('div', 'info-control');
// ... initialize other DOM elements, add listeners, etc.
container.innerHTML = '<button id="info" class="info">i</button>';
L.DomEvent
.on(container.firstChild, 'click', function(e) {
overlay.classList.toggle('md-show');
info.classList.toggle('md-show');
});
return container;
}
});
map.addControl(new infoControl());
// Modal close
var closeBtn = document.getElementById("md-close");
closeBtn.addEventListener('click', function(e) {
info.classList.toggle('md-show');
overlay.classList.toggle('md-show');
});
/* Drag'n Drop */
var dropArea = document.getElementById('map');
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults (e) {
e.preventDefault();
e.stopPropagation();
};
dropArea.addEventListener('drop', handleDrop, false)
function handleDrop(e) {
var dt = e.dataTransfer,
files = dt.files,
file = files.item(0);
var reader = new FileReader();
reader.readAsText(file);
// Show overlay while big files uploading
reader.onloadstart = function() {
overlay.classList.toggle('md-show');
};
reader.onloadend = function() {
var data = JSON.parse(reader.result);
// Merge current points with points from file
dataPoints = dataPoints.concat(data);
heat.setLatLngs(dataPoints);
heat.redraw();
// Hide overlay after big file uploaded
overlay.classList.toggle('md-show');
};
};
// Distance between points
function calcLength(p1,p2) {
var d = Math.sqrt(Math.pow(p1.lat- p2.lat, 2) + Math.pow(p1.lng - p2.lng, 2));
return d;
};