-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotator.html
More file actions
450 lines (415 loc) · 13.2 KB
/
Copy pathrotator.html
File metadata and controls
450 lines (415 loc) · 13.2 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css?family=Victor Mono|Audiowide|Bebas Neue"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@100..900&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Syne+Mono&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<title>Rotator Controller</title>
<style>
body,
html {
height: 100%;
margin: 0;
}
#map {
width: 100%;
height: 100%;
}
.spawnMarker {
background-color: rgba(0, 0, 0, 0.1);
border: 0px;
position: relative;
display: inline-block;
left: -0.25em;
top: -0.5em;
}
.azi-muth {
font-family: "Syne Mono", sans-serif;
font-size: 2vw;
padding-top: 10px;
position: absolute;
bottom: 0;
left: 0;
z-index: 9999;
pointer-events: none;
}
.locator {
clear: both;
z-index: 9999;
display: block;
background-color: rgba(0, 0, 0, 0.3);
color: #fff;
padding: 1vh;
border: 0px none;
overflow: hidden;
position: absolute;
width: auto;
top: 0;
left: 43%;
font-family: "Roboto Condensed", sans-serif;
}
</style>
</head>
<body>
<div id="map"></div>
<div class="azi-muth" id="azimuth">Bearing unreacheable</div>
<div class="locator" id="cat">
<label for="jump" style="padding-left: 10px">Locator: </label>
<input
role="presentation"
autocomplete="off"
type="text"
id="jump"
placeholder="locator"
size="10"
onkeydown="search(this)"
/>
<button
id="jmpBtn"
onclick='fLoc(document.getElementById("jump").value);'
style="margin: 0px; padding: 1px; width: 30px"
>
Go
</button>
<button
id="rstBtn"
onclick="reset();"
style="margin: 0px; padding: 1px; width: 50px"
>
Reset
</button>
</div>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script src="maidenhead.js"></script>
<script>
// Force your Latitude and Longitude with the values below
//
// const myQTH = { lat: 40.12345, lng: -100.12345 };
//
// Define the rotator port for Hamlib (rotctld)
const port = "4535";
//
var hdg = 0;
// Set the rotator heading/bearing/azimuth
function rot(heading) {
url = "http://localhost:8073/rotctl/P?" + heading + " 0&__port=" + port;
// Make the POST request using the Fetch API
fetch(url, { mode: "no-cors" })
.then((result) => {
console.log("POST request successful:", result);
// Handle the response data here
})
.catch((error) => {
console.error("Error:", error);
// Handle any errors here
});
}
// Get the rotator heading/bearing/azimuth and show on the map
function get_rot() {
url = "http://localhost:8073/rotctl/p?&__port=" + port;
// Make the POST request using the Fetch API
fetch(url)
.then((response) => response.text())
.then((data) => {
data = JSON.parse(data);
az = data.azimuth.split(".");
document.getElementById("azimuth").innerHTML =
"Bearing " + az[0] + "°";
popup.setContent("Az " + az[0] + "°");
// console.log("Popup Id: ", popup._leaflet_id);
});
}
// Read the rotator heading/bearing/azimuth every 1.5 seconds
if (typeof port !== "undefined") {
getRotId = setInterval(() => get_rot(), 1500);
}
// Initialize the map
var map = L.map("map").setView([0, 0], 5);
// Add OpenStreetMap tiles
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19,
}).addTo(map);
// Add the locator squares
const maidenhead2 = L.maidenhead({ precision: 2 });
const maidenhead4 = L.maidenhead({ precision: 4 });
const maidenhead6 = L.maidenhead({ precision: 6 });
const maidenhead8 = L.maidenhead({ precision: 8 });
maidenhead2.addTo(map);
// Before zooming
map.on("zoomstart", function (e) {
console.log("ZOOMSTART", e);
console.log(map.getZoom());
// Remove the previous line if it exists
if (line) {
line.remove();
}
if (circleMarker) {
circleMarker.remove();
}
if (rectLoc) {
rectLoc.remove();
console.log("1.Removing locator Id: ", rectLoc._leaflet_id);
}
maidenhead2.remove();
maidenhead4.remove();
maidenhead6.remove();
maidenhead8.remove();
});
// After zooming
map.on("zoomend", function (e) {
console.log("ZOOMEND", e);
console.log(map.getZoom());
zoom = map.getZoom();
switch (true) {
case zoom <= 2:
console.log("No grid");
break;
case zoom <= 5:
maidenhead2.addTo(map);
console.log("Precision 2");
break;
case zoom <= 10:
maidenhead4.addTo(map);
console.log("Precision 4");
break;
case zoom <= 13:
maidenhead6.addTo(map);
console.log("Precision 6");
break;
case zoom <= 19:
maidenhead8.addTo(map);
console.log("Precision 8");
break;
default:
console.log("This should not happen");
}
// Restore layers
if (userMarker) {
userMarker.openPopup();
}
if (line) {
line.addTo(map);
}
if (circleMarker) {
circleMarker.addTo(map);
circleMarker.openPopup();
}
if (rectLoc) {
rectLoc.addTo(map);
console.log("1.Adding Locator Id: ", rectLoc._leaflet_id);
}
});
// Define user's location and marker and popup
var userLocation;
var userMarker = L.marker([0, 0]).addTo(map);
var popup = L.popup();
// Reset the map and center to user location
function reset() {
popup.options.autoPan = false;
popup.options.keepInView = false;
if (typeof myQTH !== "undefined") {
userLocation = myQTH;
userMarker.setLatLng(userLocation);
map.setView(userLocation, 5);
console.log("Forced location: ", myQTH);
} else {
// Get the user's location and initial zoom level Range is 1 to 13 - Ideal is 5
map.locate({ setView: true, maxZoom: 5 });
// Handle location found
map.on("locationfound", function (e) {
userLocation = e.latlng;
userMarker.setLatLng(userLocation);
console.log("Detected location: ", e.latlng);
});
}
// complete the initial popup
popup.setContent("You are here");
userMarker.bindPopup(popup).openPopup();
// clean up map layers if used
if (line) {
line.remove();
line = null;
}
if (circleMarker) {
circleMarker.remove();
circleMarker = null;
}
if (rectLoc) {
rectLoc.remove();
console.log("2.Removing locator (reset) Id: ", rectLoc._leaflet_id);
rectLoc = null;
}
// re-center the map to user location
if (userLocation) {
map.setView(userLocation);
}
}
// Initialize the map for the first time
reset();
// Initialize the line and arrow marker
var line;
var circleMarker;
var cMpopUp = L.popup();
cMpopUp.options.autoPan = false;
cMpopUp.options.keepInView = false;
// Handle click event on the map
map.on("click", function (e) {
if (!userLocation) {
alert("Location missing...");
return;
}
var clickLocation = e.latlng;
var bearing = calculateBearing(
userLocation.lat,
userLocation.lng,
clickLocation.lat,
clickLocation.lng
);
var distance = calculateDistance(
userLocation.lat,
userLocation.lng,
clickLocation.lat,
clickLocation.lng
);
// Remove the previous line if it exists
if (line) {
line.remove();
}
if (circleMarker) {
circleMarker.remove();
}
// Draw a line from user location to the clicked point
line = L.polyline([userLocation, clickLocation], {
color: "blue",
}).addTo(map);
// Add a circle marker at the clicked point
circleMarker = L.circleMarker(clickLocation, {
radius: 8,
color: "red",
autoPan: false,
keepInView: false,
}).addTo(map);
// Display bearing and distance information
cMpopUp.setContent(
"Bearing: " +
bearing.toFixed(2) +
"° <br>Distance: " +
distance.toFixed(2) +
" km<br>Latitude: " +
rtrim(clickLocation.lat, 8) +
"<br>Longitude: " +
rtrim(clickLocation.lng, 9) +
"<br>Locator: " +
L.Maidenhead.latLngToIndex(clickLocation.lat, clickLocation.lng, 8)
);
circleMarker.bindPopup(cMpopUp).openPopup();
hdg = bearing.toFixed(2);
circleMarker.addEventListener("click", _markerOnClick);
});
var _markerOnClick = function (e) {
rot(hdg);
console.log("Selected: ", hdg);
};
function rtrim(text, chars) {
return text.toString().substr(0, chars);
}
// Function to calculate bearing between two points
function calculateBearing(lat1, lon1, lat2, lon2) {
var rad = Math.PI / 180;
var dLon = (lon2 - lon1) * rad;
var y = Math.sin(dLon) * Math.cos(lat2 * rad);
var x =
Math.cos(lat1 * rad) * Math.sin(lat2 * rad) -
Math.sin(lat1 * rad) * Math.cos(lat2 * rad) * Math.cos(dLon);
var bearing = Math.atan2(y, x) * (180 / Math.PI);
return (bearing + 360) % 360;
}
// Function to calculate distance between two points using the Haversine formula
function calculateDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the Earth in kilometers
var dLat = (lat2 - lat1) * (Math.PI / 180);
var dLon = (lon2 - lon1) * (Math.PI / 180);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * (Math.PI / 180)) *
Math.cos(lat2 * (Math.PI / 180)) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var distance = R * c;
return distance;
}
// Variable to store the rectangle
var rectLoc;
// Locator finder
function fLoc(locator) {
console.log(locator);
switch (locator.length) {
case 2:
zoomLevel = 5;
break;
case 4:
zoomLevel = 9;
break;
case 6:
zoomLevel = 13;
break;
case 8:
zoomLevel = 17;
break;
default:
alert("Invalid locator!");
return;
}
// console.log(L.Maidenhead.indexToBBox(locator));
// Define the top-left and bottom-right coordinates (example coordinates)
var topLeft = [
L.Maidenhead.indexToBBox(locator)[0],
L.Maidenhead.indexToBBox(locator)[1],
]; // Example coordinate
var bottomRight = [
L.Maidenhead.indexToBBox(locator)[2],
L.Maidenhead.indexToBBox(locator)[3],
]; // Example coordinate
// Calculate the center of the rectangle
var centerLat = (topLeft[0] + bottomRight[0]) / 2;
var centerLng = (topLeft[1] + bottomRight[1]) / 2;
var center = [centerLat, centerLng];
// Remove the previous rectangle if it exists
if (rectLoc) {
rectLoc.remove();
console.log(locator, " 3.Removing locator Id: ", rectLoc._leaflet_id);
rectLoc = null;
}
// Optionally, add a rectangle to the map
rectLoc = L.rectangle([topLeft, bottomRight], {
color: "#ff7800",
weight: 1,
}).addTo(map);
console.log(locator, " New Locator Id: ", rectLoc._leaflet_id);
// Set the map view to the center of the rectangle
// Adjust the zoom level as needed
map.setView(center, zoomLevel);
}
function search(ele) {
if (event.key === "Enter") {
fLoc(ele.value);
}
}
</script>
</body>
</html>