reverse geocoding for contributions

This commit is contained in:
2026-04-24 17:00:55 +02:00
parent 9ca215c36d
commit 5cadc5c1b4
2 changed files with 34 additions and 1 deletions

View File

@@ -205,7 +205,7 @@ function handle_update($input) {
}
// Builds dynamic SQL Query to only update sent Fields
$updatable_fields = ['category', 'title', 'description', 'status'];
$updatable_fields = ['category', 'title', 'description', 'status', 'address'];
$set_clauses = [];
$params = [':id' => $contribution_id];

View File

@@ -448,6 +448,14 @@ function submitCreate() {
return;
}
// Triggers Reverse Geocoding in Background
if (response.contribution_id && drawnGeometry) {
const coords = drawnGeomType === 'point' ? drawnGeometry.coordinates :
drawnGeomType === 'line' ? drawnGeometry.coordinates[0] :
drawnGeometry.coordinates[0][0];
reverseGeocode(response.contribution_id, coords[1], coords[0]);
}
Swal.fire('Eingereicht!', 'Ihr Beitrag wurde erfolgreich eingereicht und wird nach Prüfung durch das Moderationsteam veröffentlicht.', 'success');
closeCreateModal();
loadContributions();
@@ -917,6 +925,31 @@ function categoryIcon(cat) {
return '<i class="fa-solid ' + cat.faIcon + '" style="color:' + cat.color + ';"></i>';
}
// Reverse Geocodes Coordinates and saves Address to Contribution via API
function reverseGeocode(contributionId, lat, lng) {
fetch('https://nominatim.openstreetmap.org/reverse?format=json&lat=' + lat + '&lon=' + lng + '&zoom=18&addressdetails=1', {
headers: { 'Accept-Language': 'de' }
})
.then(function (r) { return r.json(); })
.then(function (data) {
if (data.display_name) {
const addr = data.address || {};
const parts = [];
if (addr.road) parts.push(addr.road + (addr.house_number ? ' ' + addr.house_number : ''));
if (addr.city || addr.town || addr.village) parts.push(addr.city || addr.town || addr.village);
const shortAddress = parts.length > 0 ? parts.join(', ') : data.display_name.split(',').slice(0, 2).join(',');
// Saves Address to Database via API
apiCall({
action: 'update',
contribution_id: contributionId,
address: shortAddress
}, function () {});
}
})
.catch(function () {});
}
// =====================================================================
// Block 16: Application Startup