1// Variables globales 2var googleMaps = {}; 3var googleMapsMarkers = {}; 4 5/** 6 * Fonction callback appelée par l'API Google Maps 7 */ 8function initGoogleMap() { 9 // Initialiser toutes les instances de cartes 10 if (typeof window.googleMapsInstances !== 'undefined') { 11 window.googleMapsInstances.forEach(function(instance) { 12 initMapInstance(instance.id, instance.showList); 13 }); 14 } 15} 16 17/** 18 * Initialise une instance spécifique de carte 19 */ 20function initMapInstance(mapId, showList) { 21 var mapElement = document.getElementById(mapId); 22 if (!mapElement) { 23 console.error('Ãlément de carte non trouvé : ' + mapId); 24 return; 25 } 26 27 // Récupérer les paramètres de la carte 28 var zoom = parseInt(mapElement.dataset.zoom) || 10; 29 var centerLat = parseFloat(mapElement.dataset.centerLat) || 48.719440; 30 var centerLng = parseFloat(mapElement.dataset.centerLng) || 9.124932; 31 32 // Créer la carte 33 var map = new google.maps.Map(mapElement, { 34 zoom: zoom, 35 center: { lat: centerLat, lng: centerLng }, 36 mapTypeId: google.maps.MapTypeId.ROADMAP, 37 styles: [ 38 { 39 featureType: 'poi', 40 elementType: 'labels', 41 stylers: [{ visibility: 'off' }] 42 } 43 ] 44 }); 45 46 // Stocker la référence de la carte 47 googleMaps[mapId] = map; 48 googleMapsMarkers[mapId] = []; 49 50 // Masquer le message de chargement 51 var loadingElement = mapElement.parentNode.querySelector('.google-maps-loading'); 52 if (loadingElement) { 53 loadingElement.style.display = 'none'; 54 } 55 56 // Charger les marqueurs 57 loadMarkers(mapId, showList); 58} 59 60/** 61 * Charge les marqueurs depuis la base de données 62 */ 63function loadMarkers(mapId, showList) { 64 if (typeof google_maps_public === 'undefined') { 65 console.error('Variables AJAX non définies'); 66 return; 67 } 68 69 jQuery.ajax({ 70 url: google_maps_public.ajax_url, 71 type: 'POST', 72 data: { 73 action: 'get_markers', 74 nonce: google_maps_public.nonce 75 }, 76 success: function(response) { 77 if (response.success && response.data) { 78 addMarkersToMap(mapId, response.data); 79 if (showList) { 80 createAddressList(mapId, response.data); 81 } 82 } else { 83 console.error('Erreur lors du chargement des marqueurs:', response); 84 } 85 }, 86 error: function(xhr, status, error) { 87 console.error('Erreur AJAX:', error); 88 } 89 }); 90} 91 92/** 93 * Crée la liste des adresses 94 */ 95function createAddressList(mapId, markers) { 96 var listContainer = document.getElementById(mapId + '-address-list'); 97 if (!listContainer) return; 98 99 var loadingElement = listContainer.querySelector('.address-list-loading'); 100 var itemsContainer = listContainer.querySelector('.address-list-items'); 101 102 if (loadingElement) loadingElement.style.display = 'none'; 103 if (itemsContainer) itemsContainer.style.display = 'block'; 104 105 // Vider le contenu existant 106 itemsContainer.innerHTML = ''; 107 108 if (markers.length === 0) { 109 itemsContainer.innerHTML = '<p>Aucun marqueur trouvé.</p>'; 110 return; 111 } 112 113 // Grouper par zone si disponible 114 var groupedMarkers = {}; 115 markers.forEach(function(marker) { 116 var zone = marker.zone || 'Autres'; 117 if (!groupedMarkers[zone]) { 118 groupedMarkers[zone] = []; 119 } 120 groupedMarkers[zone].push(marker); 121 }); 122 123 // Créer la liste 124 Object.keys(groupedMarkers).forEach(function(zone) { 125 var zoneHeader = document.createElement('h3'); 126 zoneHeader.className = 'address-zone-header'; 127 var textzone = zone; 128 if(zone == "allemagne") textzone = "Allemagne"; 129 if(zone == "europe-ouest") textzone = "Europe de l'Ouest"; 130 if(zone == "europe-centrale") textzone = "Europe Centrale et de l'Est"; 131 zoneHeader.textContent = textzone; 132 itemsContainer.appendChild(zoneHeader); 133 134 var zoneList = document.createElement('div'); 135 zoneList.className = 'address-zone-group'; 136 137 groupedMarkers[zone].sort(function(a, b) { 138 return a.name.localeCompare(b.name); 139 }); 140 141 groupedMarkers[zone].forEach(function(marker) { 142 var addressItem = document.createElement('div'); 143 addressItem.className = 'address-item'; 144 addressItem.dataset.markerId = marker.id; 145 146 var addressContent = '<strong>' + marker.name.replaceAll("/R", "</br>") + '</strong><br>'; 147 addressContent += '<span class="address-text">' + marker.address.replaceAll("/R", "</br>") + '</span>'; 148 149 if (marker.description) { 150 addressContent += '<br><small class="address-description">' + marker.description.replaceAll("/R", "</br>") + '</small>'; 151 }else{ 152 addressContent += '<br><small class="address-description"></small>'; 153 } 154 155 addressItem.innerHTML = addressContent; 156 157 // Ajouter l'événement de clic 158 addressItem.addEventListener('click', function() { 159 centerOnMarker(mapId, marker.id); 160 highlightAddressItem(this); 161 scrollToMap(mapId); 162 //showAllMarkers(mapId); 163 }); 164 165 zoneList.appendChild(addressItem); 166 }); 167 168 itemsContainer.appendChild(zoneList); 169 }); 170} 171 172/** 173 * Centre la carte sur un marqueur spécifique 174 */ 175function centerOnMarker(mapId, markerId) { 176 var map = googleMaps[mapId]; 177 if (!map) return; 178 179 var markers = googleMapsMarkers[mapId]; 180 var marker = markers.find(function(m) { 181 return m.markerData && m.markerData.id === markerId; 182 }); 183 184 if (marker) { 185 // Centrer la carte sur le marqueur 186 map.setCenter(marker.getPosition()); 187 map.setZoom(15); 188 189 // Ouvrir l'infoWindow 190 if (marker.infoWindow) { 191 // Fermer toutes les autres fenêtres
192 markers.forEach(function(m) { 193 if (m.infoWindow && m !== marker) { 194 m.infoWindow.close(); 195 } 196 }); 197 198 // Ouvrir cette fenêtre 199 marker.infoWindow.open(map, marker); 200 } 201 202 // Animation sur le marqueur 203 marker.setAnimation(google.maps.Animation.BOUNCE); 204 setTimeout(function() { 205 marker.setAnimation(null); 206 }, 1500); 207 } 208} 209 210/** 211 * Fait défiler la page jusqu'à la carte 212 */ 213function scrollToMap(mapId) { 214 var mapElement = document.getElementById(mapId); 215 if (mapElement) { 216 // Scroll doux vers la carte 217 mapElement.scrollIntoView({ 218 behavior: 'smooth', 219 block: 'center' 220 }); 221 222 // Ajouter un effet visuel temporaire sur la carte 223 mapElement.style.transition = 'box-shadow 0.5s ease'; 224 mapElement.style.boxShadow = '0 0 20px rgba(0, 115, 170, 0.5)'; 225 226 setTimeout(function() { 227 mapElement.style.boxShadow = 'none'; 228 }, 1000); 229 } 230} 231 232/** 233 * Affiche tous les marqueurs sur la carte avec un zoom adapté 234 */ 235function showAllMarkers(mapId) { 236 var map = googleMaps[mapId]; 237 if (!map) return; 238 239 var markers = googleMapsMarkers[mapId]; 240 if (markers.length === 0) return; 241 242 var bounds = new google.maps.LatLngBounds(); 243 244 // Ãtendre les limites pour inclure tous les marqueurs 245 markers.forEach(function(marker) { 246 bounds.extend(marker.getPosition()); 247 }); 248 249 // Ajuster la vue pour inclure tous les marqueurs 250 if (markers.length === 1) { 251 // Si un seul marqueur, centrer sur lui avec le zoom par défaut 252 map.setCenter(bounds.getCenter()); 253 map.setZoom(15); 254 } else { 255 // Si plusieurs marqueurs, ajuster la vue pour tous les inclure 256 map.fitBounds(bounds); 257 258 // Limiter le zoom maximum 259 google.maps.event.addListenerOnce(map, 'bounds_changed', function() { 260 var mapElement = document.getElementById(mapId); 261 var zoom = parseInt(mapElement.dataset.zoom) || 10; 262 map.setZoom(zoom); 263 }); 264 } 265} 266 267/** 268 * Met en surbrillance l'élément de liste sélectionné 269 */ 270function highlightAddressItem(selectedItem) { 271 // Retirer la classe active de tous les éléments 272 var allItems = document.querySelectorAll('.address-item'); 273 allItems.forEach(function(item) { 274 item.classList.remove('active'); 275 }); 276 277 // Ajouter la classe active à l'élément sélectionné 278 selectedItem.classList.add('active'); 279 280 // Scroll vers l'élément si nécessaire 281 selectedItem.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); 282} 283 284/** 285 * Ajoute les marqueurs à la carte 286 */ 287function addMarkersToMap(mapId, markers) { 288 var map = googleMaps[mapId]; 289 if (!map) { 290 console.error('Carte non trouvée : ' + mapId); 291 return; 292 } 293 294 // Supprimer les marqueurs existants 295 googleMapsMarkers[mapId].forEach(function(marker) { 296 marker.setMap(null); 297 }); 298 googleMapsMarkers[mapId] = []; 299 300 // Créer les nouveaux marqueurs 301 var bounds = new google.maps.LatLngBounds(); 302 var hasMarkers = false; 303 304 markers.forEach(function(markerData) { 305 var position = { lat: markerData.latitude, lng: markerData.longitude }; 306 307 var marker = new google.maps.Marker({ 308 position: position, 309 map: map, 310 title: markerData.name.replaceAll("/R", ""), 311 animation: google.maps.Animation.DROP, 312 icon: { 313 url: 'https://www.ebtrans.com/wp-content/plugins/wordpress-google-maps-plugin/assets/marqueur-b.png', 314 scaledSize: new google.maps.Size(40, 40), 315 origin: new google.maps.Point(0, 0), 316 anchor: new google.maps.Point(20, 40) 317 } 318 }); 319 320 // Stocker les données du marqueur 321 marker.markerData = markerData; 322 323 // Créer la fenêtre d'information 324 var infoContent = '<div class="google-maps-info-window">' + 325 '<h3>' + markerData.name.replaceAll("/R", "") + '</h3>' + 326 '<p>' + markerData.address.replaceAll("/R", "<br>") + '</p>'; 327 328 if (markerData.description) { 329 infoContent += '<p>' + markerData.description.replaceAll("/R", "<br>") + '</p>'; 330 } 331 332 infoContent += '</div>'; 333 334 var infoWindow = new google.maps.InfoWindow({ 335 content: infoContent 336 }); 337 338 // Ajouter l'événement de clic sur le marqueur 339 marker.addListener('click', function() { 340 // Fermer toutes les autres fenêtres d'information
341 googleMapsMarkers[mapId].forEach(function(otherMarker) { 342 if (otherMarker.infoWindow) { 343 otherMarker.infoWindow.close(); 344 } 345 }); 346 347 // Ouvrir cette fenêtre d'information 348 infoWindow.open(map, marker); 349 350 // Mettre en surbrillance l'élément de liste correspondant 351 var addressItem = document.querySelector('.address-item[data-marker-id="' + markerData.id + '"]'); 352 if (addressItem) { 353 highlightAddressItem(addressItem); 354 scrollToMap(mapId); 355 } 356 }); 357 358 // Stocker la référence de la fenêtre d'information 359 marker.infoWindow = infoWindow; 360 361 // Ajouter le marqueur à la liste 362 googleMapsMarkers[mapId].push(marker); 363 364 // Ãtendre les limites pour inclure ce marqueur 365 bounds.extend(position); 366 hasMarkers = true; 367 }); 368 369 // Ajuster automatiquement la vue pour afficher tous les marqueurs au chargement 370 if (hasMarkers) { 371 showAllMarkers(mapId); 372 } 373} 374 375// Initialiser les cartes quand le DOM est prêt 376jQuery(document).ready(function($) { 377 // Si l'API Google Maps est déjà chargée, initialiser immédiatement 378 if (typeof google !== 'undefined' && google.maps) { 379 initGoogleMap(); 380 } 381 382 // Gérer le redimensionnement de la fenêtre 383 $(window).on('resize', function() { 384 Object.keys(googleMaps).forEach(function(mapId) { 385 google.maps.event.trigger(googleMaps[mapId], 'resize'); 386 }); 387 }); 388 389});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.