const current_position = { latitude: '', longitude: '' }; const input = document.getElementById('address'); const suggestionsContainer = document.createElement('div'); suggestionsContainer.classList.add('autocomplete-suggestions'); input.parentNode.appendChild(suggestionsContainer); // Inizializziamo il geocoder di Google const geocoder = new google.maps.Geocoder(); // Funzione per ottenere suggerimenti dagli indirizzi con Geocoding API async function getGeocodingAutocomplete(inputText) { return new Promise((resolve, reject) => { geocoder.geocode({ 'address': inputText }, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { resolve(results); } else { reject(status); } }); }); } // Funzione per aggiornare i suggerimenti nella dropdown function updateSuggestions(suggestions) { suggestionsContainer.innerHTML = ""; suggestions.forEach((place) => { const item = document.createElement("div"); item.classList.add("autocomplete-suggestion"); item.textContent = place.formatted_address; item.onclick = () => selectPlace(place); suggestionsContainer.appendChild(item); }); } // Funzione per gestire la selezione di un luogo function selectPlace(place) { input.value = place.formatted_address; const latitude = place.geometry.location.lat(); const longitude = place.geometry.location.lng(); let keyLocalstorageLatitude = document.getElementById("indirizzo_appartamento_coordinates_latitude").dataset.keyLocalstorage; if(checkVar(keyLocalstorageLatitude)){ if(checkVar(latitude)){ localStorage.setItem(keyLocalstorageLatitude, latitude); } } let keyLocalstorageLongitude = document.getElementById("indirizzo_appartamento_coordinates_longitude").dataset.keyLocalstorage; if(checkVar(keyLocalstorageLongitude)){ if(checkVar(longitude)){ localStorage.setItem(keyLocalstorageLongitude, longitude); } } suggestionsContainer.innerHTML = ""; } // Ascoltatore di eventi per l'input dell'utente input.addEventListener("input", async function () { const inputValue = this.value; if (inputValue.length > 2) { try { const suggestions = await getGeocodingAutocomplete(inputValue); updateSuggestions(suggestions); } catch (error) { console.error("Errore nella richiesta Geocoding API:", error); } } else { suggestionsContainer.innerHTML = ""; } }); input.addEventListener("keydown", function(event) { if (event.keyCode === 13) { event.preventDefault(); } }); function getCurrentGeoPosition() { if (navigator.geolocation) { let options = { enableHighAccuracy: true, timeout: 3000, maximumAge: 0 }; navigator.geolocation.getCurrentPosition(getCurrentGeoPositionSuccess, getCurrentGeoPositionError, options); } else { alert('La geolocalizzazione non รจ supportata dal tuo browser'); } } function getCurrentGeoPositionSuccess(pos) { if($('#address').val() == '' || $('#address').val() == undefined || $('#address').val() == null || $('#address').val() == ' ') { let crd = pos.coords; current_position.latitude = crd.latitude; current_position.longitude = crd.longitude; $('#latitude').val(current_position.latitude); $('#longitude').val(current_position.longitude); setAddressResearchNameByLatLng(current_position.latitude, current_position.longitude); } // createGoogleMap(true); } function getCurrentGeoPositionError(err) { console.warn(`ERROR(${err.code}): ${err.message}`); } function setAddressResearchNameByLatLng(lat, lng) { geocoder = new google.maps.Geocoder() let latlng = new google.maps.LatLng(lat, lng); geocoder.geocode({ 'latLng': latlng }, function (results, status) { if (status == "OK" && results.length > 0) { $('#address').val(results[0].formatted_address); } }); }