up recherche home avec nominatim
This commit is contained in:
parent
7f79ec3a9f
commit
c89751b45c
7 changed files with 601 additions and 220 deletions
|
@ -133,6 +133,13 @@
|
|||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
// Enable the labourage form
|
||||
if (typeof enableLabourageForm === 'function') {
|
||||
enableLabourageForm();
|
||||
} else {
|
||||
console.error('enableLabourageForm function not found');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
|
@ -112,6 +112,15 @@
|
|||
background-color: #f5f5f5;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
#resultsMap {
|
||||
height: 400px;
|
||||
width: 100%;
|
||||
margin-top: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
border-radius: 8px;
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
|
@ -162,7 +171,8 @@
|
|||
<i class="bi bi-globe text-primary" style="font-size: 3rem;"></i>
|
||||
</div>
|
||||
<h3 class="card-title h5 fw-bold">Visibilité Maximale</h3>
|
||||
<p class="card-text">Vos informations apparaîtront sur TomTom, Apple Plans, Facebook, CoMaps et des
|
||||
<p class="card-text">Vos informations apparaîtront sur TomTom, Apple Plans, Facebook, CoMaps et
|
||||
des
|
||||
centaines d'autres applications.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -222,9 +232,23 @@
|
|||
<div class="form-text">
|
||||
<i class="bi bi-info-circle"></i> Exemple: Café de la Place, Lyon
|
||||
</div>
|
||||
<div class="mt-2 d-none">
|
||||
<label class="form-label">Moteur de recherche :</label>
|
||||
<div class="form-check form-check-inline">
|
||||
<input class="form-check-input" type="radio" name="searchEngine" id="nominatim"
|
||||
value="nominatim" checked>
|
||||
<label class="form-check-label" for="nominatim">Nominatim</label>
|
||||
</div>
|
||||
<div class="form-check form-check-inline">
|
||||
<input class="form-check-input" type="radio" name="searchEngine" id="addok"
|
||||
value="addok">
|
||||
<label class="form-check-label" for="addok">Addok</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="resultsList" class="mt-4"></div>
|
||||
<div id="resultsMap"></div>
|
||||
<div id="proposeLink" class="d-none"></div>
|
||||
<div id="proposeMail" class="d-none">
|
||||
<div class="mb-3">
|
||||
|
@ -328,15 +352,15 @@
|
|||
{% block javascripts %}
|
||||
{{ parent() }}
|
||||
<script>
|
||||
// Données des villes pour la carte
|
||||
const citiesData = {{ citiesForMap|json_encode|raw }};
|
||||
// Données des villes pour la carte
|
||||
const citiesData = {{ citiesForMap|json_encode|raw }};
|
||||
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
|
||||
console.log('content loaded')
|
||||
// Créer le formulaire email
|
||||
const emailFormHtml = `
|
||||
console.log('content loaded')
|
||||
// Créer le formulaire email
|
||||
const emailFormHtml = `
|
||||
<form id="emailForm" class="mt-3">
|
||||
<div class="mb-3">
|
||||
<label for="email" class="form-label">Email</label>
|
||||
|
@ -346,87 +370,151 @@
|
|||
</form>
|
||||
`;
|
||||
|
||||
// Créer les divs pour les messages
|
||||
const proposeLinkHtml = `
|
||||
// Créer les divs pour les messages
|
||||
const proposeLinkHtml = `
|
||||
<div id="proposeLink" class="alert alert-success ">
|
||||
Un email a déjà été enregistré pour ce commerce. Nous vous enverrons le lien de modification.
|
||||
</div>
|
||||
`;
|
||||
|
||||
const proposeMailHtml = `
|
||||
const proposeMailHtml = `
|
||||
<div id="proposeMail" class="alert alert-info " >
|
||||
Aucun email n'est enregistré pour ce commerce. Veuillez saisir votre email pour recevoir le lien de modification.
|
||||
${emailFormHtml}
|
||||
</div>
|
||||
`;
|
||||
|
||||
// Ajouter les éléments au DOM
|
||||
// Initialize the elements only if they exist
|
||||
const proposeLinkElement = document.querySelector('#proposeLink');
|
||||
const proposeMailElement = document.querySelector('#proposeMail');
|
||||
// Ajouter les éléments au DOM
|
||||
// Initialize the elements only if they exist
|
||||
const proposeLinkElement = document.querySelector('#proposeLink');
|
||||
const proposeMailElement = document.querySelector('#proposeMail');
|
||||
|
||||
if (proposeLinkElement) {
|
||||
proposeLinkElement.innerHTML = proposeLinkHtml;
|
||||
}
|
||||
if (proposeLinkElement) {
|
||||
proposeLinkElement.innerHTML = proposeLinkHtml;
|
||||
}
|
||||
|
||||
if (proposeMailElement) {
|
||||
proposeMailElement.innerHTML = proposeMailHtml;
|
||||
}
|
||||
if (proposeMailElement) {
|
||||
proposeMailElement.innerHTML = proposeMailHtml;
|
||||
}
|
||||
|
||||
const searchInput = document.querySelector('#researchShop');
|
||||
const resultsList = document.querySelector('#resultsList');
|
||||
resultsList.classList.add('list-group', 'mt-2');
|
||||
const searchInput = document.querySelector('#researchShop');
|
||||
const resultsList = document.querySelector('#resultsList');
|
||||
const resultsMap = document.querySelector('#resultsMap');
|
||||
resultsList.classList.add('list-group', 'mt-2');
|
||||
|
||||
let timeoutId;
|
||||
searchInput.addEventListener('input', function (e) {
|
||||
clearTimeout(timeoutId);
|
||||
resultsList.innerHTML = '';
|
||||
// Initialize map
|
||||
let map = null;
|
||||
let markers = [];
|
||||
|
||||
if (e.target.value.length < 2) return;
|
||||
function initMap() {
|
||||
if (!map) {
|
||||
map = new maplibregl.Map({
|
||||
container: 'resultsMap',
|
||||
style: 'https://api.maptiler.com/maps/streets/style.json?key={{ maptiler_token }}',
|
||||
center: [2.213749, 46.227638], // Center of France
|
||||
zoom: 5
|
||||
});
|
||||
|
||||
const query_input_user = e.target.value;
|
||||
// Add navigation controls
|
||||
map.addControl(new maplibregl.NavigationControl());
|
||||
}
|
||||
}
|
||||
|
||||
timeoutId = setTimeout(() => {
|
||||
fetch(`https://demo.addok.xyz/search?q=${query_input_user}&limit=5`)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
console.log(data);
|
||||
function clearMarkers() {
|
||||
// Remove all existing markers
|
||||
markers.forEach(marker => marker.remove());
|
||||
markers = [];
|
||||
}
|
||||
|
||||
resultsList.innerHTML = '';
|
||||
const ul = document.createElement('ul');
|
||||
ul.classList.add('list-group');
|
||||
resultsList.appendChild(ul);
|
||||
function performSearch() {
|
||||
clearTimeout(timeoutId);
|
||||
resultsList.innerHTML = '';
|
||||
resultsMap.style.display = 'none';
|
||||
clearMarkers();
|
||||
|
||||
// Show a message if there are no results
|
||||
if (data.features.length === 0) {
|
||||
const noResultsMessage = document.createElement('div');
|
||||
noResultsMessage.classList.add('alert', 'alert-info', 'mt-3');
|
||||
noResultsMessage.innerHTML = `
|
||||
<i class="bi bi-info-circle"></i> Aucun résultat trouvé pour votre recherche.
|
||||
`;
|
||||
resultsList.appendChild(noResultsMessage);
|
||||
}
|
||||
const query_input_user = searchInput.value;
|
||||
|
||||
// Add "Not found" button
|
||||
if (true) {
|
||||
const notFoundButton = document.createElement('div');
|
||||
notFoundButton.classList.add('mt-3', 'text-center');
|
||||
notFoundButton.innerHTML = `
|
||||
if (query_input_user.length < 2) return;
|
||||
|
||||
// Get the selected search engine
|
||||
const searchEngine = document.querySelector('input[name="searchEngine"]:checked').value;
|
||||
|
||||
let searchUrl;
|
||||
if (searchEngine === 'nominatim') {
|
||||
// Use Nominatim API
|
||||
searchUrl = `https://nominatim.openstreetmap.org/search?q=${query_input_user}&format=json&limit=5&countrycodes=fr`;
|
||||
} else {
|
||||
// Use Addok API
|
||||
searchUrl = `https://demo.addok.xyz/search?q=${query_input_user}&limit=5`;
|
||||
}
|
||||
|
||||
fetch(searchUrl)
|
||||
}
|
||||
|
||||
// Add event listeners to search engine radio buttons
|
||||
document.querySelectorAll('input[name="searchEngine"]').forEach(radio => {
|
||||
radio.addEventListener('change', performSearch);
|
||||
});
|
||||
|
||||
let timeoutId;
|
||||
searchInput.addEventListener('input', function (e) {
|
||||
clearTimeout(timeoutId);
|
||||
resultsList.innerHTML = '';
|
||||
resultsMap.style.display = 'none';
|
||||
clearMarkers();
|
||||
|
||||
if (e.target.value.length < 2) return;
|
||||
|
||||
const query_input_user = e.target.value;
|
||||
|
||||
timeoutId = setTimeout(() => {
|
||||
// Get the selected search engine
|
||||
const searchEngine = document.querySelector('input[name="searchEngine"]:checked').value;
|
||||
|
||||
let searchUrl;
|
||||
if (searchEngine === 'nominatim') {
|
||||
// Use Nominatim API
|
||||
searchUrl = `https://nominatim.openstreetmap.org/search?q=${query_input_user}&format=json&limit=5&countrycodes=fr`;
|
||||
} else {
|
||||
// Use Addok API
|
||||
searchUrl = `https://demo.addok.xyz/search?q=${query_input_user}&limit=5`;
|
||||
}
|
||||
|
||||
fetch(searchUrl)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
console.log(data);
|
||||
|
||||
resultsList.innerHTML = '';
|
||||
const ul = document.createElement('ul');
|
||||
ul.classList.add('list-group');
|
||||
resultsList.appendChild(ul);
|
||||
|
||||
// This section will be handled in the code below
|
||||
|
||||
// Add "Not found" button
|
||||
if (true) {
|
||||
const notFoundButton = document.createElement('div');
|
||||
notFoundButton.classList.add('mt-3', 'text-center');
|
||||
notFoundButton.innerHTML = `
|
||||
<button id="notFoundButton" class="btn btn-outline-secondary">
|
||||
<i class="bi bi-search"></i> Je ne trouve pas mon commerce dans ces propositions
|
||||
</button>
|
||||
`;
|
||||
resultsList.appendChild(notFoundButton);
|
||||
resultsList.appendChild(notFoundButton);
|
||||
|
||||
// Add event listener to the "Not found" button
|
||||
document.getElementById('notFoundButton').addEventListener('click', () => {
|
||||
// Clear results list
|
||||
resultsList.innerHTML = '';
|
||||
// Add event listener to the "Not found" button
|
||||
document.getElementById('notFoundButton').addEventListener('click', () => {
|
||||
// Clear results list
|
||||
resultsList.innerHTML = '';
|
||||
// Hide map and clear markers
|
||||
resultsMap.style.display = 'none';
|
||||
clearMarkers();
|
||||
|
||||
// Show form to enter email
|
||||
const notFoundForm = document.createElement('div');
|
||||
notFoundForm.classList.add('card', 'mt-3');
|
||||
notFoundForm.innerHTML = `
|
||||
// Show form to enter email
|
||||
const notFoundForm = document.createElement('div');
|
||||
notFoundForm.classList.add('card', 'mt-3');
|
||||
notFoundForm.innerHTML = `
|
||||
<div class="card-header">
|
||||
<h5>Vous ne trouvez pas votre commerce ?</h5>
|
||||
</div>
|
||||
|
@ -441,179 +529,405 @@
|
|||
</form>
|
||||
</div>
|
||||
`;
|
||||
resultsList.appendChild(notFoundForm);
|
||||
resultsList.appendChild(notFoundForm);
|
||||
|
||||
// Add event listener to the form
|
||||
document.getElementById('notFoundForm').addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const email = document.getElementById('notFoundEmail').value;
|
||||
const query = document.querySelector('#researchShop').value;
|
||||
// Add event listener to the form
|
||||
document.getElementById('notFoundForm').addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const email = document.getElementById('notFoundEmail').value;
|
||||
const query = document.querySelector('#researchShop').value;
|
||||
|
||||
// Try to get the INSEE code from the first search result
|
||||
let insee = null;
|
||||
if (data.features && data.features.length > 0) {
|
||||
insee = data.features[0].properties.citycode;
|
||||
}
|
||||
// Try to get the INSEE code from the first search result
|
||||
let insee = null;
|
||||
if (searchEngine === 'nominatim') {
|
||||
if (data.length > 0 && data[0].address && data[0].address.postcode) {
|
||||
insee = data[0].address.postcode;
|
||||
}
|
||||
} else {
|
||||
if (data.features && data.features.length > 0) {
|
||||
insee = data.features[0].properties.citycode;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a Demande with the business name and email
|
||||
fetch('/api/demande/create', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
businessName: query,
|
||||
insee: insee
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
const demandeId = data.id;
|
||||
// Create a Demande with the business name and email
|
||||
fetch('/api/demande/create', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
businessName: query,
|
||||
insee: insee
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
const demandeId = data.id;
|
||||
|
||||
// Update the Demande with the email
|
||||
fetch(`/api/demande/${demandeId}/email`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// Show success message
|
||||
resultsList.innerHTML = `
|
||||
// Update the Demande with the email
|
||||
fetch(`/api/demande/${demandeId}/email`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// Show success message
|
||||
resultsList.innerHTML = `
|
||||
<div class="alert alert-success mt-3">
|
||||
<i class="bi bi-check-circle"></i> Votre demande a été enregistrée. Nous vous contacterons bientôt.
|
||||
</div>
|
||||
`;
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la mise à jour de la demande.');
|
||||
});
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la création de la demande.');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la mise à jour de la demande.');
|
||||
});
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la création de la demande.');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
data.features.forEach(feature => {
|
||||
const li = document.createElement('li');
|
||||
li.classList.add('list-group-item', 'cursor-pointer');
|
||||
li.textContent = `${feature.properties.name}, ${feature.properties.city}`;
|
||||
// Process the results based on the search engine
|
||||
if (searchEngine === 'nominatim') {
|
||||
// Show a message if there are no results
|
||||
if (data.length === 0) {
|
||||
const noResultsMessage = document.createElement('div');
|
||||
noResultsMessage.classList.add('alert', 'alert-info', 'mt-3');
|
||||
noResultsMessage.innerHTML = `
|
||||
<i class="bi bi-info-circle"></i> Aucun résultat trouvé pour votre recherche.
|
||||
`;
|
||||
resultsList.appendChild(noResultsMessage);
|
||||
} else {
|
||||
// Initialize map if we have results
|
||||
initMap();
|
||||
resultsMap.style.display = 'block';
|
||||
|
||||
li.addEventListener('click', () => {
|
||||
resultsList.innerHTML = ''; // Cacher la liste
|
||||
const [lon, lat] = feature.geometry.coordinates;
|
||||
const query = `[out:json];
|
||||
(
|
||||
node["shop"](around:100,${lat},${lon});
|
||||
node["amenity"](around:100,${lat},${lon});
|
||||
);
|
||||
out body;`;
|
||||
// Calculate bounds for the map
|
||||
const bounds = new maplibregl.LngLatBounds();
|
||||
|
||||
console.log('li clicked', li)
|
||||
// Add markers for each result
|
||||
data.forEach((result, index) => {
|
||||
const lat = parseFloat(result.lat);
|
||||
const lon = parseFloat(result.lon);
|
||||
|
||||
fetch('https://overpass-api.de/api/interpreter', {
|
||||
method: 'POST',
|
||||
body: query
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(osmData => {
|
||||
console.log('osmData', osmData)
|
||||
if (osmData.elements.length > 0) {
|
||||
console.log('osmData.elements', osmData.elements)
|
||||
// Extend bounds
|
||||
bounds.extend([lon, lat]);
|
||||
|
||||
const place = osmData.elements[1];
|
||||
console.log('place', place)
|
||||
console.log(`https://www.openstreetmap.org/${place.type}/${place.id}`, place.tags);
|
||||
// Create marker with result number
|
||||
const marker = new maplibregl.Marker({color: '#007bff'})
|
||||
.setLngLat([lon, lat])
|
||||
.setPopup(new maplibregl.Popup().setHTML(`
|
||||
<strong>${result.name || result.display_name}</strong><br>
|
||||
${result.address ? result.address.city || '' : ''}
|
||||
`));
|
||||
|
||||
// Add label with result number
|
||||
const el = marker.getElement();
|
||||
el.innerHTML = `<div style="position: absolute; top: -30px; left: 7px; background: white; border-radius: 50%; width: 20px; height: 20px; text-align: center; line-height: 20px; font-weight: bold; border: 2px solid #007bff;">${index + 1}</div>` + el.innerHTML;
|
||||
|
||||
marker.addTo(map);
|
||||
markers.push(marker);
|
||||
});
|
||||
|
||||
// Fit map to bounds with padding
|
||||
if (!bounds.isEmpty()) {
|
||||
map.fitBounds(bounds, {padding: 50});
|
||||
}
|
||||
}
|
||||
|
||||
data.forEach((result, index) => {
|
||||
const li = document.createElement('li');
|
||||
li.classList.add('list-group-item', 'cursor-pointer');
|
||||
|
||||
// Format the display text based on available properties
|
||||
let displayText = result.display_name;
|
||||
if (result.name) {
|
||||
displayText = result.name;
|
||||
if (result.address && result.address.city) {
|
||||
displayText += `, ${result.address.city}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Add result number
|
||||
li.innerHTML = `<span class="badge bg-primary me-2">${index + 1}</span> ${displayText}`;
|
||||
|
||||
li.addEventListener('click', () => {
|
||||
resultsList.innerHTML = ''; // Cacher la liste
|
||||
resultsMap.style.display = 'none'; // Cacher la carte
|
||||
clearMarkers(); // Supprimer les marqueurs
|
||||
const lat = parseFloat(result.lat);
|
||||
const lon = parseFloat(result.lon);
|
||||
const query = `[out:json];
|
||||
(
|
||||
node["shop"](around:10,${lat},${lon});
|
||||
node["amenity"](around:10,${lat},${lon});
|
||||
);
|
||||
out body;`;
|
||||
|
||||
console.log('result li clicked', result);
|
||||
|
||||
fetch('https://overpass-api.de/api/interpreter', {
|
||||
method: 'POST',
|
||||
body: query
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(osmData => {
|
||||
console.log('osmData', osmData);
|
||||
if (osmData.elements.length > 0) {
|
||||
console.log('osmData.elements', osmData.elements);
|
||||
|
||||
const place = osmData.elements[0];
|
||||
console.log('place ', place);
|
||||
console.log(`https://www.openstreetmap.org/${place.type}/${place.id}`, place.tags);
|
||||
|
||||
// Get INSEE code from result if available
|
||||
let insee = null;
|
||||
if (result.address && result.address.postcode) {
|
||||
insee = result.address.postcode;
|
||||
}
|
||||
|
||||
// Create a Demande with the business name
|
||||
fetch('/api/demande/create', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
businessName: `${query_input_user} __ ${place.tags.name}` || displayText,
|
||||
insee: insee,
|
||||
osmObjectType: place.type,
|
||||
osmId: place.id
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
const demandeId = data.id;
|
||||
|
||||
if (place.tags && (place.tags['contact:email'] || place.tags['email'])) {
|
||||
document.querySelector('#proposeLink').classList.remove('d-none');
|
||||
document.querySelector('#proposeMail').classList.add('d-none');
|
||||
} else {
|
||||
document.querySelector('#proposeMail').classList.remove('d-none');
|
||||
document.querySelector('#proposeLink').classList.add('d-none');
|
||||
|
||||
const emailForm = document.querySelector('#proposeMail form');
|
||||
emailForm.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const email = emailForm.querySelector('#emailInput').value;
|
||||
|
||||
// Update the Demande with the email
|
||||
fetch(`/api/demande/${demandeId}/email`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// Redirect to the original route for backward compatibility
|
||||
window.location.href = `/propose-email/${email}/${place.type}/${place.id}`;
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la mise à jour de la demande.');
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la création de la demande.');
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ul.appendChild(li);
|
||||
});
|
||||
} else {
|
||||
// Process Addok results
|
||||
// Show a message if there are no results
|
||||
if (data.features.length === 0) {
|
||||
const noResultsMessage = document.createElement('div');
|
||||
noResultsMessage.classList.add('alert', 'alert-info', 'mt-3');
|
||||
noResultsMessage.innerHTML = `
|
||||
<i class="bi bi-info-circle"></i> Aucun résultat trouvé pour votre recherche.
|
||||
`;
|
||||
resultsList.appendChild(noResultsMessage);
|
||||
} else {
|
||||
// Initialize map if we have results
|
||||
initMap();
|
||||
resultsMap.style.display = 'block';
|
||||
|
||||
// Calculate bounds for the map
|
||||
const bounds = new maplibregl.LngLatBounds();
|
||||
|
||||
// Add markers for each result
|
||||
data.features.forEach((feature, index) => {
|
||||
const [lon, lat] = feature.geometry.coordinates;
|
||||
|
||||
// Extend bounds
|
||||
bounds.extend([lon, lat]);
|
||||
|
||||
// Create marker with result number
|
||||
const marker = new maplibregl.Marker({color: '#007bff'})
|
||||
.setLngLat([lon, lat])
|
||||
.setPopup(new maplibregl.Popup().setHTML(`
|
||||
<strong>${feature.properties.name}</strong><br>
|
||||
${feature.properties.city || ''}
|
||||
`));
|
||||
|
||||
// Add label with result number
|
||||
const el = marker.getElement();
|
||||
el.innerHTML = `<div style="position: absolute; top: -30px; left: 7px; background: white; border-radius: 50%; width: 20px; height: 20px; text-align: center; line-height: 20px; font-weight: bold; border: 2px solid #007bff;">${index + 1}</div>` + el.innerHTML;
|
||||
|
||||
marker.addTo(map);
|
||||
markers.push(marker);
|
||||
});
|
||||
|
||||
// Fit map to bounds with padding
|
||||
if (!bounds.isEmpty()) {
|
||||
map.fitBounds(bounds, {padding: 50});
|
||||
}
|
||||
}
|
||||
|
||||
data.features.forEach((feature, index) => {
|
||||
const li = document.createElement('li');
|
||||
li.classList.add('list-group-item', 'cursor-pointer');
|
||||
li.innerHTML = `<span class="badge bg-primary me-2">${index + 1}</span> ${feature.properties.name}, ${feature.properties.city}`;
|
||||
|
||||
li.addEventListener('click', () => {
|
||||
resultsList.innerHTML = ''; // Cacher la liste
|
||||
resultsMap.style.display = 'none'; // Cacher la carte
|
||||
clearMarkers(); // Supprimer les marqueurs
|
||||
const [lon, lat] = feature.geometry.coordinates;
|
||||
const query = `[out:json];
|
||||
(
|
||||
node["shop"](around:100,${lat},${lon});
|
||||
node["amenity"](around:100,${lat},${lon});
|
||||
);
|
||||
out body;`;
|
||||
|
||||
console.log('li clicked', li)
|
||||
|
||||
fetch('https://overpass-api.de/api/interpreter', {
|
||||
method: 'POST',
|
||||
body: query
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(osmData => {
|
||||
console.log('osmData', osmData)
|
||||
if (osmData.elements.length > 0) {
|
||||
console.log('osmData.elements', osmData.elements)
|
||||
|
||||
const place = osmData.elements[1];
|
||||
console.log('place', place)
|
||||
console.log(`https://www.openstreetmap.org/${place.type}/${place.id}`, place.tags);
|
||||
|
||||
|
||||
// Create a Demande with the business name
|
||||
fetch('/api/demande/create', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
businessName: `${query_input_user} __ ${place.tags.name}` || `${feature.properties.name}, ${feature.properties.city}`,
|
||||
insee: feature.properties.citycode
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
const demandeId = data.id;
|
||||
// Create a Demande with the business name
|
||||
fetch('/api/demande/create', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
businessName: `${query_input_user} __ ${place.tags.name}` || `${feature.properties.name}, ${feature.properties.city}`,
|
||||
insee: feature.properties.citycode,
|
||||
osmObjectType: place.type,
|
||||
osmId: place.id
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
const demandeId = data.id;
|
||||
|
||||
if (place.tags && (place.tags['contact:email'] || place.tags['email'])) {
|
||||
document.querySelector('#proposeLink').classList.remove('d-none');
|
||||
document.querySelector('#proposeMail').classList.add('d-none');
|
||||
} else {
|
||||
document.querySelector('#proposeMail').classList.remove('d-none');
|
||||
document.querySelector('#proposeLink').classList.add('d-none');
|
||||
if (place.tags && (place.tags['contact:email'] || place.tags['email'])) {
|
||||
document.querySelector('#proposeLink').classList.remove('d-none');
|
||||
document.querySelector('#proposeMail').classList.add('d-none');
|
||||
} else {
|
||||
document.querySelector('#proposeMail').classList.remove('d-none');
|
||||
document.querySelector('#proposeLink').classList.add('d-none');
|
||||
|
||||
const emailForm = document.querySelector('#proposeMail form');
|
||||
emailForm.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const email = emailForm.querySelector('#emailInput').value;
|
||||
const emailForm = document.querySelector('#proposeMail form');
|
||||
emailForm.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const email = emailForm.querySelector('#emailInput').value;
|
||||
|
||||
// Update the Demande with the email
|
||||
fetch(`/api/demande/${demandeId}/email`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// Redirect to the original route for backward compatibility
|
||||
window.location.href = `/propose-email/${email}/${place.type}/${place.id}`;
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la mise à jour de la demande.');
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la création de la demande.');
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
// Update the Demande with the email
|
||||
fetch(`/api/demande/${demandeId}/email`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// Redirect to the original route for backward compatibility
|
||||
window.location.href = `/propose-email/${email}/${place.type}/${place.id}`;
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la mise à jour de la demande.');
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
alert('Erreur: ' + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Une erreur est survenue lors de la création de la demande.');
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ul.appendChild(li);
|
||||
});
|
||||
});
|
||||
}, 500);
|
||||
});
|
||||
ul.appendChild(li);
|
||||
});
|
||||
}
|
||||
});
|
||||
}, 500);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue