2012-12-05 68 views
0

Javascript新手....谷歌地圖v3 - 放大/縮小能夠重複(和recenter)?

我有一個帶有多個POLYGON區域的谷歌地圖。我已經想出瞭如何通過單擊多邊形來放大,然後使用「dblclick」偵聽器放大ZOOM OUT並刪除它們的偵聽器...

一旦您單擊放大,然後雙擊即可縮小,它不允許我再次重複該功能。有沒有辦法多次放大和縮小?

下面是我的代碼有:

 //POLYGON ZONE 1 ================================ 
    var zone1; 

    var triangleCoords = [ 
    new google.maps.LatLng(29.738692, -95.399331), 
    new google.maps.LatLng(29.738688, -95.394781), 
    new google.maps.LatLng(29.737048, -95.394781), 
    new google.maps.LatLng(29.737048, -95.399331) 
    ]; 

    // Construct the polygon 
    // Note that we don't specify an array or arrays, but instead just 
    // a simple array of LatLngs in the paths property 
    zone1 = new google.maps.Polygon({ 
    paths: triangleCoords, 
    strokeColor: "#00AEEF", 
    strokeOpacity: 0.8, 
    strokeWeight: 2, 
    fillColor: "#00AEEF", 
    fillOpacity: 0.05, 
    }); 

    zone1.setMap(map); 
    zone1._myBounds = new google.maps.LatLngBounds(); 
    for (var i=0; i<triangleCoords.length; i++) 
    { 
    zone1._myBounds.extend(triangleCoords[i]); 
    } 

    var listener = google.maps.event.addListener(zone1, 'click', function() { 
     map.fitBounds(zone1._myBounds); 
     google.maps.event.removeListener(listener); 
    }); 

    var listener2 = google.maps.event.addListener(zone1, 'dblclick', function() { 
    if (map.getZoom() > 16) map.setZoom(13); 
    //map.setZoom(map.getZoom() -1); 
    //map.setCenter(map.getCenter()); 
    google.maps.event.removeListener(listener2); 
    google.maps.event.addListener(zone1, 'click', function(){ 
     map.fitBounds(zone1._myBounds);}); 
    }); 

對於我的其他多邊形區域,我也必須改變變量名稱爲他們工作呢?

此外,返回中心似乎沒有正常工作。如果我需要在另一個問題中提出這個問題,我會。

感謝您的幫助!

+0

你爲什麼要刪除,如果聽衆你想再做一次? – geocodezip

回答

0

眼前的問題是,你不這樣做的點擊收聽你DBLCLICK添加同樣的事情:

google.maps.event.addListener(zone1, 'click', function(){ 
    map.fitBounds(zone1._myBounds);}); 

VS原文:

var listener = google.maps.event.addListener(zone1, 'click', function() { 
    map.fitBounds(zone1._myBounds); 
    google.maps.event.removeListener(listener); 
}); 
+0

所以通過使用這個代碼爲polygon1,它不會影響polygon2? : 'google.maps.event.addListener(zone1,'click',function(){ map.fitBounds(zone1._myBounds);}); (map.getZoom()> 16)map.setZoom(13);});' –