Google Maps API v3 adding multiple markers w/ info windows w/ custom text

后端 未结 3 936
既然无缘
既然无缘 2020-12-20 21:25

I am making a website over cyclists killed in Norway. For my project I have been using google maps api v3, but I have vague familiarity with javascript. You can see my resul

相关标签:
3条回答
  • 2020-12-20 21:43
     google.maps.event.addListener(marker1, 'click', function() {
        // Calling the open method of the infoWindow
        infowindow1.open(map, marker);
        });
    

    change to

     google.maps.event.addListener(marker1, 'click', function() {
        // Calling the open method of the infoWindow
        infowindow1.open(map, this);
     });
    
    0 讨论(0)
  • 2020-12-20 21:49

    You need to attach the infowindow to the correct markers. Currently they are all associated with "marker", which doesn't exist (and should cause an error message in the javascript console when you click on the markers).

    Inside the click listener change:

    infowindow1.open(map, marker);
    
    infowindow2.open(map, marker);
    
    infowindow3.open(map, marker);
    

    To:

    infowindow1.open(map, marker1);
    
    infowindow2.open(map, marker2);
    
    infowindow3.open(map, marker3);
    

    working example

    0 讨论(0)
  • 2020-12-20 21:50

    In addition to HoangHieu Answer when you use for loop it better to use it this way:

    marker.info = new google.maps.InfoWindow({
      content: 'some text'
    });
    
    google.maps.event.addListener(marker, 'click', function() {
      this.info.open(map, this);
    });
    
    0 讨论(0)
提交回复
热议问题