【问题标题】:Google maps only one infoWindow谷歌只映射一个 infoWindow
【发布时间】:2019-12-02 19:25:52
【问题描述】:

我创建了一些代码来创建谷歌地图,它会提取一些标记数据。我将如何构造此代码以一次只打开一个信息窗口?回答越直接越好。

function initializeLocations(locations) {

        $.each(locations, function (i, location) {

            var marker = new google.maps.Marker({
                position: location.geo,
                map: map,
                title: location.name
            });

            var contentName = '<br><b>' + location.name + '</b><br>';
            var contentLink = '<a href="' + location.url + '">More Info</a>';
            var contentAddress = location.address;
            var content = contentName + contentAddress + contentLink

            var infoWindow = new google.maps.InfoWindow({
                content: content
            });

            marker.addListener('click', function() {
                infoWindow.open(map, this);
            });
                });
            }

【问题讨论】:

    标签: javascript maps infowindow


    【解决方案1】:

    我找到了解决方案。这就是我所做的。

    function initializeLocations(locations) {
        var infoWindow = new google.maps.InfoWindow({});
    
        $.each(locations, function (i, location) {
            var marker = new google.maps.Marker({
                position: location.geo,
                map: map,
                title: location.name
            });
    
            var contentName = '<br><b>' + location.name + '</b><br>';
            var contentLink = '<a href="' + location.url + '">More Info</a>';
            var contentAddress = location.address;
            var content = contentName + contentAddress + contentLink
    
            google.maps.event.addListener(marker, 'click', (function(marker) {
                return function(){
                    infoWindow.setContent(content);
                    infoWindow.open(map, marker);
                }
            })(marker));
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2011-09-14
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多