javascript 谷歌地图 api v3 信息窗口中的动态内容在多个自定义标记上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11518388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:29:45  来源:igfitidea点击:

Dynamic content in google maps api v3 info windows on multiple custom markers

javascriptgoogle-mapsgoogle-maps-api-3

提问by Steve Smith

I have the below js that implements a google map with a looped through set of custom markers. I need each marker to have a different infowindow so when you click the marker you get the content that is relevant. At the minute it doesn't open the infowindow and doesn't give me an error in console

我有下面的js,它实现了一个谷歌地图,其中包含一组循环的自定义标记。我需要每个标记都有不同的信息窗口,因此当您单击标记时,您会获得相关的内容。目前它没有打开信息窗口,也没有在控制台中给我一个错误

infowindow = new google.maps.InfoWindow();

    for(var i=0; i<google_map_item.length; i++)
    {
        latlon = new google.maps.LatLng(google_map_item[i].lat, google_map_item[i].lon)
        bounds.extend(latlon);
        var iconcolor = google_map_item[i].iconColor;
        marker = new google.maps.Marker({
            map: map,
            position: latlon,
            icon: "https://chart.googleapis.com/chart?chst=d_map_pin_letter_withshadow&chld=" + (i + 1) + "|"+iconcolor+"|000000",
            type: 'flat',
            icon_color: '#ff0000', 
            label_color: '#ffffff', 
            width: '20', 
            height: '20', 
            label_size: '11',
                            clickable: true
        });

                    google.maps.event.addListener(marker, 'click', function() {
                        //marker.info.open(map, this);
                        infowindow.setContent(this.latlon);
                        infowindow.open(map, this);
                    });


        map.fitBounds(bounds);
    }

回答by Anup

var marker3 = new google.maps.Marker({
          map: map,                                                        
          icon: 'miniMarker.png',
          info: destArr[i][9],
          position: new google.maps.LatLng(destArr[i][7], destArr[i][8])
});
var infowindow3 = new google.maps.InfoWindow();
google.maps.event.addListener(marker3, 'mouseover', function () {
        infowindow3.setContent(this.info);                              
        infowindow3.open(map, this);                        
});

Working example with more then 20 markers and info window

具有 20 多个标记和信息窗口的工作示例

回答by Steve Smith

The reference in the marker array was incorrect as outlined in this similar question

如此类似问题中所述,标记数组中的引用不正确

Google Maps - Multiple markers - 1 InfoWindow problem

谷歌地图 - 多个标记 - 1 InfoWindow 问题