2014-01-30 19 views
-1

是否有任何可能刪除某些Google地圖標記並必須執行map.clear()?因爲在我的應用程序中,我有幾個複選框標記和未標記的某些標記...Android:Google地圖中的chekbox標記和取消標記

我該如何實現這一目標?

+0

這是GoogleMapsv2嗎? –

+0

是的。這是谷歌地圖v2 – user3231711

+0

當你檢查當時的複選框保存該標記實例,並在最後刪除所有保存的標記通過使用marker.remove(); – Vijju

回答

0

我在過去做過類似的事情。

關鍵是要保持Marker對象的列表,而是你自己的自定義類中(我創建了一個名爲MapPoint類具有經緯度,標題,圖標,片段,並持有Marker)。

然後,當你想將更新推送到您創建MapPoint對象(與Marker設置爲NULL)與主動MapPoint對象的當前目錄的列表中選擇地圖,刪除重複和空的不再存在。這使您可以儘可能少地更新地圖,而不是對所有內容進行全面更新。

程式碼片段以方便您閱讀評論:

// holds the current list of MapPoint objects 
public ArrayList<MapPoint> mCurrentPoints = new ArrayList<MapPoint>(); 

public void addMapPoints(ArrayList<MapPoint> mapPoints) { 
    // only process if we have an valid list 
    if (mapPoints.size() > 0) { 
     // iterate backwards as we may need to remove entries 
     for (int i = mCurrentPoints.size() - 1; i >= 0; i--) { 
      // get the MapPoint at this position 
      final MapPoint point = mCurrentPoints.get(i); 
      // check if this point exists in the new list 
      if (!mapPoints.contains(point)) { 
       // if it doesn't exist and has a marker (null check), remove 
       if (point.mMarker != null) { 
        // removes Marker from mMap 
        point.mMarker.remove(); 
       } 
       // remove our MapPoint from the current list 
       mCurrentPoints.remove(i); 
      } else { 
       // already exists, remove from the new list 
       mapPoints.remove(point); 
      } 
     } 
     // add all the remaining new points to the current list 
     mCurrentPoints.addAll(mapPoints); 

     // go through every current point. If no mMarker, create one 
     for (MapPoint mapPoint : mCurrentPoints) { 
      if (mapPoint.mMarker == null) { 
       // create Marker object via mMap, save it to mapPoint 
       mapPoint.mMarker = mMap.addMarker(new MarkerOptions() 
        .position(mapPoint.getLatLng()) 
        .title(mapPoint.getTitle()) 
        .icon(mapPoint.getMarker()) 
        .snippet(mapPoint.getInfoSnippetText())); 
      } 
     } 
    } 
} 

所以,你會希望有一個方法,它決定了Marker對象中顯示,爲他們創造一個MapPoint對象,然後把他們的名單這addMapPoints()方法。

一對夫婦的好點子:synchronizemCurrentPoints名單(去除,以使代碼片斷簡單),並確保您的添加/刪除標記在UI線程運行。 (好主意做這個處理關閉主線程,然後跳到它爲實際添加/刪除Markers)。當然適應你自己的情況。

0

Marker對象上使用setVisible(true)(不是MarkerOptions,這很重要)。將所有您想要的標記添加爲可見/不可見,將對這些對象的引用保留下來,並根據需要切換它們。