0

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

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

4

2 回答 2

2

您可以检查是否已在 Google 地图的单击事件侦听器中打开了信息窗口。您可以像这样检查它:

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

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

于 2012-10-03T11:34:19.450 回答
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; 重置标志;

我希望这有帮助。

于 2012-08-14T09:40:49.203 回答