reverse lookup for addresses #12

Merged
pitbuster merged 1 commit from reverse-geo into main 2023-03-10 01:13:01 -03:00
2 changed files with 17 additions and 4 deletions

2
Cargo.lock generated
View file

@ -654,7 +654,7 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]] [[package]]
name = "huellas" name = "huellas"
version = "0.1.2" version = "0.1.3"
dependencies = [ dependencies = [
"rocket", "rocket",
"rocket_db_pools", "rocket_db_pools",

View file

@ -107,7 +107,6 @@ function clearForm(): void {
document.getElementById("modal").style.display = "none"; document.getElementById("modal").style.display = "none";
} }
async function createPlace(): Promise<void> { async function createPlace(): Promise<void> {
const newPlace = getPlaceFromForm(); const newPlace = getPlaceFromForm();
@ -163,6 +162,12 @@ interface MapEvent {
relatedTarget: L.Marker | L.Path | undefined; relatedTarget: L.Marker | L.Path | undefined;
} }
async function getAddressReverse(lat: number, long: number): Promise<string> {
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<void> { async function setupMap(): Promise<void> {
/* Create/Edit form */ /* Create/Edit form */
const modal = document.getElementById("modal"); const modal = document.getElementById("modal");
@ -178,7 +183,7 @@ async function setupMap(): Promise<void> {
} }
} }
function openForm(op: Operation, lat: number, long: number): void { async function openForm(op: Operation, lat: number, long: number): Promise<void> {
/* Fill the form for us */ /* Fill the form for us */
const h1 = modal.getElementsByTagName("h1")[0]; const h1 = modal.getElementsByTagName("h1")[0];
if (op == Operation.Create) { if (op == Operation.Create) {
@ -192,13 +197,13 @@ async function setupMap(): Promise<void> {
latInput.value = lat.toString(); latInput.value = lat.toString();
longInput.value = long.toString(); longInput.value = long.toString();
const addressInput = (document.getElementById("address") as HTMLInputElement);
if (op == Operation.Edit) { if (op == Operation.Edit) {
const place = places.get(toStr({ lat: lat, lng: long })); const place = places.get(toStr({ lat: lat, lng: long }));
console.log(toStr({ lat: lat, lng: long })); console.log(toStr({ lat: lat, lng: long }));
/*Get the form elements*/ /*Get the form elements*/
const idInput = (document.getElementById("id") as HTMLInputElement); const idInput = (document.getElementById("id") as HTMLInputElement);
const nameInput = (document.getElementById("name") 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 openHoursArea = (document.getElementById("open_hours") as HTMLTextAreaElement);
const iconSelect = (document.getElementById("icon") as HTMLSelectElement); const iconSelect = (document.getElementById("icon") as HTMLSelectElement);
const descriptionArea = (document.getElementById("description") as HTMLTextAreaElement); const descriptionArea = (document.getElementById("description") as HTMLTextAreaElement);
@ -210,6 +215,14 @@ async function setupMap(): Promise<void> {
openHoursArea.value = place.open_hours; openHoursArea.value = place.open_hours;
iconSelect.value = place.icon; iconSelect.value = place.icon;
descriptionArea.value = place.description; descriptionArea.value = place.description;
} else {
getAddressReverse(lat, long).then(
(address) => {
if (addressInput.value.length == 0) {
addressInput.value = address;
}
}
);
} }
/* Plug callbacks */ /* Plug callbacks */