forked from tejado/telegram-nearby-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
415 lines (348 loc) Β· 11.9 KB
/
web.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import 'ol/ol.css';
import Map from 'ol/Map';
import Overlay from 'ol/Overlay';
import View from 'ol/View';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import Geocoder from 'ol-geocoder';
import Popup from 'ol-popup';
import axios from 'axios';
import { fromLonLat, toLonLat } from 'ol/proj';
import { Circle as CircleStyle, Stroke, Style, Fill, Text } from 'ol/style';
import { OSM, Vector as VectorSource } from 'ol/source';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
import { trilaterate, fromLonLat_epsg4978, toLonLat_epsg4978 } from './trilaterate.js';
import SnailShellMatrix from './SnailShellMatrix.js';
import { flashFeature } from './utils.js';
var searchDistance = 500;
var searchInterval = 25 * 1000;
var flashDuration = 3000;
var cancel = false;
var users = {};
var userIdIndex = 0;
var usersIndex = {};
var usersIgnored = [];
var ssm, flashInterval, currentMarker;
// init OSM OL map
var layer = new TileLayer({
source: new OSM(),
});
var map = new Map({
layers: [layer],
target: 'map',
view: new View({
center: [0, 0],
zoom: 2,
}),
});
// styles
var stylePositions = new Style({
image: new CircleStyle({
fill: new Fill({
color: 'rgba(55, 200, 150, 0.5)',
}),
stroke: new Stroke({
width: 2,
color: 'rgba(255, 5, 5, 1)',
}),
radius: 7,
}),
});
let styleUsers = new Style({
image: new CircleStyle({
fill: new Fill({
color: '#fff',
}),
stroke: new Stroke({
width: 2,
color: 'rgba(0, 0, 0, 1)',
}),
radius: 15,
}),
text: new Text({
font: '12px Calibri,sans-serif',
fill: new Fill({ color: '#000' }),
stroke: new Stroke({
color: '#fff',
width: 3,
}),
}),
});
var source = new VectorSource({
wrapX: false,
});
var vector = new VectorLayer({
source: source,
style: stylePositions,
});
map.addLayer(vector);
var sourceUsers = new VectorSource({
wrapX: false,
});
var vectorUsers = new VectorLayer({
source: sourceUsers,
style: function (feature) {
styleUsers.getText().setText(feature.get('name'));
return styleUsers;
},
});
map.addLayer(vectorUsers);
// add location search
var geocoder = new Geocoder('nominatim', {
provider: 'osm',
targetType: 'glass-button',
lang: 'en',
placeholder: 'Map search ...',
limit: 5,
keepOpen: false,
autoComplete: true,
});
map.addControl(geocoder);
source.on('addfeature', function (e) {
currentMarker = e.feature;
flashFeature(map, layer, currentMarker, flashDuration);
flashInterval = window.setInterval(
() => flashFeature(map, layer, currentMarker, flashDuration),
2000
);
});
function addPositionMarker(c) {
var geom = new Point(c);
var feature = new Feature(geom);
source.addFeature(feature);
}
function addUserMarker(c, text) {
var geom = new Point([
c[0] + Math.floor(Math.random()) * 20,
c[1] - Math.floor(Math.random()) * 20,
]);
var feature = new Feature(geom);
feature.set('name', text);
sourceUsers.addFeature(feature);
}
function searchNearby(searchCoordinates) {
if (cancel) {
return;
}
addPositionMarker(searchCoordinates);
let c = toLonLat(searchCoordinates);
axios
.post('/getNearby', { lon: c[0], lat: c[1] })
.then((res) => {
console.log(`/test response (${res.status})`);
// stop flashing map marker
clearInterval(flashInterval);
let nextPoint = ssm.getNext()[1];
// trigger searchNearby again for next point
setTimeout(() => searchNearby(nextPoint), searchInterval);
// process search results
refreshNearby(searchCoordinates, res.data);
})
.catch((error) => {
alert(error);
console.error(error);
});
}
function refreshNearby(coordinates, nearbyUsers) {
console.log(`Refresh nearby ${coordinates}`);
if (!nearbyUsers.constructor == Object || Object.keys(nearbyUsers).length == 0) {
console.log('Empty result...');
}
let now = new Date().getTime();
for (let i in nearbyUsers) {
let d = { time: now, coordinates: coordinates, distance: nearbyUsers[i].distance };
if (users[i] === undefined) {
users[i] = {};
users[i].relId = ++userIdIndex;
usersIndex[users[i].relId] = i;
users[i].userId = nearbyUsers[i].userId;
users[i].name = nearbyUsers[i].name;
if (nearbyUsers[i].photo === undefined) {
users[i].photo = `/no_photo.png`;
} else {
users[i].photo = `/photos/${nearbyUsers[i].photo}`;
}
users[i].distances = [d];
users[i].locations = [];
} else {
users[i].distances.push(d);
if (usersIgnored.includes(users[i].relId)) {
continue;
}
// trilaterate when there are 3 distances
if (users[i].distances.length % 3 === 0) {
let lastDistances = users[i].distances.slice(users[i].distances.length - 3);
let c1 = toLonLat(lastDistances[0].coordinates);
let c2 = toLonLat(lastDistances[1].coordinates);
let c3 = toLonLat(lastDistances[2].coordinates);
let p1 = fromLonLat_epsg4978({ lon: c1[0], lat: c1[1] });
let p2 = fromLonLat_epsg4978({ lon: c2[0], lat: c2[1] });
let p3 = fromLonLat_epsg4978({ lon: c3[0], lat: c3[1] });
p1.r = lastDistances[0].distance;
p2.r = lastDistances[1].distance;
p3.r = lastDistances[2].distance;
let triP = trilaterate(p1, p2, p3, true);
console.log(`Trilaterated point: ${triP}`);
if (triP !== null) {
let triLonLat = toLonLat_epsg4978(triP);
let triC = fromLonLat([triLonLat.lon, triLonLat.lat]);
users[i].locations.push({
time: now,
coordinates: triC,
});
addUserMarker(triC, users[i].relId.toString());
}
}
}
}
let usersSorted = [];
for (let i in users) {
usersSorted.push(users[i]);
}
usersSorted.sort(function (a, b) {
if (a.locations > b.locations) return -1;
if (a.locations < b.locations) return 1;
if (a.distances > b.distances) return -1;
if (a.distances < b.distances) return 1;
if (a.name > b.name) return 1;
if (a.name < b.name) return -1;
});
let selectedRelId = $('div.active[data-user-id]').attr('data-user-id');
let newList = $('<div></div>');
newList.addClass('list list-row block');
for (let i in usersSorted) {
let user = usersSorted[i];
if (usersIgnored.includes(user.relId)) {
console.log(`Skipping user ${user.relId}`);
continue;
}
let uHtml = `
<div class="list-group-item list-group-item-action list-item" data-user-id="${user.relId}">
<div><a href="#"><span class="w-48 avatar gd-primary"><img src="${user.photo}" alt="."></span></a>
</div>
<div class="flex user-info"><a href="#" class="item-author text-color">${user.name}</a>
<div class="item-except text-sm h-1x">#${user.relId} -
Distances: <span class="badge badge-primary badge-pill">${user.distances.length}</span>
Locations: <span class="badge badge-primary badge-pill">${user.locations.length}</span></div>
</div>
<div class="no-wrap">
<input type="button" value="Ignore" class="btn_userIgnore btn btn-primary" data-user-id="${user.relId}"/>
</div>
</div>`;
uHtml = $.parseHTML(uHtml);
newList.append(uHtml);
}
$('#chatList').empty();
$('#chatList').append(newList);
selectUserInChat(selectedRelId, true);
}
$('#btn_start').click(function () {
console.log('start search');
cancel = false;
// fix position of the start-popup
map.un('postrender', onMove);
var x, y;
[x, y] = map.getView().getCenter();
ssm = new SnailShellMatrix(searchDistance, x, y);
searchNearby([x, y]);
});
$('#btn_stop').click(function () {
console.log('stop search');
cancel = true;
// avoid second onMove event trigger
map.un('postrender', onMove);
// add onMove to be able to re-position the start-popup
map.on('postrender', onMove);
});
$('#btn_reset').click(function () {
console.log('reset map');
source.clear();
});
$('#txt_gap').change(function () {
let newSearchDistance = parseInt($(this).val());
console.log(`Changed search distance value to ${newSearchDistance}`);
searchDistance = newSearchDistance;
if (ssm) {
ssm.setGap = searchDistance;
}
});
// create start popup and show it when map rendered first time
var popupElement = document.getElementById('popup');
var popup = new Overlay({
element: popupElement,
});
map.addOverlay(popup);
map.once('postrender', function (event) {
let coordinates = map.getView().getCenter();
popup.setPosition(coordinates);
$(popupElement).popover({
container: popupElement,
placement: 'top',
animation: false,
html: true,
content: '<p>Start</p>',
});
$(popupElement).popover('show');
});
// move start-popup to the center when the map moves
function onMove() {
let coordinates = map.getView().getCenter();
popup.setPosition(coordinates);
}
map.on('postrender', onMove);
// create user popup
var newPopup = new Popup();
map.addOverlay(newPopup);
// UI chat selection
function selectUserInChat(relId, scroll = false) {
if (relId === undefined) return;
// scroll chat list
if (scroll) {
$('#chatListContainer').scrollTop(0);
$('#chatListContainer').scrollTop(
$(`div[data-user-id="${relId}"]`).offset().top - $('#settings').outerHeight()
);
}
// some ui refreshments....
let thisChat = $(`div.list-group-item[data-user-id="${relId}"]`);
let others = $('.list-group-item').not(thisChat);
others.removeClass('active');
$('> div input', others).addClass('btn-primary').removeClass('btn-light');
$('> div div span', others).addClass('badge-primary').removeClass('badge-light');
$(thisChat).addClass('active');
$('> div input', thisChat).addClass('btn-light').removeClass('btn-primary');
$('> div div span', thisChat).addClass('badge-light').removeClass('badge-primary');
// show popup
let chatId = usersIndex[relId];
let lastLocation = users[chatId].locations.slice(-1);
if (lastLocation.length > 0) {
let c = lastLocation[0].coordinates;
newPopup.show(
c,
`<div><img src="${users[chatId].photo}" width="80" height="80" /><p>${users[chatId].name}</p></div>`
);
}
}
// scroll chatList when user feature is clicked on map
map.on('click', function (e) {
let found = false;
map.forEachFeatureAtPixel(e.pixel, function (feature, layer) {
if (!found && layer == vectorUsers) {
let relId = feature.get('name');
selectUserInChat(relId, true);
found = true;
}
});
});
$('#chatList').on('click', 'div', function () {
let relId = $(this).attr('data-user-id');
if (relId === undefined) return;
selectUserInChat(relId);
});
$('#chatList').on('click', '.btn_userIgnore', function () {
let relId = $(this).attr('data-user-id');
if (relId === undefined) return;
console.log(`Add ${relId} to ignore list`);
$(`div.list-group-item[data-user-id="${relId}"]`).remove();
usersIgnored.push(parseInt(relId));
});