Bonjour à tous,

d'avance merci pour votre aide. J'ai suivi le tutoriel vidéo Google MAPS API. Je me suis aidé des docs officiels Google.

Je souhaite afficher une maps présentant plusieurs markers préalablement créés en BDD avec du php basique.
Jusqu'ici aucun problème, les markers s'affichent.

Là où je ne sais plus, c'est lorsque je souhaite afficher un contenu spécifique pour chaque markers.

-> Je clique sur un marker -> l'ID, le titre, l'url s'affiche soit sur la map avec une window info soit dans une div.

Je ne sais pas comment afficher ce même contenu.

Côté script :around

      <script type="text/javascript">
function initialize() {
  var mapOptions = {
    zoom: 5,
    center: new google.maps.LatLng(47.081012, 2.398781999999983)
  }
  var map = new google.maps.Map(document.getElementById('maps'),
                                mapOptions);

  setMarkers(map, beaches);
}

/**
 * Data for the markers consisting of a name, a LatLng and a zIndex for
 * the order in which these markers should display on top of each
 * other.
 */
var beaches = [
<?php
        $query=$db->prepare('SELECT * FROM maps');
        $query->execute();
        while ($data=$query->fetch()) {
?>
  ['<?php echo $data['adresse']; ?>', <?php echo $data['lat']; ?>, <?php echo $data['lng']; ?>, <?php echo $data['id']; ?>],
<?php } ?>
];

function setMarkers(map, locations) {
  // Add markers to the map

  // Marker sizes are expressed as a Size of X,Y
  // where the origin of the image (0,0) is located
  // in the top left of the image.

  // Origins, anchor positions and coordinates of the marker
  // increase in the X direction to the right and in
  // the Y direction down.
  var image = {
    url: 'images/beachflag.png',
    // This marker is 20 pixels wide by 32 pixels tall.
    size: new google.maps.Size(20, 32),
    // The origin for this image is 0,0.
    origin: new google.maps.Point(0,0),
    // The anchor for this image is the base of the flagpole at 0,32.
    anchor: new google.maps.Point(0, 32)
  };
  // Shapes define the clickable region of the icon.
  // The type defines an HTML &lt;area&gt; element 'poly' which
  // traces out a polygon as a series of X,Y points. The final
  // coordinate closes the poly by connecting to the first
  // coordinate.
  var shape = {
      coords: [1, 1, 1, 20, 18, 20, 18 , 1],
      type: 'poly'
  };
  for (var i = 0; i < locations.length; i++) {
    var beach = locations[i];
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image,
        shape: shape,
        title: beach[0],
        zIndex: beach[3]
    });
        google.maps.event.addListener(marker,'click',function(){
        $('.place').hide();
        $('.place'+beach[3]).slideDown;
    });
  }
}
google.maps.event.addDomListener(window, 'load', initialize);
      </script>

Côté html/php

<?php
$query=$db->prepare('SELECT * FROM maps');
$query->execute();
while ($data=$query->fetch()) {
?>
<div class="place place<?php echo $data['id']; ?>" style="display:none;"><?php echo $data['adresse']; ?></div>
<?php } ?>
<div id="maps" style="width:100%; height:350px;"></div>

Merci d'avance pour vos pistes et vos aides !

WebiK.

2 réponses


Hello, si j'ai bien compris ton problème, il faut utiliser la fonction InfoWindow().

 <script type="text/javascript">
                    google.maps.event.addListener(marker, "click", (function(marker, i){
                        return function(){
                            infowindow.setContent("Ton contenu HTML");

                            infowindow.open(map, marker);
                        }

                    })(marker, i));
    </script>

Ah super c'est déjà un bon point pour moi. Merci ! Néanmoins, je voudrais pouvoir récuperer dans "Ton contenu HTML" le contenu d'un des marker sur lequel le clique. Selon la doc google, il récupere l'id avec beach(3) ... Je ne sais pas ou placer le tout.
Merci de ton aide.