diff --git a/html/static/js/address-suggestion.js b/html/static/js/address-suggestion.js index 61608c58..9474fbf1 100644 --- a/html/static/js/address-suggestion.js +++ b/html/static/js/address-suggestion.js @@ -83,10 +83,33 @@ class AddressInput extends HTMLElement { }); }, 300); } + async _handleClick(gid) { + try { + const url = `https://api.stadiamaps.com/geocoding/v2/place_details?ids=${gid}`; + const response = await fetch(url); + const data = await response.json(); + const suggestion = data.features[0]; + this.SetValue(suggestion); + + // Dispatch custom event for clients of this library + this.dispatchEvent( + new CustomEvent("address-selected", { + bubbles: true, + composed: true, // Allows event to cross shadow DOM boundary + detail: { + location: suggestion, + }, + }), + ); + } catch (error) { + console.error("Error fetching geocode of suggestion:", error); + } + } async _fetchAddressSuggestions(text) { try { - const url = `https://api.mapbox.com/search/geocode/v6/forward?q=${encodeURIComponent(text)}&access_token=${this._apiKey}`; + //const url = `https://api.mapbox.com/search/geocode/v6/forward?q=${encodeURIComponent(text)}&access_token=${this._apiKey}`; + const url = `https://api.stadiamaps.com/geocoding/v2/autocomplete?text=${encodeURIComponent(text)}&focus.point.lat=35&focus.point.lon=-115`; const response = await fetch(url); const data = await response.json(); @@ -100,44 +123,19 @@ class AddressInput extends HTMLElement { console.log("Rendering suggestions", suggestions); this._suggestions.innerHTML = suggestions .map((item, index) => { - if (item.properties.place_formatted != "") { - return ` -
-
${item.properties.name || item.properties.full_address}
-
${item.properties.place_formatted}
-
`; - } else { - return ` -
-
${item.properties.name || item.properties.full_address}
-
${item.properties.place_formatted}
-
`; - } + return ` +
+
${item.properties.name}
+
${item.properties.coarse_location}
+
`; }) .join(""); // Add click listeners to suggestions this.shadowRoot.querySelectorAll(".suggestion-item").forEach((el) => { el.addEventListener("click", (e) => { - const index = parseInt(el.dataset.index); - const suggestion = suggestions[index]; - this.SetValue(suggestion); - // Dispatch custom event - this.dispatchEvent( - new CustomEvent("address-selected", { - bubbles: true, - composed: true, // Allows event to cross shadow DOM boundary - detail: { - location: suggestion, - }, - }), - ); + this._handleClick(el.dataset.gid); }); }); } diff --git a/html/template/rmo/water.html b/html/template/rmo/water.html index 5ee65482..2784538e 100644 --- a/html/template/rmo/water.html +++ b/html/template/rmo/water.html @@ -63,7 +63,7 @@ ); let number = document.getElementById("address-number"); let postalcode = document.getElementById("address-postalcode"); - let place = document.getElementById("address-place"); + let locality = document.getElementById("address-locality"); let region = document.getElementById("address-region"); let street = document.getElementById("address-street"); @@ -75,8 +75,8 @@ country.value = context.iso_3166_a3; latitude.value = location.geometry.coordinates[1]; longitude.value = location.geometry.coordinates[0]; - latlngAccuracyType.value = props.precision; - latlngAccuracyValue.value = props.distance; + latlngAccuracyType.value = props.precision || 0; + latlngAccuracyValue.value = props.distance || 0; number.value = props.address_components.number; postalcode.value = props.address_components.postal_code; locality.value = context.whosonfirst.locality.name; @@ -272,8 +272,17 @@ id="address-postcode" name="address-postcode" /> + - +