用于创建唯一信息窗口的Google Map API问题

时间:2017-11-26 04:49:59

标签: javascript google-maps-api-3

也许现在已经很晚了,但我在谷歌地图上显示信息窗口时遇到了问题。我没有兴趣的测试点,当我为这些标记添加标记和点击事件时,我总是会获得位置5的弹出窗口。如何将各个信息窗口正确地绑定到每个标记?我尝试创建信息窗口和标记,并将它们存储在pointsOfInterests数组中,但这似乎没有帮助。

谢谢你, [R

 var pointsOfInterests = [
    ['Location 1', 43.386815, -79.8142324, 1 , '<div id="content1"><div id="siteNotice1"></div><div id="bodyContent1"><h1 id="firstHeading1" class="firstHeading">l1</h1></div></div>'],
    ['Location 2', 43.367015, -79.8241324, 2 , '<div id="content2"><div id="siteNotice2"></div><div id="bodyContent2"><h1 id="firstHeading2" class="firstHeading">l2</h1></div></div>'],
    ['Location 3', 43.357015, -79.8341324, 3, '<div id="content3"><div id="siteNotice3"></div><div id="bodyContent3"><h1 id="firstHeading3" class="firstHeading">l3</h1></div></div>'],
    ['Location 4', 43.347015, -79.8541324, 4, '<div id="content4"><div id="siteNotice4"></div><div id="bodyContent4"><h1 id="firstHeading4" class="firstHeading">l4</h1></div></div>'],
    ['Location 5', 43.377015, -79.8341324, 5, '<div id="content5"><div id="siteNotice5"></div><div id="bodyContent5"><h1 id="firstHeading5" class="firstHeading">l5</h1></div></div>'],
  ];


 function setMarkers(map) {
    // Adds markers to the map.
    for (var i = 0; i < pointsOfInterests.length; i++) {

     var pointsOfInterest = pointsOfInterests[i];

     var marker = new google.maps.Marker({
        position: {lat: pointsOfInterest[1], lng: pointsOfInterest[2]},
        map: map,
        title: pointsOfInterest[0],
        zIndex: pointsOfInterest[3]
      });

      marker.addListener('click', function() {
         var infoWindow = new google.maps.InfoWindow({
         content: pointsOfInterest[0]
         });

         var pos = {
           lat: pointsOfInterest[1],
           lng: pointsOfInterest[2]
         };

         infoWindow.setPosition(pos);
         infoWindow.open(map, marker);
      });
    }
  }

1 个答案:

答案 0 :(得分:1)

您必须创建一个闭包,否则,所有侦听器都将使用数组中的最后一个值。

var pointsOfInterests = [
['Location 1', 43.386815, -79.8142324, 1 , '<div id="content1"><div id="siteNotice1"></div><div id="bodyContent1"><h1 id="firstHeading1" class="firstHeading">l1</h1></div></div>'],
['Location 2', 43.367015, -79.8241324, 2 , '<div id="content2"><div id="siteNotice2"></div><div id="bodyContent2"><h1 id="firstHeading2" class="firstHeading">l2</h1></div></div>'],
['Location 3', 43.357015, -79.8341324, 3, '<div id="content3"><div id="siteNotice3"></div><div id="bodyContent3"><h1 id="firstHeading3" class="firstHeading">l3</h1></div></div>'],
['Location 4', 43.347015, -79.8541324, 4, '<div id="content4"><div id="siteNotice4"></div><div id="bodyContent4"><h1 id="firstHeading4" class="firstHeading">l4</h1></div></div>'],
['Location 5', 43.377015, -79.8341324, 5, '<div id="content5"><div id="siteNotice5"></div><div id="bodyContent5"><h1 id="firstHeading5" class="firstHeading">l5</h1></div></div>'],
];


function setMarkers(map) {
    // Adds markers to the map.
    for (var i = 0; i < pointsOfInterests.length; i++) {
        (function(index){
            var pointsOfInterest = pointsOfInterests[index];

            var marker = new google.maps.Marker({position: {lat: pointsOfInterest[1], lng: pointsOfInterest[2]},
                            map: map,
                            title: pointsOfInterest[0],
                            zIndex: pointsOfInterest[3]
                         });

             marker.addListener('click', function() {
                var infoWindow = new google.maps.InfoWindow({
                content: pointsOfInterest[0]
             });

                var pos = {
                  lat: pointsOfInterest[1],
                  lng: pointsOfInterest[2]
                };

                infoWindow.setPosition(pos);
                infoWindow.open(map, marker);
             });
         })(i);
    }
}