This commit is contained in:
Tykayn 2025-02-10 12:22:49 +01:00 committed by tykayn
parent 7c809b9629
commit 522f48f887
3 changed files with 66 additions and 33 deletions

View file

@ -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 = $('<option></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()
init()