fixed centering issue with radius.

Signed-off-by: boris <boris@borishub.co.uk>
This commit is contained in:
boris
2025-04-22 01:17:48 +01:00
parent 183cca3fd3
commit d027e01ccc
12 changed files with 85 additions and 215 deletions

View File

@@ -92,84 +92,6 @@ document.addEventListener('DOMContentLoaded', function() {
setupHeaderSearchHandler();
});
/**
* Get postcode from coordinates using postcodes.io API
* @param {number} lat - Latitude
* @param {number} lng - Longitude
* @returns {Promise<string>} The postcode
*/
async function getPostcodeFromCoordinates(lat, lng) {
try {
const response = await fetch(`https://api.postcodes.io/postcodes?lon=${lng}&lat=${lat}`);
if (!response.ok) {
throw new Error('Could not find postcode for coordinates');
}
const data = await response.json();
if (data.status === 200 && data.result && data.result.length > 0) {
return data.result[0].postcode;
}
throw new Error('No postcode found for coordinates');
} catch (error) {
console.error('Error getting postcode from coordinates:', error);
throw error;
}
}
/**
* Handle geolocation success
* @param {GeolocationPosition} position - The position object
*/
async function handleGeolocationSuccess(position) {
try {
const { latitude, longitude } = position.coords;
// Get postcode from coordinates
const postcode = await getPostcodeFromCoordinates(latitude, longitude);
// Update the postcode input
const postcodeInput = document.getElementById('postcode');
if (postcodeInput) {
postcodeInput.value = postcode;
// Submit the form to update the map
const postcodeForm = document.getElementById('postcodeForm');
if (postcodeForm) {
postcodeForm.dispatchEvent(new Event('submit'));
}
}
} catch (error) {
console.error('Error processing geolocation:', error);
alert('Error getting your location: ' + error.message);
}
}
/**
* Handle geolocation error
* @param {GeolocationPositionError} error - The error object
*/
function handleGeolocationError(error) {
console.error('Geolocation error:', error);
let message = 'Error getting your location: ';
switch(error.code) {
case error.PERMISSION_DENIED:
message += 'Please enable location access in your browser settings.';
break;
case error.POSITION_UNAVAILABLE:
message += 'Location information is unavailable.';
break;
case error.TIMEOUT:
message += 'Location request timed out.';
break;
default:
message += 'An unknown error occurred.';
}
alert(message);
}
/**
* Set up form handlers for postcode and radius inputs
*/
@@ -245,11 +167,13 @@ function setupFormHandlers() {
}
if (radiusSelect) {
radiusSelect.addEventListener('change', function() {
radiusSelect.addEventListener('change', async function(e) {
e.preventDefault();
const postcode = document.getElementById('postcode').value;
const coords = await getPostcodeCoordinates(postcode);
const radius = parseFloat(this.value);
if (currentPostcode) {
updateMapLocation(null, radius); // null coords means use existing center
}
updateMapLocation(coords, radius);
});
}
}
@@ -402,7 +326,7 @@ function toRad(degrees) {
* @returns {string} HTML content for popup
*/
function createPopupContent(facility) {
const isAuthenticated = window.simpleAuth && window.simpleAuth.isAuthenticated();
const isAuthenticated = window.auth && window.auth.isAuthenticated();
return `
<div class="facility-popup">
@@ -498,7 +422,7 @@ async function handleCommentSubmit(event, facilityId) {
event.preventDefault();
// Check authentication
if (!window.simpleAuth || !window.simpleAuth.isAuthenticated()) {
if (!window.auth || !window.auth.isAuthenticated()) {
alert('You must be logged in to add comments');
return false;
}