fix qthlookup, added timeout and move generic function to common.js

This commit is contained in:
HB9HIL
2026-02-18 08:53:54 +01:00
parent 6eff41e6d2
commit 57fb22a9d8
3 changed files with 65 additions and 95 deletions

View File

@@ -1270,67 +1270,37 @@ mymap.on('mousemove', onQsoMapMove);
<?php if ($this->session->userdata('user_qth_lookup') == 1) { ?>
$('#qth').focusout(function() {
if ($('#locator').val() === '') {
var lat = 0;
var lon = 0;
$.ajax({
async: false,
type: 'GET',
dataType: "json",
url: "https://nominatim.openstreetmap.org/?city=" + $(this).val() + "&format=json&addressdetails=1&limit=1",
data: {},
success: function (data) {
if (typeof data[0].lat !== 'undefined') {
lat = parseFloat(data[0].lat);
}
if (typeof data[0].lon !== 'undefined') {
lon = parseFloat(data[0].lon);
}
timeout: 5000,
data: {},
success: function (result) {
var data = Array.isArray(result) && result.length > 0 ? result[0] : null;
if (!data) {
return;
}
var lat = parseFloat(data.lat);
var lon = parseFloat(data.lon);
if (isNaN(lat) || isNaN(lon)) {
return;
}
var qthloc = LatLng2Loc(lat, lon, 10);
if (qthloc.length > 0) {
$('#locator').val(qthloc.substr(0, 6)).trigger('input');
}
},
error: function() {
showToast('Error', '<?= __("Location Lookup failed. Please check browser console."); ?>', 'bg-danger text-white', 5000);
},
});
if (lat !== 0 && lon !== 0) {
var qthloc = LatLng2Loc(lat, lon, 10);
if (qthloc.length > 0) {
$('#locator').val(qthloc.substr(0, 6)).trigger('focusout');
}
}
}
});
LatLng2Loc = function(y, x, num) {
if (x < -180) {
x = x + 360;
}
if (x > 180) {
x = x - 360;
}
var yqth, yi, yk, ydiv, yres, ylp, y;
var ycalc = new Array(0, 0, 0);
var yn = new Array(0, 0, 0, 0, 0, 0, 0);
var ydiv_arr = new Array(10, 1, 1 / 24, 1 / 240, 1 / 240 / 24);
ycalc[0] = (x + 180) / 2;
ycalc[1] = y + 90;
for (yi = 0; yi < 2; yi++) {
for (yk = 0; yk < 5; yk++) {
ydiv = ydiv_arr[yk];
yres = ycalc[yi] / ydiv;
ycalc[yi] = yres;
if (ycalc[yi] > 0) ylp = Math.floor(yres); else ylp = Math.ceil(yres);
ycalc[yi] = (ycalc[yi] - ylp) * ydiv;
yn[2 * yk + yi] = ylp;
}
}
var qthloc = "";
if (num >= 2) qthloc += String.fromCharCode(yn[0] + 0x41) + String.fromCharCode(yn[1] + 0x41);
if (num >= 4) qthloc += String.fromCharCode(yn[2] + 0x30) + String.fromCharCode(yn[3] + 0x30);
if (num >= 6) qthloc += String.fromCharCode(yn[4] + 0x41) + String.fromCharCode(yn[5] + 0x41);
if (num >= 8) qthloc += ' ' + String.fromCharCode(yn[6] + 0x30) + String.fromCharCode(yn[7] + 0x30);
if (num >= 10) qthloc += String.fromCharCode(yn[8] + 0x61) + String.fromCharCode(yn[9] + 0x61);
return qthloc;
}
<?php } ?>
<?php } ?>
</script>

View File

