Google maps api 3 谷歌地图API中的多信息窗口

Google maps api 3 谷歌地图API中的多信息窗口,google-maps-api-3,google-maps-markers,Google Maps Api 3,Google Maps Markers,我正在尝试为多个标记创建多个infowindow,但似乎只参考了上一个infowindow 这是我的代码(在for循环中,_lat和_long是唯一值): marker.addListener('click',function(){ //markerInfoWindow.close(); markerInfoWindow.setContent(“”,true); markerInfoWindow.open(map,this); }); marker.setMap(map); 我使用marke

我正在尝试为多个标记创建多个infowindow,但似乎只参考了上一个infowindow

这是我的代码(在for循环中,_lat和_long是唯一值):

marker.addListener('click',function(){
//markerInfoWindow.close();
markerInfoWindow.setContent(“”,true);
markerInfoWindow.open(map,this);
}); 
marker.setMap(map);
我使用marker进行了测试,marker显示正确,但所有这些都链接到最后一个信息窗口

如何为每个标记创建新的信息窗口

更新:

这是我更新后的全部功能:

        function getListPosition(listMarkers, map){
            db.transaction(function(tx){
                tx.executeSql('SELECT * FROM Position', [], function(tx, rs) {
                    var numberOfItems = rs.rows.length; 

                    var markerInfoWindow = new google.maps.InfoWindow();
                    var marker;

                    for (var i = 0; i < numberOfItems; i++) {

                        var _lat = rs.rows.item(i).Lat;
                        var _long = rs.rows.item(i).Long;

                        marker = new google.maps.Marker({
                            position: {lat: _lat , lng: _long}
                        });

                        listMarkers.push(marker);

                        google.maps.event.addListener(marker, 'click', (function(marker) {
                            return function() {
                                markerInfoWindow.setContent('<a href="#" onclick="onUnsavePlace(' + _lat + ',' + _long + ');">Unsave place</a>', true);
                                markerInfoWindow.open(map, marker);
                            }
                        })(marker));

                        // marker.addListener('click', function(){
                        //     //markerInfoWindow.close();
                        //     markerInfoWindow.setContent('<a href="#" onclick="onUnsavePlace(' + _lat + ',' + _long + ');">Unsave place</a>', true);
                        //     markerInfoWindow.open(map, this); 
                        // }); 

                        marker.setMap(map); 

                        // Set map center to the last marker
                        if (i == numberOfItems - 1){
                            map.setCenter({lat: _lat, lng: _long});
                        } 

                    }

                });
            }, function(err){
                console.log('Error when get list position. Error code: ' + err.code);
            });
        }
函数getListPosition(listMarkers,map){ 数据库事务(功能(tx){ tx.executeSql('SELECT*FROM Position',[],函数(tx,rs){ var numberOfItems=rs.rows.length; var markerInfoWindow=new google.maps.InfoWindow(); var标记; 对于(变量i=0;i当click listener函数运行时,需要对循环变量执行函数闭包。在您的情况下,
\u lat
\u lng
。从标记(
this
)获取这些可能是最简单的方法,不过:

google.maps.event.addListener(标记,'click',函数(evt){
markerInfoWindow.setContent(“”,true);
markerInfoWindow.open(map,this);
});

(未测试)

它的可能副本不适用于我的代码您需要在
\u lat
\u lng
以及标记器(或从标记器获取)上关闭函数。如果没有帮助,请提供一个例子来演示这个问题。你能解释一下这个语法吗:
(函数(标记,i){return function(){infowindow.setContent(locations[i][0]);infowindow.open(map,marker);}})(标记,i)
哦,它可以工作。但是为什么它不能与此一起工作:
marker.addListener('click',function(){//markerInfoWindow.close();markerInfoWindow.setContent('',true);markerInfoWindow.open(map,this);})
因为正如我在回答和评论中所说的,在
\u lat
\u long
上没有函数闭包。我的错误!非常感谢。
        function getListPosition(listMarkers, map){
            db.transaction(function(tx){
                tx.executeSql('SELECT * FROM Position', [], function(tx, rs) {
                    var numberOfItems = rs.rows.length; 

                    var markerInfoWindow = new google.maps.InfoWindow();
                    var marker;

                    for (var i = 0; i < numberOfItems; i++) {

                        var _lat = rs.rows.item(i).Lat;
                        var _long = rs.rows.item(i).Long;

                        marker = new google.maps.Marker({
                            position: {lat: _lat , lng: _long}
                        });

                        listMarkers.push(marker);

                        google.maps.event.addListener(marker, 'click', (function(marker) {
                            return function() {
                                markerInfoWindow.setContent('<a href="#" onclick="onUnsavePlace(' + _lat + ',' + _long + ');">Unsave place</a>', true);
                                markerInfoWindow.open(map, marker);
                            }
                        })(marker));

                        // marker.addListener('click', function(){
                        //     //markerInfoWindow.close();
                        //     markerInfoWindow.setContent('<a href="#" onclick="onUnsavePlace(' + _lat + ',' + _long + ');">Unsave place</a>', true);
                        //     markerInfoWindow.open(map, this); 
                        // }); 

                        marker.setMap(map); 

                        // Set map center to the last marker
                        if (i == numberOfItems - 1){
                            map.setCenter({lat: _lat, lng: _long});
                        } 

                    }

                });
            }, function(err){
                console.log('Error when get list position. Error code: ' + err.code);
            });
        }
google.maps.event.addListener(marker, 'click', function(evt) {
  markerInfoWindow.setContent('<a href="#" onclick="onUnsavePlace(' + this.getPosition().lat() + ',' + this.getPosition().lng() + ');">Unsave place</a>', true);
  markerInfoWindow.open(map, this);
});