2014-01-29 150 views
-1

我有一個eventlistener正在偵聽我的谷歌地圖對象上的「mouseout」事件。 這在大多數情況下效果很好,因爲在光標離開地圖邊界時會激發它。當鼠標懸停在標記上時,GMaps「mouseout」事件不會觸發

但是,如果在光標離開它的地圖邊緣有一個標記,則它不會觸發。 爲了說明請參見本圖片:

enter image description here

有誰知道一個方法來解決這個問題?

+0

您可以爲'mouseover'設置標記事件偵聽器,併爲地圖對象觸發事件'mouseout'。 –

+0

你能提供演示嗎?對於我來說,當我通過標記(或任何其他類型的覆蓋圖)離開地圖時,我也會發生鼠標失火。你使用哪個瀏覽器? –

+1

@AntoJurković不幸的是,這並不適用於我的情況,因爲我每次擊中標記都無法觸發此事件。 – Leonard

回答

0

我結束了同時使用谷歌地圖事件偵聽器它解決:

this.cursorLeavesMapListener = new google.maps.event.addListener(this.mapObject.gObj, "mouseout", function(e){thisStageMachine.cursorLeftMap()}); 

這個類在這裏:

(function(window) 
{ 
    function cursorState(stageMachine) 
    { 
     this.stageMachine = stageMachine; 
     this.state = "out"; 

     this.currentCheckStateTimer = null; 
     var thisCursorState = this; 
     this.stageMachine.mapObject.mapDOMDiv.addEventListener("mouseover", function(e){ thisCursorState.setInMapState() }); 
     this.stageMachine.mapObject.mapDOMDiv.addEventListener("mouseout", 
      function(e){ 
       thisCursorState.setOutOfMapState(); 
       window.clearTimeout(thisCursorState.currentCheckStateTimer); 
       thisCursorState.currentCheckStateTimer = window.setTimeout(function(){thisCursorState.checkState()}, 100); 
      }); 
    } 

    cursorState.prototype.setInMapState = function() 
    { 
     this.state = "in"; 
    } 

    cursorState.prototype.setOutOfMapState = function() 
    { 
     this.state = "out"; 
    } 

    cursorState.prototype.checkState = function() 
    { 
     if(this.state=="out") 
     { 
      this.stageMachine.cursorLeftMap(); 
     } 
    } 

    window.cursorState = cursorState; 
}(window)); 

您需要使用鼠標懸停和鼠標移開的DOMversions這個奇怪的構造,因爲當你在地圖上移動你的光標時,它們會不斷髮射。這可能與通過谷歌地圖將圖塊添加到DOM的方式有關。 然而,只有鼠標事件觸發,鼠標離開地圖時,檢查是否沒有新的鼠標懸停事件觸發(自上次觸發鼠標懸停後)100毫秒後,您需要確保我們確實已離開mouseout被觸發時的地圖。 這顯然很不老實,但它像一個魅力。

其中一個總是火災。

如果有人有更優雅的解決方案,請分享。

相關問題