Javascript 在谷歌地图中切换信息框

Javascript 在谷歌地图中切换信息框,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我在谷歌地图工作,并成功地实现了谷歌地图的信息框插件。现在我关心的是,我们如何知道标记的信息框是否处于打开状态。这样我可以在点击标记时切换它 var locations = [ //this is array of arrays ]; var map = new google.maps.Map(document.getElementById('map_canvas'),{ disableDefaultUI : true, zoom

我在谷歌地图工作,并成功地实现了谷歌地图的信息框插件。现在我关心的是,我们如何知道标记的信息框是否处于打开状态。这样我可以在点击标记时切换它

var locations = [
        //this is array of arrays
    ];

    var map = new google.maps.Map(document.getElementById('map_canvas'),{
        disableDefaultUI : true,
        zoom : 12,
        center : new google.maps.LatLng(defaultLatitude,defaultLongitude),
        mapTypeId : google.maps.MapTypeId.ROADMAP
    });


    var mapcode,myOptions;

    for (var i = 0,len = locations.length; i < len; i++) {
        var marker = add_marker(locations[i][1],locations[i][2],locations[i][3],'this is title',locations[i][0]);
        allMarkers.push(marker);
        marker.setMap(map);
    };

    function add_marker(lat,lng,icn,title,box_html) {

        var marker = new google.maps.Marker({
            animation : google.maps.Animation.DROP,
            position : new google.maps.LatLng(lat,lng),
            map : map,
            icon : icn
        }); 

        mapcode = '<this is the code of infobox to show>';

        myOptions = {
             //options of the infobox...bla bla
        };

        var ib = new InfoBox(myOptions);

        google.maps.event.addListener(marker, 'click', function() {
            ib.open(map, marker);
        });   
        return marker;
    }
var位置=[
//这是数组的数组
];
var map=new google.maps.map(document.getElementById('map_canvas'){
disableDefaultUI:true,
缩放:12,
中心:新的google.maps.LatLng(默认纬度,默认经度),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
变量映射码,myOptions;
对于(变量i=0,len=locations.length;i
我是新来的谷歌地图,所以可能我错过了一些非常小的东西…提前谢谢。。。。
Ankur

如果您需要随时打开一个信息框,您可以这样做:

    var ib = new InfoBox();
    ib.isOpen = false;
    function add_marker(lat,lng,icn,title,box_html) {

    var marker = new google.maps.Marker({
        animation : google.maps.Animation.DROP,
        position : new google.maps.LatLng(lat,lng),
        map : map,
        icon : icn
    }); 

    mapcode = '<this is the code of infobox to show>';

    myOptions = {
         //options of the infobox...bla bla
    };
    marker.ibOptions = myOptions;

    google.maps.event.addListener(marker, 'click', function() {
        ib.setOptions(marker.ibOptions);
        ib.open(map, marker);
        ib.isOpen = true;
    });   
    return marker;
}
var ib=new InfoBox();
ib.isOpen=假;
功能添加标记(lat、lng、icn、标题、方框){
var marker=new google.maps.marker({
动画:google.maps.animation.DROP,
位置:新google.maps.LatLng(lat,lng),
地图:地图,
图标:icn
}); 
地图编码=“”;
myOptions={
//信息盒的选项…等等
};
marker.ibOptions=myOptions;
google.maps.event.addListener(标记'click',函数(){
ib.setOptions(marker.ibOptions);
ib.open(地图、标记);
ib.isOpen=真;
});   
返回标记;
}
如果这样做,则每次调用ib.isOpen=false的ib.close()时都需要重置标志;(您没有指定在什么情况下关闭该框)

如果需要打开多个框:

function add_marker(lat,lng,icn,title,box_html) {

    var marker = new google.maps.Marker({
        animation : google.maps.Animation.DROP,
        position : new google.maps.LatLng(lat,lng),
        map : map,
        icon : icn
    }); 

    mapcode = '<this is the code of infobox to show>';

    myOptions = {
         //options of the infobox...bla bla
    };

    var ib = new InfoBox(myOptions);
    ib.isOpen = false;
    marker.ib = ib;

    google.maps.event.addListener(marker, 'click', function() {
        marker.ib.open(map, marker);
        marker.ib.isOpen = true;
    });   
    return marker;
}
功能添加标记(lat、lng、icn、标题、方框、html){
var marker=new google.maps.marker({
动画:google.maps.animation.DROP,
位置:新google.maps.LatLng(lat,lng),
地图:地图,
图标:icn
}); 
地图编码=“”;
myOptions={
//信息盒的选项…等等
};
var ib=新信息框(myOptions);
ib.isOpen=假;
marker.ib=ib;
google.maps.event.addListener(标记'click',函数(){
marker.ib.open(map,marker);
marker.ib.isOpen=真;
});   
返回标记;
}
同样,如果调用allMarkers[…].ib.close(),则需要使用allMarkers[…].ib.isOpen=false重置标志


我希望这能有所帮助。

如果您需要随时打开一个信息框,您可以这样做:

    var ib = new InfoBox();
    ib.isOpen = false;
    function add_marker(lat,lng,icn,title,box_html) {

    var marker = new google.maps.Marker({
        animation : google.maps.Animation.DROP,
        position : new google.maps.LatLng(lat,lng),
        map : map,
        icon : icn
    }); 

    mapcode = '<this is the code of infobox to show>';

    myOptions = {
         //options of the infobox...bla bla
    };
    marker.ibOptions = myOptions;

    google.maps.event.addListener(marker, 'click', function() {
        ib.setOptions(marker.ibOptions);
        ib.open(map, marker);
        ib.isOpen = true;
    });   
    return marker;
}
var ib=new InfoBox();
ib.isOpen=假;
功能添加标记(lat、lng、icn、标题、方框){
var marker=new google.maps.marker({
动画:google.maps.animation.DROP,
位置:新google.maps.LatLng(lat,lng),
地图:地图,
图标:icn
}); 
地图编码=“”;
myOptions={
//信息盒的选项…等等
};
marker.ibOptions=myOptions;
google.maps.event.addListener(标记'click',函数(){
ib.setOptions(marker.ibOptions);
ib.open(地图、标记);
ib.isOpen=真;
});   
返回标记;
}
如果这样做,则每次调用ib.isOpen=false的ib.close()时都需要重置标志;(您没有指定在什么情况下关闭该框)

如果需要打开多个框:

function add_marker(lat,lng,icn,title,box_html) {

    var marker = new google.maps.Marker({
        animation : google.maps.Animation.DROP,
        position : new google.maps.LatLng(lat,lng),
        map : map,
        icon : icn
    }); 

    mapcode = '<this is the code of infobox to show>';

    myOptions = {
         //options of the infobox...bla bla
    };

    var ib = new InfoBox(myOptions);
    ib.isOpen = false;
    marker.ib = ib;

    google.maps.event.addListener(marker, 'click', function() {
        marker.ib.open(map, marker);
        marker.ib.isOpen = true;
    });   
    return marker;
}
功能添加标记(lat、lng、icn、标题、方框、html){
var marker=new google.maps.marker({
动画:google.maps.animation.DROP,
位置:新google.maps.LatLng(lat,lng),
地图:地图,
图标:icn
}); 
地图编码=“”;
myOptions={
//信息盒的选项…等等
};
var ib=新信息框(myOptions);
ib.isOpen=假;
marker.ib=ib;
google.maps.event.addListener(标记'click',函数(){
marker.ib.open(map,marker);
marker.ib.isOpen=真;
});   
返回标记;
}
同样,如果调用allMarkers[…].ib.close(),则需要使用allMarkers[…].ib.isOpen=false重置标志


我希望这会有所帮助。

您可以检查谷歌地图的点击事件监听器中是否已打开信息窗口。您可以这样检查:

var ib = new InfoBox(myOptions);
google.maps.event.addListener(marker, 'click', function() {
    if(ib)
        ib.close();
    marker.ib.open(map, marker);
}

我希望这能解决您的问题。

您可以检查谷歌地图的点击事件监听器中是否已打开信息窗口。您可以这样检查:

var ib = new InfoBox(myOptions);
google.maps.event.addListener(marker, 'click', function() {
    if(ib)
        ib.close();
    marker.ib.open(map, marker);
}

我希望这能解决您的问题。

为什么不在打开一个框时设置一个全局变量,然后在关闭时重置它呢?因为有50多个标记,所有标记都有不同的内容要显示,所以为了得到这些,我使用了infobox的不同对象……然后您可以使用一个布尔数组。或将字段附加到包含其状态的长方体对象。或者(我个人认为这是最好的解决方案,但可能现在更改有点晚)使用一个框并动态更改其锚定和内容(只有在只有一个框可以打开时才可行,在这种情况下,您不必担心手动关闭其他框)您能提供一些实现此解决方案的url或博客链接吗?您希望看到哪个解决方案实现?粘贴一些代码,说明如何创建标记和