Got the automated address grabber working

This commit is contained in:
2025-06-22 13:59:40 -06:00
parent e1ddebc90a
commit 5e2a6320ff
5 changed files with 420 additions and 65 deletions

View File

@@ -579,8 +579,11 @@ function showAddLocationModal(lat, lng) {
addressInput.value = result.formattedAddress || result.fullAddress;
} else {
addressInput.value = ''; // Clear if lookup fails
showStatus('Could not fetch address automatically', 'warning');
// Don't show warning for automatic lookups
}
}).catch(error => {
console.error('Address lookup failed:', error);
addressInput.value = '';
});
// Focus on first name input
@@ -633,7 +636,12 @@ function editLocation(locationId) {
addressInput.value = result.formattedAddress || result.fullAddress;
} else if (!location['Address']) {
addressInput.value = '';
// Don't show error - just silently fail
}
}).catch(error => {
// Handle any unexpected errors
console.error('Address lookup failed:', error);
addressInput.value = '';
});
}
@@ -923,53 +931,20 @@ window.addEventListener('beforeunload', () => {
// Reverse geocode to get address from coordinates
async function reverseGeocode(lat, lng) {
try {
// Add a small delay to respect rate limits
await new Promise(resolve => setTimeout(resolve, 1000));
const response = await fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lng}&zoom=18&addressdetails=1`, {
headers: {
'User-Agent': 'NocoDB Map Viewer 1.0'
}
});
const response = await fetch(`/api/geocode/reverse?lat=${lat}&lng=${lng}`);
if (!response.ok) {
throw new Error('Geocoding service unavailable');
const error = await response.json();
throw new Error(error.error || 'Geocoding service unavailable');
}
const data = await response.json();
const result = await response.json();
if (data.error) {
throw new Error(data.error);
if (!result.success || !result.data) {
throw new Error('Geocoding failed');
}
// Format the address from the response
const address = data.display_name || '';
// You can also extract specific components if needed
const addressComponents = {
house_number: data.address?.house_number || '',
road: data.address?.road || '',
suburb: data.address?.suburb || data.address?.neighbourhood || '',
city: data.address?.city || data.address?.town || data.address?.village || '',
state: data.address?.state || '',
postcode: data.address?.postcode || '',
country: data.address?.country || ''
};
// Create a formatted address string
let formattedAddress = '';
if (addressComponents.house_number) formattedAddress += addressComponents.house_number + ' ';
if (addressComponents.road) formattedAddress += addressComponents.road + ', ';
if (addressComponents.suburb) formattedAddress += addressComponents.suburb + ', ';
if (addressComponents.city) formattedAddress += addressComponents.city + ', ';
if (addressComponents.state) formattedAddress += addressComponents.state + ' ';
if (addressComponents.postcode) formattedAddress += addressComponents.postcode;
return {
fullAddress: address,
formattedAddress: formattedAddress.trim().replace(/,$/, ''),
components: addressComponents
};
return result.data;
} catch (error) {
console.error('Reverse geocoding error:', error);
@@ -977,45 +952,73 @@ async function reverseGeocode(lat, lng) {
}
}
// Add a new function for forward geocoding (address to coordinates)
async function forwardGeocode(address) {
try {
const response = await fetch(`/api/geocode/forward?address=${encodeURIComponent(address)}`);
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Geocoding service unavailable');
}
const result = await response.json();
if (!result.success || !result.data) {
throw new Error('Geocoding failed');
}
return result.data;
} catch (error) {
console.error('Forward geocoding error:', error);
return null;
}
}
// Manual address lookup for add form
function lookupAddressForAdd() {
const lat = parseFloat(document.getElementById('location-lat').value);
const lng = parseFloat(document.getElementById('location-lng').value);
async function lookupAddressForAdd() {
const latInput = document.getElementById('location-lat');
const lngInput = document.getElementById('location-lng');
const addressInput = document.getElementById('location-address');
const lat = parseFloat(latInput.value);
const lng = parseFloat(lngInput.value);
if (!isNaN(lat) && !isNaN(lng)) {
addressInput.value = 'Looking up address...';
reverseGeocode(lat, lng).then(result => {
if (result) {
addressInput.value = result.formattedAddress || result.fullAddress;
showStatus('Address found!', 'success');
} else {
addressInput.value = '';
showStatus('Could not find address for these coordinates', 'error');
}
});
const result = await reverseGeocode(lat, lng);
if (result) {
addressInput.value = result.formattedAddress || result.fullAddress;
showStatus('Address found!', 'success');
} else {
addressInput.value = '';
showStatus('Could not find address for these coordinates', 'warning');
}
} else {
showStatus('Please enter valid coordinates first', 'warning');
}
}
// Manual address lookup for edit form
function lookupAddressForEdit() {
const lat = parseFloat(document.getElementById('edit-location-lat').value);
const lng = parseFloat(document.getElementById('edit-location-lng').value);
async function lookupAddressForEdit() {
const latInput = document.getElementById('edit-location-lat');
const lngInput = document.getElementById('edit-location-lng');
const addressInput = document.getElementById('edit-location-address');
const lat = parseFloat(latInput.value);
const lng = parseFloat(lngInput.value);
if (!isNaN(lat) && !isNaN(lng)) {
addressInput.value = 'Looking up address...';
reverseGeocode(lat, lng).then(result => {
if (result) {
addressInput.value = result.formattedAddress || result.fullAddress;
showStatus('Address found!', 'success');
} else {
addressInput.value = '';
showStatus('Could not find address for these coordinates', 'error');
}
});
const result = await reverseGeocode(lat, lng);
if (result) {
addressInput.value = result.formattedAddress || result.fullAddress;
showStatus('Address found!', 'success');
} else {
addressInput.value = '';
showStatus('Could not find address for these coordinates', 'warning');
}
} else {
showStatus('Please enter valid coordinates first', 'warning');
}