2013-11-25 32 views
2

我用下面的方法添加的標記在地圖上,也保持了標記記錄如何刪除Google map v2中的標記?

public static void showallLocations() 
    { 
     ArrayList<LinkedHashMap<String, String>> listshow=latLngStoragepreference.getLatlng(); 
     markerlist=new ArrayList<Marker>();// to keep the marker record so that later we can delete 
     if(listshow.size()>0) 
     { 
     for(int i=0;i<listshow.size();i++) 
     { 
     LinkedHashMap<String, String> Linkedhashmap=listshow.get(i); 

     Set mapSet = (Set) Linkedhashmap.entrySet(); 
     //Create iterator on Set 
     Iterator mapIterator = mapSet.iterator(); 

     Map.Entry mapEntry = (Map.Entry) mapIterator.next(); 
     // getKey Method of HashMap access a key of map 
     String keyValue = (String) mapEntry.getKey(); 
     //getValue method returns corresponding key's value 
     String value = (String) mapEntry.getValue(); 
     String[] parts=value.split("#"); 
     String Latitude=parts[0]; 
     String Longitude=parts[1]; 
     Double Latitudeconverted=Double.parseDouble(Latitude); 
     Double Longitudeconverted=Double.parseDouble(Longitude); 
     System.out.println(Latitudeconverted+""+Longitudeconverted); 
     //show on map 
     LatLng latLngs=new LatLng(Latitudeconverted, Longitudeconverted); 

     Marker marker=map.addMarker(new MarkerOptions() 
     .position(
       latLngs) 
       .title(keyValue) 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_navigate_to))); 
     markerlist.add(marker);// keeping the record of marker object 

     } 
     } 


    } 

定製baseadapter,我試圖刪除標記,但marker.remove()沒有工作

holder.btnDeletelocation.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 


     Marker marker= MainActivity.markerlist.get(position); 
     Log.d("MARKERlist before Remove", MainActivity.markerlist.toString()); 

     Log.d("MARKER Title",marker.getTitle()); 


     marker.remove(); 
     marker.setVisible(false); 
     Log.d("MARKERlist after Remove", MainActivity.markerlist.toString()); 



      notifyDataSetChanged(); 




      } 
     }); 

請幫助如果任何人都經歷過相同的過程。在此先感謝

+0

看到這個問題, HTTP://計算器.com/questions/13692398/remove-a-marker-from-a-googlemap –

+0

如果您調用'googleMap.clear()'而不是刪除單個標記,它會起作用嗎? –

+0

@MaciejGórski不,它不起作用,它會清除完整的地圖。我得到了我自己的答案...你在同一個問題? –

回答

10

我谷歌地段,發現有沒有簡單的方法來刪除地圖標記, 每當你添加標記到地圖不要忘記保留它的記錄,如將其添加到地圖或ArrayList。

your_google_map_obj.addMarker(new MarkerOptions()) //this adds Marker on Google Map, you should know it always returns Marker object so that you can use it later especially for removal. 

所以Marker marker=your_google_map_obj.addMarker(new MarkerOptions())添加此標記對象列表或地圖markerArraylist.add(marker);那麼容易,你可以通過 Marker marker=markerArraylist.get(index);提取列表標記,然後調用marker.remove();

+1

謝謝你的男人,你節省了我的時間 – mdDroid

0
Marker currentMarker = null; 
if (currentMarker!=null) { 
    currentMarker.remove(); 
    currentMarker=null; 
} 

if (currentMarker==null) { 
    currentMarker = mMap.addMarker(new MarkerOptions().position(arg0). 
    icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); 
} 
+0

這是刪除你的標記? –

+0

是刪除我的最後一個標記並創建當前標記 –

+0

其工作嘗試!我不會解決whay -1!其誤導人民! –

相關問題