-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
359 lines (270 loc) · 9.43 KB
/
main.js
File metadata and controls
359 lines (270 loc) · 9.43 KB
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
// imports
import XYZ from 'ol/source/XYZ';
import TileLayer from 'ol/layer/Tile';
import {Map, View} from 'ol';
import {fromLonLat} from 'ol/proj';
import Draw from 'ol/interaction/Draw';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';
import Feature from 'ol/Feature.js';
import Point from 'ol/geom/Point.js';
import {Icon, Style} from 'ol/style.js';
import io from 'socket.io-client';
let device_types;
//////////////////////////////////////////////////////
////////////////////////////////////////////////////// socket
// Setting up socket
const socket = io('http://127.0.0.1:5000');
// Handle connection
socket.on('connect', () => {
console.log('Connected to Flask Socket.IO server');
});
// Handle 'initialize' events from Flask
socket.on('initialize', (data) => {
console.log('Received Initialize', data.baseview)
console.log('Received Initialize', data.devices_types)
device_types = data.devices_types;
initializeMap(data.baseview);
});
// Handle 'update' events from Flask
socket.on('update', (data) => {
// console.log('Received update:', data);
updateMap(data);
});
//////////////////////////////////////////////////////
////////////////////////////////////////////////////// map
//SETTING UP OUR LAYERS
// Base Layer
var base = new TileLayer({
source: new XYZ(
{url:'http://192.168.20.84/hot/{z}/{x}/{y}.png'}
),
})
// Overlay (CSL Layer)
var overlay = new TileLayer({
source: new XYZ(
{url:'/tiles/{z}/{x}/{-y}.png'}
),
})
// Marker Layer
const iconFeature = new Feature;
let MarkerSource = new VectorSource({
features: [iconFeature],
});
let MarkerLayer = new VectorLayer({
source: MarkerSource,
});
//Setting up view (zoom level and coordinates)
let view = new View;
//Adding our Layers to the Map
const map = new Map({
target: 'map-container',
layers: [
base, overlay, MarkerLayer
],
view: view
});
// Left Click logic on map
map.on('click', function (evt) {
if (evt.originalEvent.which === 1){ //left click
customMenu.style.display = 'none';
console.log('Left clicked on map at:', evt.coordinate);
map.forEachFeatureAtPixel(evt.pixel, function (feature) {
const dev_name = feature.get('dev_name');
if (dev_name) {
console.log(`Left Clicked on feature: ${dev_name}`);
// Remove any existing popups
const existingPopup = document.getElementById('popup');
if (existingPopup) {
existingPopup.parentNode.removeChild(existingPopup);
}
// Create a new popup
const popup = document.createElement('div');
popup.id = 'popup';
popup.className = 'popup';
popup.innerText = dev_name;
// Position the popup
const coordinate = evt.coordinate;
const pixel = map.getPixelFromCoordinate(coordinate);
popup.style.left = pixel[0] + 'px';
popup.style.top = pixel[1] + 'px';
// Add the popup to the map container
document.getElementById('map-container').appendChild(popup);
// Remove the popup after 1 second
setTimeout(() => {
const existingPopup = document.getElementById('popup');
if (existingPopup) {
existingPopup.parentNode.removeChild(existingPopup);
}
}, 1000);
// alert(`Clicked on feature: ${name}`); // Display the name in an alert or create a popup
}
});
}
else{
console.log(evt.originalEvent.which);
}
});
// Right click logic on map
map.on('contextmenu', (evt) => {
if (evt.originalEvent.button === 2) {
evt.preventDefault(); // Prevent default context menu
console.log('Right clicked on map at:', evt.coordinate);
map.forEachFeatureAtPixel(evt.pixel, function (feature) {
const dev_id = feature.get('dev_id');
const dev_name = feature.get('dev_name');
const dev_type = feature.get('dev_type');
console.log(typeof(dev_name)); // Should be "string"
if (dev_name) {
console.log(`Right Clicked on feature: ${dev_name}`);
// Get the coordinates of the click
const coordinates = evt.coordinate;
// Clear existing options (optional)
customMenu.innerHTML = ''; // Removes all child elements (previous options)
// Access dictionary value using name dev_as key (if it exists)
const deviceOptions = device_types[dev_type]; // deviceOptions will be the value associated with the name key
// Check if deviceOptions exists before iterating
if (deviceOptions) {
for (const choice of deviceOptions) {
const option = document.createElement('div');
option.textContent = choice;
option.addEventListener('click', () => {
console.log(`Selected option: ${choice}`);
// Emit a socket.io event with clicked feature details
socket.emit('feature-right-clicked', {
dev_id: dev_id,
dev_type: dev_type,
dev_name: dev_name,
choice: choice,
// coordinates: coordinates,//not needed
});
customMenu.style.display = 'none'; // Hide the menu after option selection
});
customMenu.appendChild(option);
}
} else {
console.log(`No options found for device: ${dev_name}`); // Optional: Handle missing device type
}
// Position the custom menu
customMenu.style.left = evt.pixel[0] + 'px';
customMenu.style.top = evt.pixel[1] + 'px';
customMenu.style.display = 'block';
}
});
}
});
// Create the custom menu HTML element
const customMenu = document.createElement('div');
customMenu.id = 'customContextMenu';
customMenu.style.display = 'none';
customMenu.style.position = 'absolute';
customMenu.style.backgroundColor = 'white';
customMenu.style.border = '1px solid black';
customMenu.style.padding = '5px';
// Add options to the menu
const optionA = document.createElement('div');
optionA.textContent = 'Option A';
optionA.addEventListener('click', () => {
console.log('Option A pressed');
customMenu.style.display = 'none'; // Hide the menu after option selection
});
const optionB = document.createElement('div');
optionB.textContent = 'Option B';
optionB.addEventListener('click', () => {
console.log('Option B pressed');
customMenu.style.display = 'none'; // Hide the menu after option selection
});
customMenu.appendChild(optionA);
customMenu.appendChild(optionB);
document.body.appendChild(customMenu);
//middle click
document.addEventListener('mousedown', (evt) => {
if (evt.which === 2) {
console.log('Middle clicked on map at:', evt.coordinate); //not working
// Perform your desired action here
}
});
//////////////////////////////////////////////////////
/////////////////////////////////////////////// DRAW FUNCTIONALITIES
//Vector Source that will contain the features
let drawSource = new VectorSource();
//Layer that will contain our source
let drawLayer = new VectorLayer({
source: drawSource,
})
let draw;
//Interaction for starting draw
function addInteraction(type) {
draw = new Draw({
source: drawSource,
type: type,
});
drawLayer.set("name","Draw_Layer");
map.addInteraction(draw);
map.addLayer(drawLayer);
console.log(drawLayer.getProperties())
}
document.getElementById('print').addEventListener('click',()=>{
map.getLayers().forEach((layer)=> {
let layer_name = layer.getProperties().name;
console.log(layer.getProperties().name)
if(layer_name === "Draw_Layer"){
let source = layer.getSource();
console.log(source.getFeatures()[0].getGeometry().getCoordinates())
}
})
})
//Button for enabling and disabling draw
document.getElementById('draw-polygon').addEventListener('click', function() {
addInteraction('Polygon');
});
document.getElementById('draw-point').addEventListener('click', function() {
addInteraction('Point');
});
document.getElementById('disable-draw').addEventListener('click', function(){
if (draw) {
map.removeInteraction(draw);
}
});
// Once everything is setup
// request flask to send initial configurations (baseview, )
socket.emit('init');
//////////////////////////////////////////////////////
////////////////////////////////////////////////////// Functions
// initialize map
function initializeMap(data){
// Convert the latitude and longitude to the map's projection (assuming EPSG:3857)
let newCenter = fromLonLat([data.longitude, data.latitude]);
// Set the new center and zoom level
view.setCenter(newCenter);
view.setZoom(data.zoom);
}
// Update the map with new points
function updateMap(data) {
// Clear previous features
MarkerSource.clear();
// Iterate over the data and create new features
data.forEach(item => {
const iconFeature = new Feature({
geometry: new Point(fromLonLat([item.dev_longitude, item.dev_latitude])),
dev_id: item.dev_id,
dev_name: item.dev_name,
dev_status: item.dev_status,
dev_type: item.dev_type,
});
const iconStatus = item.dev_status;
const iconType = item.dev_type;
// Construct the image source dynamically
const iconSrc = `/src/${iconType}${iconStatus}.png`;
const iconStyle = new Style({
image: new Icon({
src: iconSrc,
scale: 0.06,
}),
});
iconFeature.setStyle(iconStyle);
//here it will be updated to markerlayer
MarkerSource.addFeature(iconFeature);
});
}