From b401e1ef892b9094a7eb7d9a02534890bdbb33f8 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Thu, 2 Oct 2025 15:39:20 +0200 Subject: [PATCH] FEATURE Optimizing behaviour --- .../src/localisation-map/view.js | 106 +++++++++++++++++- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/plugins/carhop-blocks/blocks/localisation-map/src/localisation-map/view.js b/plugins/carhop-blocks/blocks/localisation-map/src/localisation-map/view.js index c25bbc5..06acf9f 100644 --- a/plugins/carhop-blocks/blocks/localisation-map/src/localisation-map/view.js +++ b/plugins/carhop-blocks/blocks/localisation-map/src/localisation-map/view.js @@ -14,7 +14,8 @@ const GOOGLE_MAPS_CONFIG = { lat: 50.61036915386491, lng: 4.151615298397132, // Position originale du pin title: "Carhop", - // Options de couleur : 'red', 'blue', 'green', 'yellow', 'purple', 'orange' + address: "Av. de la Houssière 80, 7090 Braine-le-Comte", + placeId: "ChIJ41pZLUq1w0cRjXw8zlHnvXI", // Place ID Google Maps (à remplacer par le vrai) color: "green", }, styles: [ @@ -423,11 +424,48 @@ function initializeCarhopMaps() { streetViewControl: false, // Contrôle Street View fullscreenControl: false, // Bouton plein écran }); + + // Créer une InfoWindow pour afficher les informations + const infoWindow = new google.maps.InfoWindow(); + + // Optionnel : Utiliser le Place ID pour obtenir les détails du lieu + const service = new google.maps.places.PlacesService(map); + const placeId = GOOGLE_MAPS_CONFIG.marker.placeId; + let placeDetails = null; + + if (placeId && placeId !== "ChIJXXXXXXXXXXXXXXXXXXXX") { + service.getDetails( + { + placeId: placeId, + fields: [ + "name", + "geometry", + "formatted_address", + "photos", + "rating", + "formatted_phone_number", + "website", + "opening_hours", + ], + }, + (place, status) => { + if ( + status === google.maps.places.PlacesServiceStatus.OK && + place.geometry + ) { + // Centrer la carte sur le lieu exact + map.setCenter(place.geometry.location); + placeDetails = place; + console.log("Lieu trouvé:", place.name, place.formatted_address); + } + }, + ); + } // Chemin vers votre pin personnalisé (à ajuster selon votre structure) const image = "/wp-content/plugins/carhop-blocks/blocks/localisation-map/build/localisation-map/carhop-pin.png"; // Add marker with custom color - new google.maps.Marker({ + const marker = new google.maps.Marker({ position: new google.maps.LatLng( GOOGLE_MAPS_CONFIG.marker.lat, GOOGLE_MAPS_CONFIG.marker.lng, @@ -437,6 +475,68 @@ function initializeCarhopMaps() { icon: image, }); + // Add click event to show InfoWindow + marker.addListener("click", function () { + let content = `
`; + + if (placeDetails) { + // Utiliser les détails du lieu Google Maps + content += `

${ + placeDetails.name || GOOGLE_MAPS_CONFIG.marker.title + }

`; + + if (placeDetails.formatted_address) { + content += `

Adresse: ${placeDetails.formatted_address}

`; + } + + if (placeDetails.formatted_phone_number) { + content += `

Téléphone: ${placeDetails.formatted_phone_number}

`; + } + + if (placeDetails.rating) { + content += `

Note: ${placeDetails.rating}/5 ⭐

`; + } + + if (placeDetails.website) { + content += `

Site web

`; + } + + // Bouton pour ouvrir dans Google Maps + content += `
+ + Voir sur Google Maps + +
`; + } else { + // Fallback avec les informations de base + content += `

${GOOGLE_MAPS_CONFIG.marker.title}

`; + content += `

Adresse: ${GOOGLE_MAPS_CONFIG.marker.address}

`; + + // Bouton pour ouvrir dans Google Maps + const address = encodeURIComponent(GOOGLE_MAPS_CONFIG.marker.address); + content += `
+ + Voir sur Google Maps + +
`; + } + + content += `
`; + + infoWindow.setContent(content); + infoWindow.open(map, marker); + }); + + // Optionnel : Ouvrir automatiquement l'InfoWindow au chargement + // Décommentez les lignes suivantes si vous voulez ouvrir le popup automatiquement + /* + setTimeout(() => { + marker.trigger('click'); + }, 1500); // Attendre 1.5 secondes après le chargement + */ + // Mark as initialized mapElement.dataset.initialized = "true"; }); @@ -474,7 +574,7 @@ function loadGoogleMapsAPI() { // Load Google Maps API const script = document.createElement("script"); - script.src = `https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAPS_CONFIG.apiKey}&callback=carhopMapsReady`; + script.src = `https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAPS_CONFIG.apiKey}&libraries=places&callback=carhopMapsReady`; script.async = true; script.defer = true; document.head.appendChild(script);