From 256b36f7028c5a716877d3d971204b6268df98d3 Mon Sep 17 00:00:00 2001 From: Felipe Date: Fri, 10 Mar 2023 01:12:59 -0300 Subject: [PATCH] reverse lookup for addresses (#12) fixes #6 Co-authored-by: Felipe Contreras Salinas Reviewed-on: https://oolong.ludwig.dog/pitbuster/huellas/pulls/12 --- Cargo.lock | 2 +- ts-client/client.ts | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0166af..eec7613 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -654,7 +654,7 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] name = "huellas" -version = "0.1.2" +version = "0.1.3" dependencies = [ "rocket", "rocket_db_pools", diff --git a/ts-client/client.ts b/ts-client/client.ts index 12e5d59..af68056 100644 --- a/ts-client/client.ts +++ b/ts-client/client.ts @@ -107,7 +107,6 @@ function clearForm(): void { document.getElementById("modal").style.display = "none"; } - async function createPlace(): Promise { const newPlace = getPlaceFromForm(); @@ -163,6 +162,12 @@ interface MapEvent { relatedTarget: L.Marker | L.Path | undefined; } +async function getAddressReverse(lat: number, long: number): Promise { + const nominatimEndpoint = `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${long}&format=json` + const address = (await fetch(nominatimEndpoint).then(response => response.json())).address; + return `${address.road} ${address.house_number}, ${address.city}`; +} + async function setupMap(): Promise { /* Create/Edit form */ const modal = document.getElementById("modal"); @@ -178,7 +183,7 @@ async function setupMap(): Promise { } } - function openForm(op: Operation, lat: number, long: number): void { + async function openForm(op: Operation, lat: number, long: number): Promise { /* Fill the form for us */ const h1 = modal.getElementsByTagName("h1")[0]; if (op == Operation.Create) { @@ -192,13 +197,13 @@ async function setupMap(): Promise { latInput.value = lat.toString(); longInput.value = long.toString(); + const addressInput = (document.getElementById("address") as HTMLInputElement); if (op == Operation.Edit) { const place = places.get(toStr({ lat: lat, lng: long })); console.log(toStr({ lat: lat, lng: long })); /*Get the form elements*/ const idInput = (document.getElementById("id") as HTMLInputElement); const nameInput = (document.getElementById("name") as HTMLInputElement); - const addressInput = (document.getElementById("address") as HTMLInputElement); const openHoursArea = (document.getElementById("open_hours") as HTMLTextAreaElement); const iconSelect = (document.getElementById("icon") as HTMLSelectElement); const descriptionArea = (document.getElementById("description") as HTMLTextAreaElement); @@ -210,6 +215,14 @@ async function setupMap(): Promise { openHoursArea.value = place.open_hours; iconSelect.value = place.icon; descriptionArea.value = place.description; + } else { + getAddressReverse(lat, long).then( + (address) => { + if (addressInput.value.length == 0) { + addressInput.value = address; + } + } + ); } /* Plug callbacks */