2012-11-04 45 views

回答

0

此問題與此相似 - Google Maps API v3: Close infowindow when DOM element is clicked - 所以答案適用。

關鍵區別在於您想要檢測鼠標懸停事件,這將是參考問題和答案中提到的"mouseover"事件而不是"click"事件。

google.maps.event.addListener(marker, 'mouseover', function() { 
    infowindow.setContent(contentString); 
    infowindow.open(map, marker); 
}); 

無論您的問題是否服務多個標記,我們可以這樣做: -

var infowindow = null; 
. 
. 
. 
/* now inside your initialise function */ 
infowindow = new google.maps.InfoWindow({ 
    content: "holding..." 
}); 

for (var i = 0; i < markers.length; i++) { 
    var marker = markers[i]; 
    google.maps.event.addListener(marker, 'mouseover', function() { 
     //open the infowindow when it's not open yet 
     if(contentStringCal!=infowindow.getContent()) 
     { 
      infowindow.setContent(contentStringCal); 
      infowindow.open(map, marker); 
     } 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
     //when the infowindow is open, close it an clear the contents 
     if(contentStringCal==infowindow.getContent()) 
     { 
      infowindow.close(map, marker); 
      infowindow.setContent(''); 
     } 
     //otherwise trigger mouseover to open the infowindow 
     else 
     { 
      google.maps.event.trigger(marker, 'mouseover'); 
     } 
    }); 

    //clear the contents of the infwindow on closeclick 
    google.maps.event.addListener(infowindow, 'closeclick', function() { 
      infowindow.setContent(''); 
    }); 
} 
+0

Ty爲您的anwser,但我的地圖與多標記。例如:http://fiddle.jshell.net/spYYh/13/ – user1798101

+0

已更新,以顯示如何使用for-loop將addLister添加到您擁有的所有標記(指定您自己的「i」值基於你有標記),每個標記都有自己的html內容。 –