2012-01-13 104 views
2

this page上,我显示了一张Google地图,上面显示了地图标记上显示的InfoBubble。 InfoBubbles最初是开放的。如果关闭它们,您可以通过点击标记重新打开它们。我想最初关闭InfoBubbles。相关的功能(简化,以去除不相关的东西)是:带有信息气泡的谷歌地图

addMarker = function(festivalData, hasPopup) { 

    var map = this._map; 

    this.marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(festivalData.latitude, festivalData.longitude), 
     map: map 
    }); 

    if (hasPopup) { 
     var infoBubble = new InfoBubble({ 
      map: map, 
      content: "<a href='" + festivalData.url + "'>" + festivalData.name + "</a>", 
      hideCloseButton: false, 
     }); 

     infoBubble.open(map, this.marker); 

     var infoBubbleHandler = function(bubble) { 
      return function() { 
       if (!bubble.isOpen()) { 
        bubble.open(map, this.marker); 
       } 
      } 
     }(infoBubble); 

     google.maps.event.addListener(this.marker, 'click', infoBubbleHandler); 
    } 
} 

我预计,通过删除线

infoBubble.open(map, this.marker); 

这会实现我的目标,但是这只是完全消除InfoBubbles,使他们不点击标记时甚至不会出现。我怎样才能让InfoBubbles最初显示关闭,但是当你点击一个标记时它们会打开?

回答

1

改变了所有引用this.marker一个局部变量,并改变了处理函数的创造,它的工作原理我的思维方式,你希望它

addMarker = function(festivalData, hasPopup) { 

    var map = this._map; 

    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(festivalData.latitude, festivalData.longitude), 
     map: map 
    }); 

    if (hasPopup) { 
     var infoBubble = new InfoBubble({ 
      map: map, 
      content: "<a href='" + festivalData.url + "'>" + festivalData.name + "</a>", 
      hideCloseButton: false, 
     }); 

     //infoBubble.open(map, this.marker); 

     var infoBubbleHandler = function() { 
      if (!infoBubble.isOpen()) { 
       infoBubble.open(map, marker); 
      } 
     }; 

     google.maps.event.addListener(marker, 'click', infoBubbleHandler); 
    } 
} 
0

这是一个奇怪的行为,我也希望它在删除打开调用后工作。

从我的头顶 - 尝试将其打开后立即关闭泡沫:

infoBubble.open(map, this.marker); 
infoBubble.close(); 
+0

我已经考虑过这个。如果我找不到“合适的”解决方案,这是我的最后手段。感谢您的建议。 – 2012-01-13 10:56:54

+0

我同意这不是一个“适当的”解决方案。检查infoBubbleHandler中的_open_是否被调用。如果是这样,那么除了调试InfoBubble的_open__方法,我猜想没有别的办法可以做:/ – 2012-01-13 11:12:44

1

这是一个简单的方法,使一个标记开头看不见,弹出它在点击打开

var myLatLng = new google.maps.LatLng(some_lat, some_lng); 
var marker = new google.maps.Marker({ 
     position: myLatLng, 
     map: your_map, 
     title: some_title, 
     zIndex: some_z_index_int, 
     clickable: true 
    }); 

infoBubble = new InfoBubble({ 
     content: '<div>Some label</div>', 
     shadowStyle: 1, 
     padding: 0, 
     backgroundColor: 'rgb(57,57,57)', 
     borderRadius: 4, 
     arrowSize: 10, 
     borderWidth: 1, 
     borderColor: '#2c2c2c', 
     disableAutoPan: true, 
     hideCloseButton: true, 
     arrowPosition: 10, 
     arrowStyle: 2 
    }); 

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

      infoBubble.open(map, marker); 
     } 
})(marker, i));