6
votes

API V3 Google Maps Ajout de plusieurs marqueurs avec info Windows W / Texte personnalisé

Je fais un site Web sur les cyclistes tués en Norvège. Pour mon projet, j'ai utilisé Google Maps API V3, mais j'ai une familiarité vague avec JavaScript. Vous pouvez voir mon résultat jusqu'à présent ici: http://salamatustudios.com/googlemapstest/

fondamentalement, je veux avoir plusieurs marqueurs avec des info-outils sur chacun. Chacune des info-infoques contiendra: Nom Age), Emplacement, Date de décès, En savoir plus (qui sera lié à une page sur le site Web lui-même). P>

Comme cet exemple ici: http: // salamatstudios .com / Bicyclettes / P>

J'ai essayé de travailler avec un seul marqueur et un info-info-info-annonce et cela a bien fonctionné. Lorsque je souhaite ajouter de nouveaux marqueurs avec des fenêtres d'information personnalisées sur chacun, je suis coincé. Pour le moment, j'ai 3 marqueurs sur différents emplacements comme indiqué dans le premier lien, mais aucune des fenêtres d'informations n'apparaît lorsque je clique sur le marqueur. P>

Comment puis-je le faire pour le coder pour le coder pour les infoousses apparaître? Et comment puis-je avoir du texte personnalisé dans chaque info-annonce? Je vais avoir environ 30 à 40 marqueurs sur la carte quand c'est fait. Toutes les fenêtres d'information auront différents types d'informations. P>

function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(65.18303, 20.47852),
      zoom: 5,
      mapTypeId: google.maps.MapTypeId.ROADMAP,


      // MAP CONTROLS (START)
      mapTypeControl: true,

      panControl: true,
      panControlOptions: {
      position: google.maps.ControlPosition.TOP_RIGHT
      },

      zoomControl: true,
      zoomControlOptions: {
      style: google.maps.ZoomControlStyle.LARGE,
      position: google.maps.ControlPosition.LEFT_TOP
      },

      streetViewControl: true,
      streetViewControlOptions: {
      position: google.maps.ControlPosition.LEFT_TOP
      },
      // MAP CONTROLS (END)



    };
    var map = new google.maps.Map(document.getElementById("map"),
        mapOptions);


    // -------------- MARKER 1
    var marker1 = new google.maps.Marker({
    position: new google.maps.LatLng(59.96384, 11.04120),
    map: map,
    icon: 'img/bike5.png'
    });


    // MARKER 1'S INFO WINDOW
    var infowindow1 = new google.maps.InfoWindow({
    content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
    });
    // End of infowindow code

    // Adding a click event to the marker
    google.maps.event.addListener(marker1, 'click', function() {
    // Calling the open method of the infoWindow
    infowindow1.open(map, marker);
    });
    // -------- END OF 1st MARKER


    // -------------- MARKER 2
    var marker2 = new google.maps.Marker({
    position: new google.maps.LatLng(60.63040, 8.56102),
    map: map,
    icon: 'img/bike5.png'
    });

    // MARKER 2'S INFO WINDOW
    var infowindow2 = new google.maps.InfoWindow({
    content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
    });
    // End of infowindow code

    // Adding a click event to the marker
    google.maps.event.addListener(marker2, 'click', function() {
    // Calling the open method of the infoWindow
    infowindow2.open(map, marker);
    });
    // -------- END OF 2nd MARKER


    // -------------- MARKER 3
    var marker3 = new google.maps.Marker({
    position: new google.maps.LatLng(60.39126, 5.32205),
    map: map,
    icon: 'img/bike5.png'
    });

    // MARKER 3'S INFO WINDOW
    var infowindow3 = new google.maps.InfoWindow({
    content: 'Name<br />Location<br />Date<br /><br /><a href="http://www.db.no" target="_blank">Read more(test link)</a>'
    });
    // End of infowindow code

    // Adding a click event to the marker
    google.maps.event.addListener(marker3, 'click', function() {
    // Calling the open method of the infoWindow
    infowindow3.open(map, marker);
    });
    // -------- END OF 3rd MARKER


  }
  google.maps.event.addDomListener(window, 'load', initialize);


0 commentaires

3 Réponses :


9
votes

Vous devez fixer l'info-annonce aux bons marqueurs. Actuellement, ils sont tous associés au "marqueur", qui n'existent pas (et devrait provoquer un message d'erreur dans la console JavaScript lorsque vous cliquez sur les marqueurs).

à l'intérieur du clic ADMISTEUR Changez: P>

infowindow1.open(map, marker1);

infowindow2.open(map, marker2);

infowindow3.open(map, marker3);


4 commentaires

Wow! Cela a fonctionné instantanément. Merci beaucoup. Il existe également un moyen de disposer d'une fonction qui ferme la dernière infoWowindow lorsque vous cliquez sur le marqueur suivant et ouvrez une nouvelle infoWindow?


Vous voulez dire comme Ceci (traduit en v3 de l'exemple dans Tutoriel V2 de Mike Williams ) (Je pense à cela comme" comportement Infrowindow "V2")? Si vous voulez seulement une info-info-info-annonce à la fois, partagez la même infoWindow pour tous les marqueurs. Si cela répondit à votre question, s'il vous plaît Acceptez-le


Merci encore! Je l'ai testé pendant un moment et j'ai beaucoup échoué, mais je l'ai eu pour travailler comme je le voulais à la fin. Merci pour votre temps, Geocodezip :-)


Pour quelqu'un d'autre qui a le même problème au même problème à l'avenir, ils peuvent regarder ceci: salamatstudios.com/bicycles24/a >



1
votes
 google.maps.event.addListener(marker1, 'click', function() {
    // Calling the open method of the infoWindow
    infowindow1.open(map, this);
 });

0 commentaires

2
votes

En plus de Hoanghieu Réponse Lorsque vous utilisez une boucle, il est préférable de l'utiliser de cette façon:

marker.info = new google.maps.InfoWindow({
  content: 'some text'
});

google.maps.event.addListener(marker, 'click', function() {
  this.info.open(map, this);
});


1 commentaires

C'est en fait une approche plus intelligente au lieu d'instantiquer InfroWindow1 , Infowindow2 , etc.