谷歌只映射一个信息窗口



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

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

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

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

最新更新