2012-02-25 81 views

回答

3

您可以使用地圖的setOptions方法來隱藏或顯示控件。將想要顯示/隱藏的所有controlOptions作爲參數傳遞給一個對象,並將控件的值設置爲true或false。

將mouseout和mouseover的eventlisteners添加到地圖並在其中設置選項。

例子:

//the controls you want to hide 
    var controlsOut={ 
        mapTypeControl:false, 
        zoomControl:false, 
        panControl:false, 
        streetViewControl:false 
        }; 

    //create a copy of controlsOut and set all values to true 
    var controlsIn={}; 

     for(var c in controlsOut) 
     { 
     controlsIn[c]=true; 
     } 

    //initially hide the controls 
    map.setOptions(controlsOut) 

    //add listeners to show or hide the controls 
     google.maps.event.addDomListener(map.getDiv(), 
           'mouseover', 
           function(e) 
           { 
            e.cancelBubble=true; 
            if(!map.hover) 
            { 
             map.hover=true; 
             map.setOptions(controlsIn); 
            } 
            }); 

     google.maps.event.addDomListener(document.getElementsByTagName('body')[0], 
           'mouseover', 
           function(e) 
           { 
            if(map.hover) 
            { 
            map.setOptions(controlsOut); 
            map.hover=false; 
            } 
           }); 
+0

你能看看crossborders.tv/client/frisbie。請滾動到底部的聯繫部分。我無法讓你的代碼工作。 – user1215071 2012-02-27 16:38:59

+0

把上面的代碼放在這行後面:'map.mapTypes.set(frisbieMapID,mapType); ',爲我工作。 – 2012-02-27 17:08:55

+0

謝謝!得到它的工作! – user1215071 2012-02-27 17:33:21

0

這似乎是關於關於製作地圖控件懸停只有唯一的問題。上面的答案對我來說不是很有用,所以我想我會記錄我自己的修改:

// dom is the enclosing DOM supplied to new google.maps.Map 
// controlsIn and controlsOut are hashes of options to set 
// when the mouse enters or exits. 

$(dom).mouseenter(function(evt) { 
    if (!map.hover) { 
    map.hover = true 
    map.setOptions(controlsIn) 
    } 
}); 

$('body').mouseover(function(evt) { 
    if (map.hover) { 
    if ($(evt.target).closest(dom).length == 0) { 
     map.hover = false 
     map.setOptions controlsOut 
    } 
    } 
});