1

我试图让它一次只显示一个信息泡泡,换句话说就是切换信息泡泡。标记在 for 循环中被分配此事件处理程序,循环通过标记数组。

目前,这以一种打开信息气泡但从不关闭它的方式起作用。我可以单击多个标记,每个标记都会有信息气泡而不关闭前一个标记

Iv 尝试添加infobuble2.close();,但 iv 得到的关闭是我必须单击标记两次才能关闭以前的 infobubbles。

Iv 还尝试测试 infobubble 是否打开,使用infoBubble2.isOpen()它似乎只是错误的。

是的,如果您想知道我已经尝试过 infowindow,并且使用它的功能很好,但是由于设计限制,我需要相应地设置弹出窗口的样式,下面是我的代码

  var infoBubble2;

  google.maps.event.addListener(marker, 'click', function() {

             infoBubble2 = new InfoBubble({
                map: map,
                position: new google.maps.LatLng(-35, 151),
                shadowStyle: 1,
                padding: 0,
                backgroundColor: 'rgb(255,255,255)',
                borderRadius: 4,
                arrowSize: 10,
                borderWidth: 1,
                borderColor: '#2c2c2c',
                disableAutoPan: true,
                hideCloseButton: !0,
                arrowPosition: 30,
                backgroundClassName: 'phoney',
                arrowStyle: 2
              });

            infoBubble2.setContent("some content");
            infoBubble2.open(map, marker);
  });
4

3 回答 3

2

尝试这样的事情:

function f_createBalloon(num, marker) {
    balloons[num] = new google.maps.InfoWindow({
        content: ballonInfo[num]
    });
    balloonListeners[num] = new google.maps.event.addListener(marker, 'click',
        function () {
            f_closeBalloons();
            balloons[num].open(map, marker);
        });
}

function f_closeBalloons() {
    for (var j in balloons) {
        if (balloons[j]) balloons[j].close(map, markers[j]);
    }
}
于 2013-08-13T15:24:50.677 回答
2

如果您只希望一次显示一个对象,则只创建一个对象并重新使用它,而不是不断创建新对象并尝试管理它们,如下所示:

 var infoBubble2 = new InfoBubble({
    map: map,
    position: new google.maps.LatLng(-35, 151),
    shadowStyle: 1,
    padding: 0,
    backgroundColor: 'rgb(255,255,255)',
    borderRadius: 4,
    arrowSize: 10,
    borderWidth: 1,
    borderColor: '#2c2c2c',
    disableAutoPan: true,
    hideCloseButton: !0,
    arrowPosition: 30,
    backgroundClassName: 'phoney',
    arrowStyle: 2
  });


  google.maps.event.addListener(marker,'click',function() {
    infoBubble2.close();
    infoBubble2.setContent("some content");
    infoBubble2.open(map, marker);
  });
于 2013-08-13T15:28:20.113 回答
1

我通过在 initialize() 函数之外声明 infobubble2 变量然后为其分配新 infobubble() 的值来解决它;在 initialize() 中,分配给每个标记的 onclick 事件将打开并填充信息气泡的内容。

下面是我所做的。感谢大家的帮助。

var infoBubble2;

function initialize() {
    var myOptions = {
          zoom: zoom,
          minZoom: 0, maxZoom: 20,
          center: myLatLng,
          mapTypeId: maptype,
          mapTypeControl:false,
          streetViewControl:false,
          panControl:false
      };
   map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

   infoBubble2 = new InfoBubble({
            map: map,
            position: new google.maps.LatLng(-35, 151),
            shadowStyle: 1,
            padding: 0,
            backgroundColor: 'rgb(255,255,255)',
            borderRadius: 4,
            arrowSize: 10,
            borderWidth: 1,
            borderColor: '#2c2c2c',
            disableAutoPan: true,
            hideCloseButton: !0,
            arrowPosition: 30,
            backgroundClassName: 'phoney',
            arrowStyle: 2
          });

      $.getJSON('include/jsonCatPoints.php', function(data) {
          for(var i=0; i<data.length;i++){
            placeMarker(data[i]['cat_lat'], data[i]['cat_long'], map);
          }
        });


}//ends initialize

function placeMarker(lat, longg, map) {
      var image = 'img/cat_marker.png';
         marker = new google.maps.Marker({
          position: new google.maps.LatLng(lat,longg),
          map: map,
          icon: image
        });
      makeBubble(map, "test" +lat, marker);
       markers.push(marker);
  }//ends placeMarker


function makeBubble(map, contentString, marker) {
  google.maps.event.addListener(marker, 'click', function() {
        infoBubble2.setContent("message"+contentString);
        infoBubble2.open(map, marker)
 });
}// ends makeBubble
于 2013-08-13T20:13:43.233 回答