在谷歌地图中切换信息框

时间:2012-08-14 06:10:36

标签: 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 : 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;
    }

我是谷歌地图中的新手,所以可能是我错过了一些非常小的东西......在此先感谢.... ANKUR

2 个答案:

答案 0 :(得分:2)

如果你只需要打开一个信息框就可以这样做:

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

如果你这样做,你需要在每次用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;
}

如果你曾经调用allMarkers [...]。ib.close(),你将需要使用allMarkers重置标志[...]。ib.isOpen = false;

我希望这会有所帮助。

答案 1 :(得分:1)

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

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

我希望这可以解决您的问题。