2013-01-04 48 views
0

我有一個Google Map(API v3),其中填充了指示數據庫數據的標記。我想添加一個側欄,用於檢查地圖上當前可見的標記,並將它們添加到側欄,並在地圖移動時進行調整。有這樣的可能嗎?谷歌地圖上的側邊欄對地圖做出反應

回答

1

我有一個GitHub project,它加載包含地標的KML文件,將它們放在邊欄中,並在更改地圖時使用Google地圖API調暗/增亮地點。相關的部分是這樣的片段:

google.maps.event.addListener(mapInstance, 'bounds_changed', function() { 
    currentBounds = mapInstance.getBounds(); 
    for (i = 0; i < parser.docs[0].placemarks.length; i++) { 
     var myLi = $("#p" + i); 
     if (currentBounds.contains(parser.docs[0].placemarks[i].marker.getPosition())) { 
      myLi.css("color","#000000"); 
     } else { 
      myLi.css("color","#CCCCCC"); 
     } 
    } 
}); 

的是,簡化版本是:

google.maps.event.addListener(mapInstance, 'bounds_changed', function() { 
    currentBounds = mapInstance.getBounds(); 
    /* 
    Loop over Google map placemarks and check the placemark by doing: 
    if (currentBounds.contains(currentPlacemark.getPosition())) { 
     // Change CSS to make sidebar entry visible 
    } else { 
     // Change CSS to make sidebar entry invisible 
    } 
    */ 
}); 

在我的例子中,mapInstance是google.maps.Map的一個實例,currentPlacemark是一個實例您的google.maps.Marker對象。

+0

這就是我正在尋找的。謝謝。 – hubert