4

前使用谷歌地圖V2,我能夠從去地圖和處理,並取消鼠標滾動事件縮放地圖停止滾動鼠標(DOMMouseScroll)事件。但是,在v3中,這不再有效。GMaps V3:如何取消鼠標滾動的事件,他們滾動地圖

Here is an example. Try to scroll through the text with the mouse wheel

注意如何拖動,他們得到的地圖之前雙擊被取消,但是如果你試圖通過文字滾動,那麼DOMMouseScroll事件去一直到地圖。

取消事件的代碼基本相同,V2,看起來像這樣:

// Set the overlay's div_ property to this DIV 
    this.div_ = div; 

    var cancelEvent = function(e) 
    { 

     if((navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) || 
        navigator.userAgent.indexOf('Opera') > -1) { 
      window.event.cancelBubble = true; 
      window.event.returnValue = false; 
     } else { 
      e.stopPropagation(); 
     } 

     return false; 
    } 

    var panes = this.getPanes(); 
    panes.floatPane.appendChild(div); 

    var stealEvents = [ 'mousedown', 'dblclick', 'DOMMouseScroll', 'onmousewheel', 'drag']; 

    for(i=0; i < stealEvents.length; i++){ 
    google.maps.event.addDomListener(this.div_, stealEvents[i], cancelEvent); 
    } 


    // for IE/Opera 
    if((navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) || 
        navigator.userAgent.indexOf('Opera') > -1) { 
     this.div_.attachEvent('onmousewheel', cancelEvents); 
    } 

    // for safari 
    if (navigator.userAgent.indexOf('AppleWebKit/') > -1) { 
     this.div_.onmousewheel = cancelEvents; 
    } 

回答

9

當初始化V3地圖,你可以指定一個選項來禁用滾輪縮放:

var mapOptions = { 
    center: new google.maps.LatLng(-34.397, 150.644), 
    zoom: 8, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    scrollwheel: false 
}; 
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

Google Map V3 - Map Options

您正在查找的選項是撥輪 - 您想將其設置爲False - b y默認設置爲True。從約翰

+0

好吧。我是想只取消該事件,但關閉地圖縮放/移動在必要時也可以。我想你不能在第二版中取消那個事件。 – 2013-05-01 16:59:12

1

真棒回答,我不能發表評論[由於代表]所以我回答這個問題的方式 - 即使已經回答了(兩年前)接受。

顯然JS的布爾是小寫,所以正確的代碼是:

var mapOptions = { 
    center: new google.maps.LatLng(-34.397, 150.644), 
    zoom: 8, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    scrollwheel: false 
}; 
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);