javascript - Google Map API 创建独特信息窗口时出现问题

标签 javascript google-maps-api-3

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

谢谢你,

 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);
      });
    }
  }

最佳答案

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

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);
    }
}

关于javascript - Google Map API 创建独特信息窗口时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47493160/

相关文章:

javascript - MVC 使用上下文在数据库中查找记录

javascript - vis.js Graph3d 条形图 - 可能的错误?

javascript - 带有子模块的 Typescript 定义文件

google-maps-api-3 - Google maps api v3 上标记的多条折线

android - 在 Android 中计算总行驶距离 Google Maps API V3

javascript - 如何使用 onclick 事件触发此代码。我尝试过但失败了

javascript - 如何在不在html上添加脚本标签的情况下使用google-maps-api-v3

ruby - 无法使用 Ruby 的 'open-uri' 打开 utf-8 URI

javascript - 错误处理不适用于 Chrome 中的 HTML5 地理定位

javascript - 使用 javascript 删除 'this' 单击的列表元素