Opening only a infobox at a time when multiple markers are displayed on the map

柔情痞子 提交于 2019-12-04 21:49:32

You declare a new infobox every time you create a marker. So 'ib' refers to the infobox created for that marker and not the others.

You need to set the infobox variable outside the createMarker function scope. Then inside your event listener, close the old infobox and then create a new one.

var ib;

function createMarker(<params>) {
     google.maps.event.addListener(marker, "click", function (e) {
        if (typeof ib === 'object') {
            ib.close();
        }
        ib = new Infobox();
        ib.setOptions(myOptions_click);
        ib.open(map, this);
    });
}

If you need a new infobox for each marker, then you could store an array of infoboxes.

var ibs = [];

var closeInfoBox = function() {
    for (var i in ibs) {
        ibs[i].close();
    }
}

function createMarker(<params>) {
    var ibIndex = ibs.push(new Infobox()) - 1,
        ib = ibs[ibIndex];

    google.maps.event.addListener(marker, "click", function (e) {
        closeInfoBox();
        ib.setOptions(myOptions_click);
        ib.open(map, this);
    });

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!