diff --git a/index.html b/index.html index 23d951c..1d2af97 100644 --- a/index.html +++ b/index.html @@ -108,7 +108,8 @@
diff --git a/js/lcm_main.js b/js/lcm_main.js index d26d418..73a2ba0 100644 --- a/js/lcm_main.js +++ b/js/lcm_main.js @@ -823,9 +823,7 @@ function searchFoodPlaces(map) { // Modifier la fonction init pour ajouter le contrôle des couches function init() { - // ... existing map initialization code ... - // Ajouter le groupe de marqueurs à la carte food_places_markers.addTo(map); $('#found_charging_stations').hide(); @@ -859,7 +857,16 @@ function init() { $('#searchButton').on('click', searchLocation); $('#shareUrl').on('click', copyCurrentUrl); - + + + $('#searchResults').on('change', function() { + const selectedIndex = $(this).val(); + if (selectedIndex !== null) { + const selectedPlace = $(this).find('option:selected').data('place'); + moveToLocation(selectedPlace); + } + }); + document.getElementById('searchButton').addEventListener('click', searchLocation); } @@ -875,35 +882,52 @@ function copyCurrentUrl() { document.execCommand('copy'); document.body.removeChild(dummy); } + + -function searchLocation(event) { - event.preventDefault(); - event.stopPropagation(); - - const location = document.getElementById('searchLocation').value; - if (!location) { - alert('Veuillez entrer un lieu à rechercher.'); - return; + function searchLocation() { + const location = $('#searchLocation').val(); + if (!location) { + alert('Veuillez entrer un lieu à rechercher.'); + return; + } + + const url = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(location)}`; + + fetch(url) + .then(response => response.json()) + .then(data => { + const resultsDropdown = $('#searchResults'); + resultsDropdown.empty(); // Clear previous results + + if (data.length === 0) { + alert('Lieu non trouvé. Veuillez essayer un autre terme de recherche.'); + resultsDropdown.hide(); + } else if (data.length === 1) { + const place = data[0]; + moveToLocation(place); + resultsDropdown.hide(); + } else { + data.forEach((place, index) => { + const option = $('') + .val(index) + .text(`${place.display_name} (${place.lat}, ${place.lon})`); + resultsDropdown.append(option); + }); + resultsDropdown.show(); + } + }) + .catch(error => { + console.error('Erreur lors de la recherche du lieu :', error); + alert('Erreur lors de la recherche du lieu.'); + }); } + + function moveToLocation(place) { + const lat = parseFloat(place.lat); + const lon = parseFloat(place.lon); + map.setView([lat, lon], 13); // Ajustez le niveau de zoom selon vos besoins + } + - const url = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(location)}`; - - fetch(url) - .then(response => response.json()) - .then(data => { - if (data.length > 0) { - const place = data[0]; - const lat = parseFloat(place.lat); - const lon = parseFloat(place.lon); - map.setView([lat, lon], 13); // Ajustez le niveau de zoom selon vos besoins - } else { - alert('Lieu non trouvé. Veuillez essayer un autre terme de recherche.'); - } - }) - .catch(error => { - console.error('Erreur lors de la recherche du lieu :', error); - alert('Erreur lors de la recherche du lieu.'); - }); -} - -init() \ No newline at end of file + init() \ No newline at end of file diff --git a/styles/style.css b/styles/style.css index 677412d..8fab8a1 100644 --- a/styles/style.css +++ b/styles/style.css @@ -632,4 +632,12 @@ overrides leaflet } } +.search-results { + width: calc(100% - 40px); + margin-top: 10px; + border: 1px solid var(--button-border); + border-radius: 5px; + display: none; +} + /*# sourceMappingURL=style.css.map */