Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建唯一信息窗口的Google地图API问题_Javascript_Google Maps Api 3 - Fatal编程技术网

Javascript 创建唯一信息窗口的Google地图API问题

Javascript 创建唯一信息窗口的Google地图API问题,javascript,google-maps-api-3,Javascript,Google Maps Api 3,也许只是时间晚了,但我在谷歌地图上显示信息窗口时遇到了问题。我有几个感兴趣的测试点,当我添加标记并单击这些标记的事件时,我总是会看到位置5的弹出窗口。如何正确地将各个信息窗口绑定到每个标记?我尝试创建信息窗口和标记,并将它们存储在兴趣点数组中,但似乎没有帮助 谢谢,, R var pointsofinterest=[ [位置1',43.386815,-79.8142324,1',l1'], [位置2',43.367015,-79.8241324,2',l2'], [位置3',43.357015,

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

谢谢,, R

var pointsofinterest=[
[位置1',43.386815,-79.8142324,1',l1'],
[位置2',43.367015,-79.8241324,2',l2'],
[位置3',43.357015,-79.8341324,3',l3'],
[位置4',43.347015,-79.8541324,4',l4'],
[位置5',43.377015,-79.8341324,5',l5'],
];
函数设置标记(map){
//将标记添加到地图中。
对于(变量i=0;i
必须创建闭包,否则,所有侦听器都将使用数组中的最后一个值

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);
    }
}
var pointsofinterest=[
[位置1',43.386815,-79.8142324,1',l1'],
[位置2',43.367015,-79.8241324,2',l2'],
[位置3',43.357015,-79.8341324,3',l3'],
[位置4',43.347015,-79.8541324,4',l4'],
[位置5',43.377015,-79.8341324,5',l5'],
];
函数设置标记(map){
//将标记添加到地图中。
对于(变量i=0;i
成功了谢谢
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);
    }
}