2010-05-25 42 views
29

所有可見的標記這裏有幾點:如何獲得當前的縮放級別

  1. 我與它除了在地圖的右側面板上相關的地圖和記錄上的一些標記。它們通過數字標識進行連接,數字標識存儲爲標記屬性。
  2. 所有標記都存儲在一個數組中。
  3. 當用戶放大地圖時,應在右側面板上顯示與僅可見標記相關聯的記錄。

那麼,如何獲取當前縮放級別上所有可見標記的列表?我在互聯網上搜索,沒有找到有用的東西。可能會發現某種我想要實現的功能here

回答

22

使用GMap2.getBounds()可以找到邊界框。使用GLatLngBounds.containsLatLng()檢查每個標記以查看它是否可見。

+0

謝謝你,它的工作。對於另一位尋求解決方案的人,別忘了通過GMarker.getLatLng() – 2010-05-26 11:55:18

44

在谷歌地圖的JavaScript API V3,我們可以使用這樣的事情:

var markers; // your markers 
var map; // your map 
for (var i=0; i<markers.length; i++){ 
    if(map.getBounds().contains(markers[i].getPosition())){ 
     // code for showing your object, associated with markers[i] 
    } 
} 
18

我知道你想要的API V2,但我不得不糾正一些東西,我在@ bruha對V3響應看到,萬一有人來尋找它:

var markers; // your markers 
var map; // your map 

for(var i = markers.length, bounds = map.getBounds(); i--;) { 
    if(bounds.contains(markers[i].getPosition())){ 
     // code for showing your object 
    } 
} 

通過這樣的方式經過標記更快的陣列的方向倒退,加上我們之前進入循環所設定的範圍爲變量,因此我們不會每次都要求它,我們經歷循環,唯一的要求是我們要做的一個特定的標記位於邊界內。

編輯:瘋玩我縮減器

編輯:map.getBounds()是應該的,是map.getBounds

+0

謝謝。它適用於Android .. – 2013-12-19 09:25:18

+0

任何想法對於V2的相同的東西會是什麼樣子? – 2015-02-18 12:00:22

+2

爲什麼反向更快地通過陣列? – jayp 2016-03-25 03:52:55

1

這很容易代碼。試試這個代碼。

private boolean CheckVisibility(Marker marker) 
{ 
    if(googleMap != null) 
    { 
     //This is the current user-viewable region of the map 
     LatLngBounds latLongBounds = googleMap.getProjection().getVisibleRegion().latLngBounds; 

      if(latLongBounds.contains(marker.getPosition())) 
        //If the item is within the the bounds of the screen 
        return true; 
      else 
        //If the marker is off screen 
        return false; 
    } 
    return false; 
} 
0

我的代碼片段

private boolean isAnyMarkerVisible(LatLng ll) { 
    if(gMap != null && markersData != null) { 
     final LatLngBounds latLongBounds = LatLngBounds.builder().include(ll).build(); 
     for (Store store : markersData) { 
      if (latLongBounds.contains(store.getLatLng())) { 
       return true; 
      } 
     } 
    } 
    return false; 
} 
相關問題