很好地关闭其他信息窗口 [英] close other infowindows nicely

查看:58
本文介绍了很好地关闭其他信息窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在这样做,以便为我的Google地图创建标记.

Im currently doing this to create markers for my google map.

function createMarker(posn, title, html) {
            var marker = new google.maps.Marker({ position: posn, title: title, draggable: false });
            marker['infowindow'] = new google.maps.InfoWindow({ content: html });

            infoWindows.push(marker['infowindow']);
            google.maps.event.addListener(marker, "click", function () {
                for (i = 0; i < infoWindows.length; i++) {
                    infoWindows[i].close();
                }
                this['infowindow'].open(map, this);
            });
            return marker;
        }

我对for循环不满意,为了关闭标记,我知道可以使用一个引用来完成与此类似的事情:

im not happy with the for loop, for closing the markers, i know something similar to this can be done by using one refernce:

如果(infowindow)infowindow.close();

if (infowindow) infowindow.close();

我使用上述代码的原因是因为我在做类似的事情
markers[myPoint]['infowindow'].open(map, markers[myPoint]);否则,(myPoint是一个数字).

the reason I am using code like above is because i am doing something like
markers[myPoint]['infowindow'].open(map, markers[myPoint]); else where, (myPoint is a number).

如何避免此for循环关闭打开的信息窗口并以一种不错的方式做到这一点?

how can i avoid this for loop to close open infowindows and do it the nice way?

推荐答案

仅将最后打开的infoWindow存储在全局变量中:

Just store last opened infoWindow in a global variable:

var activeInfoWindow;

function createMarker(posn, title, html) {
    var marker = new google.maps.Marker({ position: posn, title: title, draggable: false });
    marker['infowindow'] = new google.maps.InfoWindow({ content: html });

    infoWindows.push(marker['infowindow']);
    google.maps.event.addListener(marker, "click", function () {
        if ( activeInfoWindow == this['infowindow'] ) {
            return;
        }
        if ( activeInfoWindow ) {
            activeInfoWindow.close();
        }

        this['infowindow'].open(map, this);
        activeInfoWindow = this['infowindow'];
    });
    return marker;
}

这篇关于很好地关闭其他信息窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