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

私はグーグルマップに慣れていないので、いくつかの非常に小さなものが欠けている可能性があります...事前に感謝します....アンクル

4

2 に答える 2

2

いつでも1つのインフォボックスを開く必要がある場合は、次のようにすることができます。

    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 に答える
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 に答える