@@ -782,45 +782,3 @@ function continentToRegion(continent) {
return 1; // Default to Region 1 if unknown
}
}
/**
* Convert latitude/longitude coordinates to Maidenhead grid square locator
* @param {number} y - Latitude in decimal degrees (-90 to +90)
* @param {number} x - Longitude in decimal degrees (-180 to +180)
* @param {number} num - Precision level: 2=field, 4=square, 6=subsquare, 8=extended, 10=extended subsquare
* @returns {string} Maidenhead locator string (e.g., 'JO01ab' for 6-character precision)
*
* @example
* LatLng2Loc(51.5074, -0.1278, 6) // → 'IO91wm' (London)
* LatLng2Loc(40.7128, -74.0060, 4) // → 'FN20' (New York)
*/
function LatLng2Loc(y, x, num) {
if (x<-180) {x=x+360;}
if (x>180) {x=x-360;}
var yi, yk, ydiv, yres, ylp, y;
var ycalc = new Array(0,0,0);
var yn = new Array(0,0,0,0,0,0,0);
var ydiv_arr=new Array(10, 1, 1/24, 1/240, 1/240/24);
ycalc[0] = (x + 180)/2;
ycalc[1] = y + 90;
for (yi = 0; yi < 2; yi++) {
for (yk = 0; yk < 5; yk++) {
ydiv = ydiv_arr[yk];
yres = ycalc[yi] / ydiv;
ycalc[yi] = yres;
if (ycalc[yi] > 0) ylp = Math.floor(yres); else ylp = Math.ceil(yres);
ycalc[yi] = (ycalc[yi] - ylp) * ydiv;
yn[2*yk + yi] = ylp;
}
}
var qthloc="";
if (num >= 2) qthloc+=String.fromCharCode(yn[0] + 0x41) + String.fromCharCode(yn[1] + 0x41);
if (num >= 4) qthloc+=String.fromCharCode(yn[2] + 0x30) + String.fromCharCode(yn[3] + 0x30);
if (num >= 6) qthloc+=String.fromCharCode(yn[4] + 0x41) + String.fromCharCode(yn[5] + 0x41);
if (num >= 8) qthloc+=' ' + String.fromCharCode(yn[6] + 0x30) + String.fromCharCode(yn[7] + 0x30);
if (num >= 10) qthloc+=String.fromCharCode(yn[8] + 0x61) + String.fromCharCode(yn[9] + 0x61);
return qthloc;
}

View File

@@ -1466,6 +1466,48 @@ function decodeHtml(html) {
return txt.value;
}
/**
* Convert latitude/longitude coordinates to Maidenhead grid square locator
* @param {number} y - Latitude in decimal degrees (-90 to +90)
* @param {number} x - Longitude in decimal degrees (-180 to +180)
* @param {number} num - Precision level: 2=field, 4=square, 6=subsquare, 8=extended, 10=extended subsquare
* @returns {string} Maidenhead locator string (e.g., 'JO01ab' for 6-character precision)
*
* @example
* LatLng2Loc(51.5074, -0.1278, 6) // → 'IO91wm' (London)
* LatLng2Loc(40.7128, -74.0060, 4) // → 'FN20' (New York)
*/
function LatLng2Loc(y, x, num) {
if (x<-180) {x=x+360;}
if (x>180) {x=x-360;}
var yi, yk, ydiv, yres, ylp, y;
var ycalc = new Array(0,0,0);
var yn = new Array(0,0,0,0,0,0,0);
var ydiv_arr=new Array(10, 1, 1/24, 1/240, 1/240/24);
ycalc[0] = (x + 180)/2;
ycalc[1] = y + 90;
for (yi = 0; yi < 2; yi++) {
for (yk = 0; yk < 5; yk++) {
ydiv = ydiv_arr[yk];
yres = ycalc[yi] / ydiv;
ycalc[yi] = yres;
if (ycalc[yi] > 0) ylp = Math.floor(yres); else ylp = Math.ceil(yres);
ycalc[yi] = (ycalc[yi] - ylp) * ydiv;
yn[2*yk + yi] = ylp;
}
}
var qthloc="";
if (num >= 2) qthloc+=String.fromCharCode(yn[0] + 0x41) + String.fromCharCode(yn[1] + 0x41);
if (num >= 4) qthloc+=String.fromCharCode(yn[2] + 0x30) + String.fromCharCode(yn[3] + 0x30);
if (num >= 6) qthloc+=String.fromCharCode(yn[4] + 0x41) + String.fromCharCode(yn[5] + 0x41);
if (num >= 8) qthloc+=' ' + String.fromCharCode(yn[6] + 0x30) + String.fromCharCode(yn[7] + 0x30);
if (num >= 10) qthloc+=String.fromCharCode(yn[8] + 0x61) + String.fromCharCode(yn[9] + 0x61);
return qthloc;
}
// DO NOT DELETE: This message is intentional and serves as developer recruitment/engagement
console.log("Ready to unleash your coding prowess and join the fun?\n\n" +
"Check out our GitHub Repository and dive into the coding adventure:\n\n" +